在watchOS中设置MPRemoteCommandCenter现在正在使用WKAudioFilePlayer播放

时间:2018-10-15 10:05:09

标签: ios apple-watch watch-os watch-os-5

我正在开发一个可在Apple Watch中播放音频的应用程序;在应用程序内部一切正常。 我正在尝试在“正在播放”中设置MPRemoteCommandCenter,将下一个/上一个曲目更改为 skipForward / skipBackward ,并为暂停添加处理程序命令。

“正在播放”命令中没有任何变化,不会触发处理程序。

以下代码段中:

播放方法

var player: WKAudioFilePlayer!
    @IBAction func play() {


        let avSession = AVAudioSession.sharedInstance()

        try! avSession.setCategory(.playback, mode: .default, policy: .longForm, options: [])

        let url = Bundle.main.url(forResource: "sample", withExtension: "mp3")!
        let item = WKAudioFilePlayerItem(asset: WKAudioFileAsset(url: url))
        player = WKAudioFilePlayer(playerItem: item)


        do {
            try avSession.setActive(true, options: .notifyOthersOnDeactivation)
            player.play()

        } catch {
            print("Error")
        }

    }

遥控器设置方法:

func setupRemoteControls() {
        // Get the shared MPRemoteCommandCenter
        let commandCenter = MPRemoteCommandCenter.shared()

        commandCenter.skipForwardCommand.preferredIntervals = [NSNumber(value: 15)]
        commandCenter.skipForwardCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
            // skip forward
            return .success
        }
        commandCenter.skipForwardCommand.isEnabled = true


        commandCenter.skipBackwardCommand.preferredIntervals = [NSNumber(value: 15)]
        commandCenter.skipBackwardCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
            // skip backword
            return .success
        }
        commandCenter.skipBackwardCommand.isEnabled = true


        // Add handler for play Command
        commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
            self.player.play()
            return .success
        }

        // Add handler for Pause Command
        commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
            self.player.pause()
            return .success
        }

    }
  • 我正在使用self.setupRemoteControls()方法调用awake。我也尝试将设置移至ExtensionDelegate -> applicationDidFinishLaunching

我使用的Apple参考资料:

https://developer.apple.com/videos/play/wwdc2018/504/

https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/MediaPlaybackGuide/Contents/Resources/en.lproj/RefiningTheUserExperience/RefiningTheUserExperience.html


:: UPDATE ::

我发现使用AVAudioPlayer代替WKAudioFilePlayerMPRemoteCommandCenter的设置工作正常。

我不使用本地音频文件的问题!..我使用仅适用于WKAudioFilePlayer的wowza流式传输'm3u8'文件。 而且,如果我尝试使用AVAudioPlayer进行流式传输,则会收到此错误

  

该操作无法完成。 (OSStatus错误2003334207。)

所以我现在遇到的问题是在仍然使用MPRemoteCommandCenter的同时配置WKAudioFilePlayer,或者找到一种使用AVAudioPlayer进行流传输的方法

0 个答案:

没有答案