【问题标题】:AVAudioRecorder Won't Record On DeviceAVAudioRecorder 不会在设备上录制
【发布时间】:2010-04-20 15:16:43
【问题描述】:

这是我的方法:

-(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。有人知道为什么会这样吗?

【问题讨论】:

    标签: iphone avaudiorecorder avfoundation


    【解决方案1】:

    如果您的代码中的任何地方都有此设置:

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

    改成:

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

    设置错误的 AVAudioSession 类别会导致录制/播放失败。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          我遇到了完全相同的问题。事实证明,我遇到了错误生成的路径语句。 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];
          

          现在这完全有道理。感谢您引导我朝着正确的方向前进。

          【讨论】:

          • 不同的是,如果需要,添加路径组件。它本质上在recorded.caf之前添加了一个/如果他使用之前的'不正确'代码会起作用: NSString *recordedAudioPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@"/recorded.caf"] ;
          【解决方案5】:

          另外,请确保您的应用被允许访问麦克风(即在 iOS 设置中,向下滚动并找到您的应用,它应该有“麦克风”,您需要将其切换为“开启”)

          【讨论】:

          • 什么?这个问题我试了两天调试,不可能这么简单,当然这个设备有麦克风权限,看……哦,原来如此。设备/模拟器完全是一个红鲱鱼。 iOS 至少应该记录一个错误,而不是静默失败。谢谢,有时它是如此基本的东西。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-29
          • 2010-11-03
          相关资源
          最近更新 更多