1
我正在使用MPMusicPlayerController.systemMusicPlayer()
來製作一個音樂播放器來學習Swift,除了一個小問題外,一切都很順利;每當我的應用程序播放音樂,然後切換到音樂應用程序並通過多任務處理終止它時,音樂會按預期停止,但當切換到該應用程序時,我的應用程序會崩潰。 我在想這可能是由於我沒有爲音樂終止時發生的情況設置條件,而且我似乎無法在Apple Docs中找到任何有助於實際工作的條件。我試過如何檢測股票音樂應用程序是否已終止?
if (musicPlayer.playbackState == MPMusicPlaybackState.Interrupted) || (musicPlayer.playbackState == MPMusicPlaybackState.Stopped)
無濟於事。任何人都可以提供一些見解,爲什麼會發生這種情況和/或解決方案? 這裏是我的代碼示例:
override func viewDidLoad() {
super.viewDidLoad()
var notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "handlePlayingItemChanged", name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification, object: musicPlayer)
notificationCenter.addObserver(self, selector: "handlePlayState", name: MPMusicPlayerControllerPlaybackStateDidChangeNotification, object: musicPlayer)
musicPlayer.beginGeneratingPlaybackNotifications()
if (musicPlayer.playbackState == MPMusicPlaybackState.Playing) {
playButton.setImage(pause, forState: UIControlState.Normal)
setupCurrentMediaItem()
handleShuffleAndReplayMode()
self.isPlay = true
} else if (musicPlayer.playbackState == MPMusicPlaybackState.Paused) {
playButton.setImage(play, forState: UIControlState.Normal)
self.isPlay = false
} else if (musicPlayer.playbackState == MPMusicPlaybackState.Interrupted) || (musicPlayer.playbackState == MPMusicPlaybackState.Stopped) {
playButton.setImage(play, forState: UIControlState.Normal)
self.isPlay = false
}
}
使用'applicationMusicPlayer'是否有所作爲? 'MPMusicPlayerController'不是很好,爲什麼不用'AVPlayer'播放音樂? –