我正在开发具有录制音频和播放功能的应用程序,该录制文件存储在文档目录中。
这是我的代码:
记录按钮方法。
(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];
}
}
答案 0 :(得分:2)
我认为你正在使用ARC,在这种情况下你需要保留AVAudioPlayer,因为它自动设置为nil。在头文件中输入以下内容`
@property(非原子,强)AVAudioPlayer * audioPlayer;
希望这适合你, 干杯吉姆
答案 1 :(得分:1)
path = [[NSBundle mainBundle] pathForResource:@"forest" ofType:@"mp3"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:path];
NSError *audioError = nil;
av = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
如果一切顺利,也许这与你代码中的audioRecorder.url有关?
希望这会给你一些想法。