How can I control Background Music of other Apps in Swift?

十年热恋 提交于 2020-01-15 10:25:08

问题


First sorry for my English because I'm French.

I work on a connected object which send data to my app to start, pause, start the next music or the precedent and to augment the volume.

My application has to execute these actions on any music emitted by the iPhone. For example, if I listen to music in Spotify or Deezer, the app has to control it.

I don't know how I can do this in Swift. Just I really think that work with AVAudioSession.


回答1:


I'm not sure what you're asking about, but I think this is what you mean. This will control the music playing on you device and the volume. Here's an article about it : http://www.justindoan.com/tutorials/2016/7/31/mpmedia-tutorial-control-music-playback-in-ios-with-swift

So here's basically what you have to do. Firstly import MediaPlayer.

    import MediaPlayer

then you define the mediaController and prepare to play

    let mediaController = MPMusicPlayerController.systemMusicPlayer()
    mediaController.prepareToPlay()

and to control the media.

    //play
    mediaController.play()

    //pause
    mediaController.pause()

    //go to previous item
    mediaController.skipToPreviousItem()

    //go to start of the item
    mediaController.skipToBeginning()

    //and to go to the next item
    mediaController.skipToNextItem()

Hope this helps.



来源:https://stackoverflow.com/questions/38161900/how-can-i-control-background-music-of-other-apps-in-swift

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