一旦屏幕在实际设备中被锁定,LockScreen Player就不会出现。 [英] LockScreen Player is not appearing once the screen is locked in actual device.

查看:1207
本文介绍了一旦屏幕在实际设备中被锁定,LockScreen Player就不会出现。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是swift的新手,我有一个AVPlayer在特定的屏幕上播放得很好。我的目标是在锁屏模式下播放相同的音频。到目前为止,这是我所做的和代码如下。出于某种原因,我根本没有看到锁定屏幕上的播放器小部件。我在这里做错了什么。

I'm new to swift and I have a AVPlayer which is playing fine on a particular screen. My objective is to play the same audio on lock-screen mode. So far this is what I have done and the code as bellow. for some reason I don't see the player widget on lock-screen mood at all. What am I doing wrong here.

class MyAudiosViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,AVAudioPlayerDelegate {


@IBAction func playButtonPressed(_ sender: Any) {
        if(currentRowSelected != nil ){
            if AudioPlayer.isPlaying() {
                AudioPlayer.pause()
                playButton.setImage(UIImage(named: "play_button"), for: UIControlState.normal)
            } else {
                AudioPlayer.play()
                playButton.setImage(UIImage(named: "pause_button"), for: UIControlState.normal)
            }

            setUpBackgroundMode()

        }else{
            "Track is not selected from list"
        }

    }

}


 extension MyAudiosViewController {

    override func remoteControlReceived(with event: UIEvent?) {
        if let receivedEvent = event {
            if (receivedEvent.type == .remoteControl) {
                switch receivedEvent.subtype {
                case .remoteControlTogglePlayPause:
                   playButtonPressed(AudioPlayer.isPlaying())
                case .remoteControlPlay:
                playButtonPressed(AudioPlayer.player?.play())
                case .remoteControlPause:
                   playButtonPressed(AudioPlayer.player?.pause())
                case .remoteControlNextTrack:
                     print("next pressed")
                case .remoteControlPreviousTrack:
                     print("previous pressed")
                default:
                    print("received sub type \(receivedEvent.subtype) Ignoring")
                }
            }
        }
    }

    func setUpBackgroundMode() {
        if  let positionSong = AudioPlayer.getPlayingIndex() {
            let song = songDetailsArray[positionSong]
            let songData = song.audioDetails

        MPNowPlayingInfoCenter.default().nowPlayingInfo = [
            MPMediaItemPropertyTitle: songData?.title ?? "",
            MPMediaItemPropertyArtist: songData?.healerName ?? "",
            MPMediaItemPropertyPlaybackDuration: 
            self.player?.currentItem?.asset.duration
        ]
            UIApplication.shared.beginReceivingRemoteControlEvents()

        becomeFirstResponder()
       }
      }
  }


推荐答案

试试这个并查看演示

在你的playerviewcontroller中声明这个变量

declare this variable in your playerviewcontroller

var audioSession = AVAudioSession.sharedInstance()

in viewDidiLoad 添加此代码

    try! self.audioSession.setCategory(AVAudioSessionCategoryPlayback)
    try! self.audioSession.setActive(true)

    UIApplication.shared.beginReceivingRemoteControlEvents()
    self.becomeFirstResponder()

将数据传递给此func或您的

pass data to this func or yours

var nowPlayingInfoCenter = MPNowPlayingInfoCenter.default()
var remoCommandCenter = MPRemoteCommandCenter.shared()
func updateNowPlayingInfo(trackName:String,artistName:String,img:UIImage) {

    var art = MPMediaItemArtwork(image: img)
    if #available(iOS 10.0, *) {
        art = MPMediaItemArtwork(boundsSize: CGSize(width: 200, height: 200)) { (size) -> UIImage in
            return img
        }
    }

    nowPlayingInfoCenter.nowPlayingInfo = [MPMediaItemPropertyTitle: trackName,
                                           MPMediaItemPropertyArtist: artistName,
                                           MPMediaItemPropertyArtwork : art]

    remoCommandCenter.seekForwardCommand.isEnabled = false
    remoCommandCenter.seekBackwardCommand.isEnabled = false
    remoCommandCenter.previousTrackCommand.isEnabled = false
    remoCommandCenter.nextTrackCommand.isEnabled = false
    remoCommandCenter.togglePlayPauseCommand.isEnabled = false
}

当你的负载调用上面的函数新歌。

call above function when your load new song.

  func loadNewSong() {
      //your audio player code.
      updateNowPlayingInfo(trackName:"just Like that", artistName:"LeoJam",img:UIImage(string:"img")!)
  }

这篇关于一旦屏幕在实际设备中被锁定,LockScreen Player就不会出现。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆