为什么AVAudioPlayer无法初始化? OSStatus错误1685348671?

时间:2014-07-04 22:53:18

标签: ios avaudioplayer avaudiorecorder avaudiosession

我正在视频控制器中录制音频(语音备忘录)。这有效。只要用户不关闭视图控制器,我就可以播放录制的音频。

但是,如果用户取消视图控制器并再次打开视图控制器,则初始化AVAudioPlayer会因OSStatus错误1685348671而失败。

我还尝试使用NSData初始化播放器: NSData *data = [[NSData alloc] initWithContentsOfURL:_outputFileURL]; _player = [[AVAudioPlayer alloc] initWithData:data error:&error2];,但这不起作用

我试过.caf,跟着每个教程,但不知怎的,我不能让它工作...... 这个错误似乎是指无效文件左右,但我不确定。但是,如果我在管理器中下载应用程序并在我的Mac上打开它,我可以播放它,因此文件本身没有损坏。

这是我的记录方法:

- (IBAction)recordPauseTapped:(id)sender {
    // Stop the audio player before recording
    if (_player.playing) {
        [_player stop];
    }
    if (!_recorder.recording) {
        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setActive:YES error:nil];

        // Start recording
        [_recorder record];
        [recordPauseButton setTitle:@"Pause" forState:UIControlStateNormal];

    } else {

    // Pause recording
        [_recorder pause];
        [recordPauseButton setTitle:@"Aufnahme" forState:UIControlStateNormal];
    }

    [stopButton setEnabled:YES];
    [playButton setEnabled:NO];
 }

录制工作正常,文件存在于Documents目录中。

这是我的播放方法,如果文件是在之前录制的,并且没有用户离开视图控制器,则该方法有效:

- (IBAction)playTapped:(id)sender {
    if (!_recorder.recording){

        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setActive:YES error:nil];

        NSError *error;
        BOOL success = [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
        if(!success)
        {
            NSLog(@"error doing outputaudioportoverride - %@", [error localizedDescription]);
        }

        NSError *error2;
        NSLog(@"_outputFileURL: %@", _outputFileURL);

        _player = [[AVAudioPlayer alloc] initWithContentsOfURL:_outputFileURL error:&error2];
        if (error2) {
            NSLog(@"error initialising player - %@", [error2 localizedDescription]);
         }
        [_player setDelegate:self];
        [_player play];
    }
}

这是我的viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;

    [stopButton setEnabled:NO];

    // Set the audio file
    if ([_audioFileName isEqualToString:@""]) {
        NSString *guid = [[NSUUID new] UUIDString];
        _audioFileName = [NSString  stringWithFormat:@"audio-%@.m4a", guid];
        [playButton setEnabled:NO];
    } else {
        [playButton setEnabled:YES];
    }
    NSLog(@"_audioFileName: %@", _audioFileName);
    NSArray *pathComponents = [NSArray arrayWithObjects:
                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                           _audioFileName,
                           nil];
    _outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

    // Setup audio session
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [session setActive:YES error:nil];

    // Define the recorder setting
    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

    // Initiate and prepare the recorder
    _recorder = [[AVAudioRecorder alloc] initWithURL:_outputFileURL settings:recordSetting error:nil];
    _recorder.delegate = self;
    _recorder.meteringEnabled = YES;
    [_recorder prepareToRecord];
}

1 个答案:

答案 0 :(得分:1)

你的问题是你有[_recorder prepareToRecord];进入viewLoad,意思是 每次加载视图控制器时都会被覆盖。 如果你不希望这种行为,你应该删除这一行,并把它放在开头 以recordPauseTapped为例。 这应该可以解决你的问题。