簡體   English   中英

當iPhone進入睡眠狀態時,AVAudioPlayer停止播放音頻

[英]AVAudioPlayer stops playing the audio when iPhone goes to sleep

只要我的應用程序運行,我就需要一直播放背景聲音。 我試圖將AVAudioPlayer用於此目的。 但是,一旦iPhone進入睡眠模式,它就會停止播放聲音。

你能讓我知道如何解決這個問題嗎?

謝謝

您可能需要設置音頻會話類別。 來自“iPhone應用程序編程指南”

類別是標識應用程序的一組音頻行為的鍵。 通過設置類別,您可以向iPhone OS指示您的音頻意圖,例如當屏幕鎖定時您的音頻是否應該繼續。

有關每個類別的詳細信息,請參閱“ 音頻會話類別

在您的特定示例中,您可以嘗試以下操作。 它應該開箱即用。

// Registers this class as the delegate of the audio session.
[[AVAudioSession sharedInstance] setDelegate: self];    
// Allow the app sound to continue to play when the screen is locked.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

那么這個解決方案也可能有所幫助

    [[AVAudioSession sharedInstance] setDelegate: self]; 
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
[[AVAudioSession sharedInstance] setActive: YES error: nil];

參考http://www.mindyourcode.com/ios/iphone/how-to-play-audio-in-iphone-sleep-mode/

暫無
暫無

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

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