How to stop and resume background audio from iPhone app?

后端 未结 2 1647
醉酒成梦
醉酒成梦 2020-12-06 03:41

Am working in Messaging based iPhone app. I have added Beep sound to receive message from someone else. I am playing beep sound using AVAudioPlayer. My problem

2条回答
  •  时光说笑
    2020-12-06 04:16

    You cannot do this for all applications. For applications that use the iPod player in their applications:

    Assuming your application is in foreground. You can do this:

    MPMusicPlayerController *mp = [MPMusicPlayerController iPodMusicPlayer];        
    [mp stop]; // or [mp pause]
    

    This needs the MediaPlayer.framework and also #import .

    Read more at MPMusicPlayerController Reference.


    For doing it through Audio Player Sessions you can use the property kAudioSessionCategory_SoloAmbientSound. Reference here.

    This is similar to AVAudioSessionCategorySoloAmbient defined in AVAudioSession class.

    Quoting from the documentation:

    When you use this category, audio from other apps is silenced.

提交回复
热议问题