Command Center 媒体按钮不与 App 的媒体状态 Swift Unity 同步

问题描述 投票:0回答:0

我已经使用

swift framework
为我的
Unity
项目创建了一个媒体播放器插件,媒体播放器在应用程序上运行良好。

但是,当应用程序的媒体状态为

paused
时,命令中心媒体按钮不会更新它仍然在
pause icon
上并且与
playing
相同按钮不会更新。

我已经启动了 AVAudioSession 共享实例并将类别设置为

.playback
并将
.setActive
设置为
true
.

let audioSession = AVAudioSession
                .sharedInstance()
            try audioSession
                .setCategory(
                    .playback,
                    mode: .default
                )
            print("Playback OK")
            try audioSession.setActive(true)
            print("Session is Active")

我还初始化了

command center
,用于在命令中心单击按钮时处理
play
pause
事件。

self.commandCenter.playCommand.isEnabled = true
        self.commandCenter.playCommand.addTarget { [weak self] (event) -> MPRemoteCommandHandlerStatus in
            self?.play()
            return .success
        }
        
        // Add handler for Pause Command
        self.commandCenter.pauseCommand.isEnabled = true
        self.commandCenter.pauseCommand.addTarget { [weak self] (event) -> MPRemoteCommandHandlerStatus in
            self?.pause()
            return .success
        }

这是处理

pause

的代码
self.nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(self.player.currentTime())
            self.nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = CMTimeGetSeconds(self.player.currentItem!.duration);
            self.nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 0
            self.nowPlayingInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = 0
            
            MPNowPlayingInfoCenter.default().nowPlayingInfo = self.nowPlayingInfo
self.player.pause()

这是处理

play

的代码
self.nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(self.player.currentTime())
            self.nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = CMTimeGetSeconds(self.player.currentItem!.duration);
            self.nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 1
            self.nowPlayingInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = 1
            MPNowPlayingInfoCenter.default().nowPlayingInfo = self.nowPlayingInfo;
self.player.play()

我期待的是,当我在应用程序中单击暂停时,

command center's
按钮应该是
play icon
如果我点击播放
command center's
按钮应该是
pause icon
,但现在没有任何效果。

swift unity3d avplayer mpremotecommandcenter
© www.soinside.com 2019 - 2024. All rights reserved.