-->

Independent Volume control of AVAudioPlayer and MP

2020-03-30 01:35发布

问题:

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.