簡體   English   中英

錄制音頻並保存到文檔目錄后,無法使用AVAudioPlayer播放音頻文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM