簡體   English   中英

AVAudioPlayer無法播放臨時文件路徑中的聲音

[英]AVAudioPlayer not playing sound from temp file path

我已將錄音保存到我的Temp目錄中 ,可以使用iExplorer進行驗證

文件路徑結構為TempFolder/UserID/SoundName001.wav 。我保存到數據庫的位為UserID/SoundName001.wav

這是我用來播放文件的代碼

@property (nonatomic, strong) AVAudioPlayer *player;

-(void) playSound: (NSIndexPath*)recordingIndex {
    NSArray *recording = [recordingArray objectAtIndex:recordingIndex.item-1];
    NSString *filePath = [NSString stringWithFormat:@"%@%@",NSTemporaryDirectory(),[recording objectAtIndex:2]];
    NSURL *recorderURL = [[NSURL alloc] initFileURLWithPath: filePath];
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorderURL error:nil];
    [self.player prepareToPlay];
    [self.player setMeteringEnabled:YES];
    [self.player setDelegate:self];
    [self.player play];
}

我設置一個斷點並recorderURL file:///private/var/mobile/Containers/Data/Application/0883D0DC-B1FD-4D06-A7BB-EBD9EEF5D607/tmp/1/Dog001.wav返回以下file:///private/var/mobile/Containers/Data/Application/0883D0DC-B1FD-4D06-A7BB-EBD9EEF5D607/tmp/1/Dog001.wav

照片證據

當我調用此方法時,什么也沒發生,沒有崩潰,沒有聲音……我在這里缺少明顯的東西嗎?

您是否檢查過設備上的靜音開關? 我認為如果打開靜音開關,AVPlayer不會播放聲音。 嘗試放入以下代碼行:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

在您的viewDidLoad或在[self.player play];之前[self.player play]; 並查看音頻是否播放。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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