iphone - 录制音频并保存到文档目录后无法使用 AVAudioPlayer 播放音频文件

标签 iphone ios ios6 avaudioplayer avaudiorecorder

我正在开发录制音频并播放存储在文档目录中的录制文件的应用程序。

这是我的代码:

记录按钮方法。

(IBAction)stopButtonPressed:(id)sender{

     UIButton *button = (UIButton *)sender;

     if (button.selected) { // Play recorded file.

     self.stopButton.selected = NO;

     NSError *error;

    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioRecorder.url     error:&error];

    self.audioPlayer.delegate = self;

    self.audioPlayer.volume = 1.0;

    [self.audioPlayer prepareToPlay];

    [self.audioPlayer play];

    }

    else{ // Stop recording.

    self.stopButton.selected = YES;

    if (recordTimer) {

    [recordTimer invalidate];

    }

    [self.audioPlayer stop];

    self.recordButton.selected = NO;

    [audioRecorder stop];

    self.readyLabel.text = @"READY";

    [self insertNewRecording];
        }
    }

最佳答案

我认为您使用的是 ARC,在这种情况下,您需要保留 AVAudioPlayer,因为它会自动设置为 nil。在您的头文件中输入以下内容`

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

希望这对你有用, 干杯吉姆

关于iphone - 录制音频并保存到文档目录后无法使用 AVAudioPlayer 播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15968636/

相关文章:

iphone - 核心动画围绕任意点旋转图层

iphone - 单击按钮时的poptorootviewcontroller

iphone - 便利构造函数会增加应用程序的大小吗?

ios - 检测 UICollectionView 的上拉或拖动

ios - didSelectRowAtIndexPath 在带有侧边菜单的 ios6 中不起作用

ios - UIActivityViewController。如何定制?

iphone - 如何更改 Cocos2d MenuItem 的颜色?

ios - 将项目从 Collection View 拖放到其他 View IOS 中

ios - 我如何有效地为 100 多个 View Controller 使用多个 Storyboard?

ios - 在 Objective-C 中声明没有确定大小的数组