以下は、MPRemoteCommandCenter の共有インスタンスを初期化するコードです。
UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
MPRemoteCommandCenter.sharedCommandCenter().playCommand.enabled = true
MPRemoteCommandCenter.sharedCommandCenter().playCommand.addTarget(self, action: #selector(HostPlayer.playCommand))
MPRemoteCommandCenter.sharedCommandCenter().pauseCommand.enabled = true
MPRemoteCommandCenter.sharedCommandCenter().pauseCommand.addTarget(self, action: #selector(HostPlayer.pauseCommand))
MPRemoteCommandCenter.sharedCommandCenter().nextTrackCommand.enabled = true
MPRemoteCommandCenter.sharedCommandCenter().nextTrackCommand.addTarget(self, action: #selector(HostPlayer.nextTrackCommand))
MPRemoteCommandCenter.sharedCommandCenter().previousTrackCommand.enabled = true
MPRemoteCommandCenter.sharedCommandCenter().previousTrackCommand.addTarget(self, action: #selector(HostPlayer.previousTrackCommand))
HostPlayer.pauseCommand などのハンドラーで実行される操作は、外部アクセサリ信号に応じてサウンド トラックを再生/一時停止/前/次へ移動することです。そのようなハンドラー関数の 1 つを次に示します。
func pauseCommand () -> MPRemoteCommandHandlerStatus {
if player.playbackState == MPMusicPlaybackState.Playing {
player.pause()
return MPRemoteCommandHandlerStatus.Success
}
return MPRemoteCommandHandlerStatus.CommandFailed
}
ここで、プレーヤーは MPMusicPlayerController.applicationMusicPlayer() のインスタンスです。
問題は、特定のボタンを押して外部アクセサリ (AirPods など) から再生/一時停止しても、関連するハンドラが実行されないか、プログラム制御がそこに到達しないことです。この問題の解決策またはそのためのその他の回避策をいただければ幸いです。