![](/img/trans.png)
[英]xcode: How to save audio file after recording audio using AVFoundation
[英]After recording audio and save to document directory unable to play audio file with AVAudioPlayer
我正在開發具有錄制音頻並播放存儲在文檔目錄中的錄制文件的應用程序。
這是我的代碼:
記錄按鈕的方法。
(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。 在頭文件中輸入以下內容`
@屬性(非原子的,強的)AVAudioPlayer * audioPlayer;
希望這對您有用,干杯吉姆
嗯..我不確定。 但是作為測試,也許您可以嘗試通過指定文件名來播放文件? 例如,您有一個forest.mp3播放,並且代碼的關鍵部分可以像這樣:
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有關?
希望這能給您一些想法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.