使用MPRemoteCommandCenter时遇到麻烦

时间:2017-05-28 17:51:07

标签: ios swift avplayer avplayerviewcontroller avplayeritem

MPRemoteCommandCenter有两个问题:

1)当我改变歌曲时,遥控器删除上一首歌曲图像,制作默认歌曲图像,制作下一首歌曲图像。我不想看默认图像。我花了一些时间来寻找解决方案。

2)当AVPlayer是实时流式传输声音时,遥控器变为非活动的圆圈箭头(圆圈箭头为15号,这意味着什么?)。

这是播放声音的代码:

public func playAVSound(trackName : String) -> String {
    let path = _findPath(trackName: trackName);
    if (path == "") {
        return "getting url"
    }
    if (self.AVPlayerVC.player == nil) {
        print("Init remote control events...")
        UIApplication.shared.beginReceivingRemoteControlEvents()
        let commandCenter = MPRemoteCommandCenter.shared()
        commandCenter.nextTrackCommand.isEnabled = true
        commandCenter.nextTrackCommand.addTarget(self, action:#selector(self.next))
        commandCenter.previousTrackCommand.isEnabled = true
        commandCenter.previousTrackCommand.addTarget(self, action:#selector(self.previous))
    }
    if (self.AVPlayerVC.player != nil && self.play_info.trackName == trackName) {
        if (self.play_info.paused!) {
            self.AVPlayerVC.player?.play()
            self.updatePlayInfo(number: Global.PlayList.find_by_trackName(trackName: trackName), trackName: trackName, path: path, paused: false)
            return "continue"
        } else {
            self.AVPlayerVC.player?.pause()
            self.play_info.paused = true
            return "pause"
        }
    }
    let nsurl = NSURL(string: path)
    if let url = nsurl {
        print("Play AV from : \(url)")
        let item = AVPlayerItem(url: url as URL)
        if (self.AVPlayerVC.player?.currentItem == nil) {
            self.AvPlayer = AVPlayer(playerItem: item)
            self.AVPlayerVC.player = self.AvPlayer
            self.AVPlayerVC.player?.automaticallyWaitsToMinimizeStalling = false
        } else {
            self.AvPlayer?.replaceCurrentItem(with: item)
        }
        NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying), name: .AVPlayerItemDidPlayToEndTime, object: item)
        self.AVPlayerVC.player?.play()
        self.updatePlayInfo(number: Global.PlayList.find_by_trackName(trackName: trackName), trackName: trackName, path: path, paused: false)
        return "playing"
    } else {
        print ("Incorrect nsurl")
    }
    return "error"
}

private func _findPath (trackName: String) -> String {
    let n = Global.PlayList.find_by_trackName(trackName: trackName)
    var path = ""
    if (n >= 0) {
        let p_item = Global.PlayList.PlaylistItems[n];
        if (!p_item.fromData!) {
            if (p_item.playing_url == nil) {
                NetLib.makeTrackUrl(trackName: trackName, closure: self.playAVSound)
            } else {
                path = p_item.playing_url!
            }
        } else {
            path = NetLib.makePath(filename: p_item.filename!)
        }
    } else {
        print("Error find_path: \(trackName) was not found in playlist")
    }
    return path
}

这是代码,改变了歌曲

    @objc private func previous() -> MPRemoteCommandHandlerStatus{
    var n = self.play_info.number! - 1;
    if (self.play_info.number == 0) {
        n = Global.PlayList.size() - 1;
    }
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
    _ = self.playAVSound(trackName: Global.PlayList.PlaylistItems[n].trackName!)
    print("Previous song \(Global.PlayList.PlaylistItems[n].trackName!)")
    return .success
}

first song

after click on next

second song

remote controls while live streaming

1 个答案:

答案 0 :(得分:1)

我在音频流时解决控制问题。我不是从主线程播放音频,因此远程控件在该线程中工作不正确。