Попробуй быстро сделать MPRemoteCommand - PullRequest
1 голос
/ 05 мая 2020

Привет всем, я пытаюсь сделать MPRemoteCommandCenter быстро, он работает нормально, но если я играю в AVPlayer из приложения, кнопка воспроизведения MPRemoteCommand не меняется, изображения покажут вам, что я имею в виду, и очень извиняюсь за орфографические ошибки потому что английский sh язык не является моим первым языком.

Экран MPRemoteCommand

Экран приложения

Это мой код

fun c setupNowPlaying (player: AVPlayer?, Статья: Article) {

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
}

}

fun c 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()

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...