AVAudioPlayer不播放用AVAudioRecorder制作的录音

时间:2022-05-24 18:18:48

This may be a really simple problem but I've made a recording using AVAudioRecorder, then I stopped the recorder. After stopping the AVAudioRecorder, I press another button to play the recording but it doesn't play. The file exists, I can play it on my computer, even the code knows it exists, there is no error, but refuses to play. What can be the issue?

这可能是一个非常简单的问题但是我使用AVAudioRecorder进行了录音,然后我停止了录音机。停止AVAudioRecorder后,我按另一个按钮播放录音但不播放。该文件存在,我可以在我的计算机上播放,即使代码知道它存在,没有错误,但拒绝播放。可能是什么问题?

NSError *error = nil;
    if ([[NSFileManager defaultManager] fileExistsAtPath:[self.recorder.url path]]) {
        self.recordingPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.recorder.url error:&error];
        self.recordingPlayer.delegate = self;

        if (error) {
            NSLog(@"error: %@", [error localizedDescription]);
        } else {
            [self.recordingPlayer play];
        }


    } else {
        NSLog(@"Recording file doesn't exist");
    }

EDIT: just tried it on my device and it works fine, plays the recording. It just doesn't work on iOS simulator

编辑:只是在我的设备上尝试它,它工作正常,播放录音。它只是在iOS模拟器上不起作用

2 个解决方案

#1


1  

The problem was with my record settings, I had the number of AVNumberOfChannelsKey set to 2, for some reason iOS simulator didn't like this, but iPhone was fine with it. Either way, my recording shouldn't have 2 channels in the first place, so good thing I spotted this.

问题在于我的记录设置,我将AVNumberOfChannelsKey的数量设置为2,由于某些原因,iOS模拟器不喜欢这个,但iPhone很好用。无论哪种方式,我的录音首先不应该有2个频道,所以我发现这个好事。

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

#2


0  

Try this link's code. In this you can record an audio and retrieve from document folder.

试试这个链接的代码。在此,您可以录制音频并从文档文件夹中检索。

Record audio file and save locally on iPhone

录制音频文件并在iPhone上本地保存

#1


1  

The problem was with my record settings, I had the number of AVNumberOfChannelsKey set to 2, for some reason iOS simulator didn't like this, but iPhone was fine with it. Either way, my recording shouldn't have 2 channels in the first place, so good thing I spotted this.

问题在于我的记录设置,我将AVNumberOfChannelsKey的数量设置为2,由于某些原因,iOS模拟器不喜欢这个,但iPhone很好用。无论哪种方式,我的录音首先不应该有2个频道,所以我发现这个好事。

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

#2


0  

Try this link's code. In this you can record an audio and retrieve from document folder.

试试这个链接的代码。在此,您可以录制音频并从文档文件夹中检索。

Record audio file and save locally on iPhone

录制音频文件并在iPhone上本地保存