簡體   English   中英

iOS在模擬器上成功播放音頻,但在設備上失敗

[英]iOS play audio success on simulator but fail on device

我使用錄音機在文檔目錄中記錄和創建文件,然后使用音頻播放器進行播放。 我可以在模擬器和設備上錄制它,但不能僅在設備上播放它。

請注意1.我不會播放文件中的聲音,而是從NSData中播放(我將音樂存儲到數據庫中,然后將其檢索以播放)。2.在模擬器上工作正常,但在設備上失敗。

這是我播放音頻的代碼

- (void)playAudioWithData:(NSData *)data {

    [self stopPlayingAudio];
    [self stopRecordingAudio];


    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [audioSession setActive:YES error:nil];


    self.audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];

    [self.audioPlayer play];

}

這是錄制的設置:

    NSDictionary *audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithFloat:44100],AVSampleRateKey,
                                   [NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey,
                                   [NSNumber numberWithInt: 1],AVNumberOfChannelsKey,
                                   [NSNumber numberWithInt:AVAudioQualityMedium],AVEncoderAudioQualityKey,nil];

而不是使用AVAudioSessionCategoryPlayAndRecord試用音頻會話設置為AVAudioSessionCategoryRecord錄制時再到AVAudioSessionCategoryPlayback當你想播放剛剛錄制的內容。

錄制和播放音頻時,我在應用程序中遇到了同樣的事情,這解決了該問題。

暫無
暫無

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

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