1

大家好,我正在尝试快速制作 MPRemoteCommandCenter,它工作正常,但如果我从应用程序播放 AVPlayer,MPRemoteCommand 播放按钮不会改变,图片会告诉你我的意思,非常抱歉拼写错误,因为英语不是我的第一语言。

MPRemoteCommand 屏幕

应用程序屏幕

这是我的代码

func setupNowPlaying(播放器:AVPlayer?,文章:文章){

if let player = player{


nowPlayingInfo[MPMediaItemPropertyTitle] = article.title

nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = article.sections[index].title

nowPlayingInfo[MPMediaItemPropertyArtist] = article.name
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player.currentTime().seconds
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = player.currentItem!.asset.duration.seconds
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate

nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds((player.currentItem?.asset.duration)!)

nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 1

MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}

}

func setupRemoteTransportControls() {

commandCenter.playCommand.addTarget { [unowned self] event in


    self.delegate.notificationPlayerHandle()

    return .success

}


commandCenter.pauseCommand.addTarget { [unowned self] event in


    self.delegate.notificationPlayerHandle()


    return .success

}


commandCenter.nextTrackCommand.addTarget { [unowned self] event in


    self.getNextSong(article: self.article)
    NotificationCenter.default.post(name: NSNotification.Name.init("Hidden"), object: self)

    return .success

}


commandCenter.previousTrackCommand.addTarget { [unowned self] event in

    self.getPreviousSong(article: self.article)
    NotificationCenter.default.post(name: NSNotification.Name.init("Hidden"), object: self)

    return .success


}

commandCenter.changePlaybackPositionCommand.addTarget(handler: { (event) in




    return MPRemoteCommandHandlerStatus.success
})

}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

    if object is AVPlayer{

        switch player!.timeControlStatus {

        case .waitingToPlayAtSpecifiedRate , .paused:

            nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds((player?.currentItem?.asset.duration)!)
            nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 0
            MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo

            break

        case .playing:
            nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds((player?.currentItem?.asset.duration)!)
            nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 1
            MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo

            break

        default:
            break
        }


    }

}

  func notificationPlayerHandle() {

    guard let player = player else {

               Toast.showAlert(viewController: self, text: NSLocalizedString("لا يوجد ملف صوتي", comment: ""))
               return

           }


    if player.timeControlStatus == .playing{

        player.pause()
        playerButton.setImage(UIImage(named: "player-btn-ic"), for: .normal)
        progressView.setProgress(value: CGFloat(horizontalSlider!.value), animated: true, duration: CMTimeGetSeconds((player.currentItem?.asset.duration)!), completion: nil)

        homePlayerSatute = false

        NotificationCenter.default.post(name: NSNotification.Name.init("Hidden"), object: self)

    }else if player.timeControlStatus == .paused{


        player.play()

        playerButton.setImage(UIImage(named: "ic_bg_pause_circle_yellow")!, for: .normal)

        self.formatAudioDuration()

        self.currentTimeLabel.isHidden = false

        progressView.setProgress(value: 1, animated: true, duration: CMTimeGetSeconds((player.currentItem?.asset.duration)!), completion: nil)

        homePlayerSatute = true

        NotificationCenter.default.post(name: NSNotification.Name.init("Hidden"), object: self)
    }
}

}

协议 NotificationPlayerDelegate{

func notificationPlayerHandle()

}

4

0 回答 0