2010-04-20 59 views
6

这是我的方法:AVAudioRecorder不会记录在设备

-(void) playOrRecord:(UIButton *)sender { 
    if (playBool == YES) { 
     NSError *error = nil; 
     NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat: @"%d", [sender tag]] ofType:@"caf"]; 
     NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; 
     AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error]; 
     [player setNumberOfLoops:0]; 
     [player play];  
    } 
    else if (playBool == NO) { 
     if ([recorder isRecording]) { 
      [recorder stop]; 
      [nowRecording setImage:[UIImage imageNamed:@"NormalNormal.png"] forState:UIControlStateNormal]; 
      [nowRecording setImage:[UIImage imageNamed:@"NormalSelected.png"] forState:UIControlStateSelected]; 
     } 
     if (nowRecording == sender) { 
     nowRecording = nil; 
     return; 
     } 
     nowRecording = sender; 
     NSError *error = nil; 
     NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat: @"%d", [sender tag]] ofType:@"caf"]; 
     NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; 
     [sender setImage:[UIImage imageNamed:@"RecordingNormal.png"] forState:UIControlStateNormal]; 
     [sender setImage:[UIImage imageNamed:@"RecordingSelected.png"] forState:UIControlStateSelected];   
     recorder = [[AVAudioRecorder alloc] initWithURL:fileUrl settings:recordSettings error:&error]; 
     [recorder record]; 
    } 
} 

大部分是自我解释; playBool是BOOL,当它处于播放模式时为YES。 但是,当我在设备上运行它时,[记录器记录]返回NO。有没有人有一个线索,为什么会发生这种情况?

回答

3

好的,解决了我自己的问题;我不知道为什么,但是我必须在设备上记录到NSTemporaryDirectory。改变它完全解决了它。

+0

因为你不能更改应用程序捆绑!大声笑 – 2014-03-28 13:45:33

4

这是因为您无法修改设备上的应用程序包(已签名)。您可以记录到您的应用的文档文件夹或tmp目录。

1

我有完全相同的问题。事实证明,我正在碰撞一条错误生成的路径声明。 AVAudioRecorder初始化正常 - 没有发生 - 但是iOS根本不允许我写信给它。为什么?检查出来:

/* The following code will fail - why? It fails because it is trying to write some path that is NOT accessible by 
    this app. 
NSString *recordedAudioPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 
           objectAtIndex:0] 
           stringByAppendingString:@"recorded.caf"]; 
*/ 
// Correction: 
NSString *recordedAudioPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 
           objectAtIndex:0]; 

recordedAudioPath = [recordedAudioPath stringByAppendingPathComponent:@"recorded.caf"]; 
NSURL *recordURL = [NSURL fileURLWithPath:recordedAudioPath]; 

这使现在完全合理。感谢您指引正确的方向。

+0

他们看起来完全一样... – Gu1234 2011-05-08 21:22:41

+0

区别在于如果需要,路径组件被添加。它本质上增加了一个/ recorded.caf 之前,如果他用以前的 '不正确' 的代码会工作: 的NSString * recordedAudioPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0] stringByAppendingString:@“/ recorded.caf“]; – 2014-09-17 14:37:50

17

如果在你的代码的任何地方,你有这样的设置:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 

将其更改为:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 

设置错误AVAudioSession类别将导致录音/播放失败。

1

此外,请确保您的应用程序被允许访问的麦克风(即iOS的设置,然后找到你的应用程序,它应该有“麦克风”,你需要切换到“开”)

+0

什么?我一直试图调试两天的这个问题不可能如此简单,当然这个设备具有麦克风权限,请参阅... 哦,就是这样。设备/模拟器完全是一只红鲱鱼。 iOS至少应该记录一个错误,而不是默默地失败。 谢谢,有时它是如此基本的东西。 – 2018-01-12 19:49:51