问题
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