所以我剛剛注意到,在我的iPod Touch上,當我的應用程序觸發使用AVAudioPlayer播放的短wav文件時,音樂會暫停。這是正常的嗎?iPhone AVAudioPlayer停止背景音樂
我找不到任何這方面的參考,它似乎會在某處注意到。在播放我的聲音時,有沒有辦法讓音樂繼續播放?
所以我剛剛注意到,在我的iPod Touch上,當我的應用程序觸發使用AVAudioPlayer播放的短wav文件時,音樂會暫停。這是正常的嗎?iPhone AVAudioPlayer停止背景音樂
我找不到任何這方面的參考,它似乎會在某處注意到。在播放我的聲音時,有沒有辦法讓音樂繼續播放?
注意:The AudioSession API has been completely deprecated in iOS 7.0
你不能在同一時間運行AVAudioPlayer和iPod播放器或MPMusicPlayer或MPMoviePlayer,沒有做更多的工作。如果您想要簡單,請使用Audio Toolbox的系統聲音。
如果你想要做一些額外的工作,那麼你應該看看Audio Sessions:
kAudioSessionCategory_UserInterfaceSoundEffects
對於聲音效果,如觸摸 反饋,爆炸等。相當於
kAudioSessionCategory_AmbientSound
類別,您應該使用 來代替。kAudioSessionCategory_UserInterfaceSoundEffects
類別在iPhone OS 3.0中已棄用。
kAudioSessionCategory_AmbientSound
對於 長時間的聲音,如雨,汽車 發動機噪音等等。它也是 用於「play along」風格的應用程序, 這樣的虛擬鋼琴,用戶通過iPod音頻播放 。當您使用此類別時,來自 的內置應用程序(例如iPod的 )的音頻會與您的音頻混合使用。當Ring/Silent 開關設置爲無聲或 屏幕鎖定時,您的 音頻會消失。
基本上,每個應用程序都分配了一個音頻會話,它被建模爲單例類,您可以在應用程序啓動時將其獲取並設置參數。我固定同樣的問題的方法是通過一個單一的代碼線放置在applicationDidFinishLaunching
:
目的-C:
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryAmbient error:nil];
夫特2/3:
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
根據根據您的要求,播放系統聲音可能不夠,請查看會話選項,例如MixWithOthers
struct AVAudioSessionCategoryOptions : OptionSetType {
init(rawValue rawValue: UInt)
static var MixWithOthers: AVAudioSessionCategoryOptions { get }
static var DuckOthers: AVAudioSessionCategoryOptions { get }
static var AllowBluetooth: AVAudioSessionCategoryOptions { get }
static var DefaultToSpeaker: AVAudioSessionCategoryOptions { get }
static var InterruptSpokenAudioAndMixWithOthers: AVAudioSessionCategoryOptions { get }
}
當您啓動會議,通過MixWithOthers
選項,也許DuckOthers
(將下降iPod的音量,當你播放聲音)是另一種選擇。
肯尼請改變你接受的答案。0 :) – 2016-06-29 06:37:16