【问题标题】:Use player controls on iOS lock screen for Remote Control application使用 iOS 锁定屏幕上的播放器控件进行远程控制应用程序
【发布时间】:2020-06-19 10:38:40
【问题描述】:

我正在尝试在 iOS 锁定屏幕上为我的远程控制应用程序实现播放器控件,该应用程序控制 PC 上的音乐播放器。 所以基本上我需要控制 iOS 设备之外的播放。 我在功能(音频)中启用了背景模式并尝试设置 MPRemoteCommandCenter 锁定屏幕控件,但未显示它们。这是代码:

class RemoteCommandHandler: NSObject {
    static let shared = RemoteCommandHandler()

    private let player = AVPlayer()
    private let audioSession = AVAudioSession.sharedInstance()
    private let commandCenter = MPRemoteCommandCenter.shared()

    func initRemote() {
        //UIApplication.shared.beginReceivingRemoteControlEvents()

        do {
            if #available(iOS 10.0, *) {
                try audioSession.setCategory(.playback, mode: .default, options: [])
            } else {
                // Fallback on earlier versions
                try audioSession.setCategory(.playback)
            }
        } catch {
            NSLog("\(error)")
        }

        do {
            try audioSession.setActive(true)
            NSLog("AVSession is active")
        } catch {
            NSLog("\(error)")
        }

        setupRemoteTransportControls()
        setupNowPlaying()
    }

    func setupRemoteTransportControls() {
        commandCenter.togglePlayPauseCommand.isEnabled = true
        commandCenter.togglePlayPauseCommand.addTarget(self, action: #selector(controlPlayPause))
    }

    func setupNowPlaying() {
        var nowPlayingInfo = [String : Any]()
        nowPlayingInfo[MPMediaItemPropertyTitle] = NowPlayingInfo.getValue(.Title)

        if let image = UIImage(named: "DemoArtwork") {
            nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: image)
        }
        nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = TimeInterval(exactly: 10)
        nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = NowPlayingInfo.getDurationMs() / 1000
        nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 1.0

        MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
    }
}

甚至可以做我想做的事吗?我做错了什么?

【问题讨论】:

    标签: ios swift avplayer avaudiosession mpremotecommandcenter


    【解决方案1】:

    看起来在锁定屏幕上进行控制的唯一可能方法是从您的应用程序开始播放真实音频文件。 我做了一个在应用启动时播放非常短的静音文件的解决方法,现在我可以访问这些控件了。

    请记住,应启用后台模式(音频),以便在您的应用不在屏幕上时查看控件。

    另一个限制是音量控制硬连线到设备音量,因此您不能单独更改应用音量。

    这是启用控件的最少代码:

    import MediaPlayer
    import AVFoundation
    
    class RemoteCommandHandler: NSObject {
        static let shared = RemoteCommandHandler()
    
        private var player: AVPlayer!
        private var playerItem: AVPlayerItem!
        private let audioSession = AVAudioSession.sharedInstance()
        private let commandCenter = MPRemoteCommandCenter.shared()
    
        func initRemote() {
            let sound = Bundle.main.path(forResource: "SilenceAAC", ofType: "m4a")
            playerItem = AVPlayerItem(url: URL(fileURLWithPath: sound!))
            player = AVPlayer(playerItem: playerItem)
    
            do {
                if #available(iOS 10.0, *) {
                    try audioSession.setCategory(.playback, mode: .default, options: [])
                } else {
                    // Fallback on earlier versions
                    try audioSession.setCategory(.playback)
                }
    
                try audioSession.setActive(true)
                NSLog("AVSession is active - \(audioSession)")
            } catch {
                NSLog("\(error)")
            }
    
            setupNowPlaying()
            setupRemoteTransportControls()
    
            player.play()
        }
    
        func setupRemoteTransportControls() {
            commandCenter.togglePlayPauseCommand.addTarget(self, action: #selector(controlPlayPause))
        }
    
        func setupNowPlaying() {
            var nowPlayingInfo = [String : Any]()
            nowPlayingInfo[MPMediaItemPropertyTitle] = NowPlayingInfo.getValue(.Title)
    
            MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多