Independent Volume control of AVAudioPlayer and MPMusicPlayerController in iOS App.

独自空忆成欢 提交于 2019-12-19 10:33:10

问题


Within my application, I am playing downloaded audio using an AVAudioPlayer, while simultaneously playing audio from the user's iPod music library with an MPMusicPlayerController.

I need to be able to adjust the Volume of the AVAudioPlayer instance so that it's louder than the audio coming from the MPMusicPlayerController.

The problem is, when I adjust the Volume property of the AVAudioPlayer, it also adjusts the volume of the MPMusicPlayerController.

Is there any solution which would allow me to independently control the volume of these two players?

If not, is there another technique I should use to do this? Any help is appreciated.


回答1:


Take a look at the documentation for AVAudioSession. For example, in the AVAudioSession Programming Guide, says the following:

"Finally, you can enhance a category to automatically lower the volume of other audio when your audio is playing. This could be used, for example, in an exercise application. Say the user is exercising along to their iPod when your application wants to overlay a verbal message—for instance, “You’ve been rowing for 10 minutes.” To ensure that the message from your application is intelligible, apply the kAudioSessionProperty_OtherMixableAudioShouldDuck property to your audio session. When ducking takes place, all other audio on the device—apart from phone audio—lowers in volume."

I think it might solve your problem. The documentation on initializing an AVAudioSession and setting its categories and properties is pretty clear and easy to follow; you should have no trouble.



来源:https://stackoverflow.com/questions/6867112/independent-volume-control-of-avaudioplayer-and-mpmusicplayercontroller-in-ios-a

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