我正在開發一個小遊戲,我正在使用AVAudioPlayer來播放遊戲的背景音樂。問題是,如果用戶在應用程序抽屜中關閉應用程序而未關閉應用程序(因此它位於後臺),則音樂不會停止。我該如何改變這一點?謝謝你的幫助!當應用程序轉到背景時停止AVAudioPlayer音樂
0
A
回答
0
在視圖控制器,你處理你AVAudioPlayer在viewDidLoad中添加此
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
然後添加的2種方法:
-(void) appWillResignActive:(NSNotification*)note{
NSLog(@"App is going to the background");
// This is where you stop the music
}
-(void) appWillTerminate:(NSNotification*)note{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];
NSLog(@"App will terminate");
}
0
這裏簡單的回答是,你要配置的AVAudioSession使用類別AVAudioSessionCategorySoloAmbient。
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
這在你激活會話當然。
相關問題
- 1. iOS AVAudioPlayer讓其他應用程序的背景音樂停止
- 2. iPhone AVAudioPlayer停止背景音樂
- 3. 停止背景音樂
- 4. 使用AVAudioPlayer啓動/停止背景音樂按鈕
- 5. 如何在用戶看不到應用程序時停止背景音樂
- 6. 如何停止音樂背景,同時使用音樂服務
- 7. 當用戶關閉應用程序時停止背景音樂,而不是在用戶更改活動時停止背景音樂
- 8. 返回(關注)到Adobe AIR應用程序(iOS)時停止背景音樂
- 9. 將應用程序移動到背景時音樂不會停止
- 10. 當返回到應用程序時停止iTunes/Spotify音樂
- 11. IOS:應用程序音樂背景
- 12. SKAction playSoundFileNamed停止背景音樂
- 13. 背景音樂自動停止
- 14. 停止/只播放背景音樂
- 15. avaudioplayer在背景播放音樂
- 16. 當應用程序轉到背景時播放音頻
- 17. android:當使用MediaPlayer播放背景音樂時,音效過早停止
- 18. 背景音樂停止在iPhone應用程序3/4運行後
- 19. 停止音樂應用播放音樂
- 20. VideoView停止其他應用的背景音樂
- 21. 當應用程序變爲背景時,服務已停止
- 22. 停止的MediaPlayer當其他應用程序播放音樂
- 23. 啓動應用程序時,iPod音樂應該停止播放
- 24. 當背景視頻停止時音頻停止
- 25. 當使用AVPLayer播放視頻時,它會停止背景音樂ios
- 26. 如何在用戶退出應用程序時停止音樂
- 27. 暫停用戶背景音樂
- 28. 背景音樂
- 29. 背景音樂
- 30. 將音頻剪切爲背景音樂 - PhoneGap應用程序
「resign active」與進入後臺不一樣。當應用程序進入後臺時,使用'UIApplicationDidEnterBackground'而不是'UIApplicationWillResignActive'來通知。 – rmaddy 2015-04-17 16:22:22
在「應用程序將終止」通知中刪除觀察者沒有任何意義。該應用正在終止。這種清理在那個階段毫無意義。 – rmaddy 2015-04-17 16:23:38
確實如此,但我喜歡這種對稱性,而這一點練習可確保我永遠不會忘記刪除通知。 – ElectrikSheep 2015-06-18 09:42:44