2012-09-07 173 views
0

嗨我有一个非常奇怪的问题,在模拟器完美的作品,但是当我给iPad充电应用程序不起作用。任何想法?iOS,没有录音声音在我的iPad,在模拟器工作正常

这里我创建了所有的变量来使它工作。

- (void)viewDidLoad { 

[super viewDidLoad]; 

NSArray *dirPaths; 
NSString *docsDir; 

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"]; 

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithInt:AVAudioQualityMin], 
           AVEncoderAudioQualityKey, 
           [NSNumber numberWithInt:16], 
           AVEncoderBitRateKey, 
           [NSNumber numberWithInt: 2], 
           AVNumberOfChannelsKey, 
           [NSNumber numberWithFloat:44100.0], 
           AVSampleRateKey, 
           nil]; 

NSError *error = nil; 

audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL 
              settings:recordSettings 
               error:&error]; 
if (error) 
{ 
    NSLog(@"error: %@", [error localizedDescription]); 
} else { 
    [audioRecorder prepareToRecord]; 
} 

} 

我有一些记录,播放和停止按钮。所以这些是所有这些代码。

-(void) recordAudio{ 
if (!audioRecorder.recording){ 
    recordButton.enabled = NO; 
    playButton.enabled = NO; 
    saveButton.enabled = NO; 
    stopButton.enabled = YES; 
    [audioRecorder record]; 
} 
} 

-(void)stop{ 
recordButton.enabled = YES; 
playButton.enabled = YES; 
saveButton.enabled = YES; 
stopButton.enabled = NO;  
if (audioRecorder.recording){ 
    [audioRecorder stop]; 
} 
else if (audioPlayer.playing){ 
    [audioPlayer stop]; 
} 
} 


-(void) playAudio{ 
if (!audioRecorder.recording){ 
    recordButton.enabled = NO; 
    stopButton.enabled = YES; 
    saveButton.enabled = NO;   
    if (audioPlayer) 
     audioPlayer=nil; 
    NSError *error; 

    audioPlayer = [[AVAudioPlayer alloc] 
        initWithContentsOfURL:audioRecorder.url 
        error:&error]; 

    audioPlayer.delegate = self; 

    if (error) 
     NSLog(@"Error: %@", [error localizedDescription]); 
    else 
     [audioPlayer play]; 
} 
} 
+0

iPad没有内置麦克风。你有没有插入某种麦克风? –

+0

什么不行?错误信息或根本没有声音? –

+0

只是不健全,也许它也没有记录。 – andres83

回答

0

终于我找到了解决方案。对于我以前的代码,我在ViewdidLoad中添加了这行代码。

AVAudioSession * audioSession = [AVAudioSession sharedInstance]; 
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error]; 
[audioSession setActive:YES error: &error]; 

和设置添加此:

[NSNumber numberWithInt:kAudioFormatAppleIMA4], AVFormatIDKey 

我的代码http://www.iphoneam.com/blog/index.php?title=using-the-iphone-to-record-audio-a-guide&more=1&c=1&tb=1&pb=1复制和使用AVAudioSession的想法来自How to record and play sound in iPhone app?

如果需要完整的代码来是这个。

- (void)viewDidLoad { 

[super viewDidLoad]; 

NSError *error = nil; 

AVAudioSession * audioSession = [AVAudioSession sharedInstance]; 
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error]; 
[audioSession setActive:YES error: &error]; 

playButton.enabled = NO; 
stopButton.enabled = NO; 
saveButton.enabled = NO; 

NSArray *dirPaths; 
NSString *docsDir; 

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 
docsDir = [HSAudioController pathToDocumentsFolder]; 
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"]; 

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithInt:kAudioFormatAppleIMA4], AVFormatIDKey, 
           [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey, 
           [NSNumber numberWithInt:16], AVEncoderBitRateKey, 
           [NSNumber numberWithInt: 2], AVNumberOfChannelsKey, 
           [NSNumber numberWithFloat:44100.0], AVSampleRateKey, 
           nil]; 

audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL 
              settings:recordSettings 
               error:&error]; 
NSLog(@"%@",audioRecorder.url); 
if (error) 
{ 
    NSLog(@"error: %@", [error localizedDescription]); 
} else { 
    [audioRecorder prepareToRecord]; 
} 
} 
相关问题