AVAudioPlayer with MPMusicPlayerController

不问归期 提交于 2019-12-03 13:21:10

问题


I have music playing within my application using a MPMusicPlayerController, using iPodMusicPlayer (also tried applicationMusicPlayer). When I play a sound using AVAudioPlayer my music from my MPMusicPlayerController will stop. Is there a way to have the MPMusicPlayerController and the AVAudioPlayer play sounds simultaneously?


回答1:


Hey, found the solution on the web.

Write these 2 lines somewhere, I did it in application:didFinishLaunchingWithOptions

// Set sounds not to stop music
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

Must admit I didn't delve into the details, but it works...

Good luck!

Oded.




回答2:


Oded Ben Dov's solution does the job, but you might not want to change your session category. For example, if you want your audio to keep playing while the screen is locked, you need the Playback category.

In that case, you can apply this category override instead:

UInt32 mixWithOthers = YES;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(mixWithOthers), &mixWithOthers);


来源:https://stackoverflow.com/questions/3224269/avaudioplayer-with-mpmusicplayercontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!