AVAudioPlayer仅播放自定义音频文件一秒钟

时间:2012-10-07 21:59:23

标签: ios ios6 avaudioplayer

在我的应用程序中,我使用AVAudioRecorder对象允许用户录制声音文件,当用户完成录制后,他们可以通过AVAudioPlayer播放声音。我遇到的问题是当用户尝试播放音频文件时,它只播放一秒钟。更奇怪的是,这个代码在iOS 5上运行得很完美,但自从升级到iOS 6以来,这个问题已经开始发生。

我已经检查了通过iTunes文件共享创建的文件,我可以在桌面上播放整个声音文件。

下面我粘贴了加载音频播放器的操作文件中的代码。从我可以告诉

- (void)playRecordingBtnClk:(id)sender
{
    NSLog(@"Play btn clk");

    if (!isRecording) {

        // disable all buttons
        [_recordButton disableButton];
        [_stopButton disableButton];
        [_playButton disableButton];
        [_saveButton disableButton];

        // create the player and play the file from the beginning
        if (audioPlayer) {
            [audioPlayer release];
            audioPlayer = nil;
        }

        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioRecorder.url error:&error];
        audioPlayer.delegate = self;
        audioPlayer.currentTime = 0.0;
        [audioPlayer prepareToPlay];

        if (error) {
            NSLog(@"Error Playing Audio File: %@", [error debugDescription]);
            return;
        }

        [audioPlayer play];
        [self startTicker];
    }
}

看来

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
一秒后调用

方法,这就是音频播放器停止的原因。任何人都有任何想法在这里发生了什么?

1 个答案:

答案 0 :(得分:3)

对于iOS 6.0,再添加两行:

soundPlayer.currentTime = soundPlayer.duration + soundPlayer.duration;
soundPlayer.numberOfLoops = 1;

不是一个完美的解决方案。我们可能需要等待iOS 6.0.1 / 6.1更新。

更多信息,请visit this,有人评论说,这可能是CDAudioManager的问题。