1

我在https://developer.apple.com/streaming/fps/上按照 Apple 的 Fairplay Streaming 示例代码使用 Fairplay 实现,尽管我尝试仅选择与 Online Fairplay Streaming 相关的部分,而不是持久性/离线播放。在下面的代码中,没有 Fairplay 的视频正常播放/暂停/搜索,但是当我播放受 Fairplay 保护的视频时,只有视频轨道正常运行。

暂停播放不会停止音频播放,改变音轨不会停止前一个音轨,所以两者一起播放,也许搜索也不起作用。

除了下面的这个助手类之外,我还有AssetLoaderDelegate来自AssetPlaybackManagerApple 的 FairPlay Streaming Server SDK 客户端示例代码https://developer.apple.com/streaming/fps/并且我已经更新了代码来处理我们的 DRM 密钥提供程序的 SPC/CKC。

我是否错过了实现代码的一些重要部分来处理 FPS Streaming 的音频?你能指出我正确的方向吗?非常感谢。

class PlayHelper {

    static let shared = PlayHelper()

    fileprivate var playerViewController: PlayerViewController?

    init() {

        AssetPlaybackManager.sharedManager.delegate = self
    }

    // Play video without DRM
    func playVideo(from urlString: String, at context: UIViewController) {

        guard let videoURL = URL(string: urlString) else {
            Log.error("Video URL can't be created from string: \(urlString)")
            return }

        let player = AVPlayer(url: videoURL)

        let playerViewController = PlayerViewController()
        playerViewController.player = player

        context.present(playerViewController, animated: true) {
            playerViewController.player?.play()
        }
    }

    // Play FPS video
    func playFpsVideo(with asset: AVURLAsset, at context: UIViewController) {

        // Cleanup, should be done when playerViewController is actually dismissed
        if self.playerViewController != nil {
            // The view reappeared as a results of dismissing an AVPlayerViewController.
            // Perform cleanup.
            AssetPlaybackManager.sharedManager.setAssetForPlayback(nil)
            self.playerViewController?.player = nil
            self.playerViewController = nil
        }

        let item = AVPlayerItem(asset: asset)

        let player = AVPlayer(playerItem: item)

        // Customize player
        player.appliesMediaSelectionCriteriaAutomatically = true

        let playerViewController = PlayerViewController()
        playerViewController.player = player

        self.playerViewController = playerViewController

        context.present(playerViewController, animated: true) {
            playerViewController.player?.play()
        }
    }

    // Stop video
    func stop() {

        // Cleanup, should be done when playerViewController is dismissed
        if self.playerViewController != nil {

            // Results of dismissing an AVPlayerViewController, perform cleanup
            AssetPlaybackManager.sharedManager.setAssetForPlayback(nil)
            self.playerViewController?.player = nil
            self.playerViewController = nil
        }
    }
}


// MARK: - Extend `PlayHelper` to conform to the `AssetPlaybackDelegate` protocol

extension PlayHelper: AssetPlaybackDelegate {

    func streamPlaybackManager(_ streamPlaybackManager: AssetPlaybackManager, playerReadyToPlay player: AVPlayer) {

        player.play()
    }

    func streamPlaybackManager(_ streamPlaybackManager: AssetPlaybackManager, playerCurrentItemDidChange player: AVPlayer) {

        guard let playerViewController = playerViewController, player.currentItem != nil else { return }

        playerViewController.player = player
    }
}

如果需要AssetLoaderDelegate,我还可以提供代码。AssetPlaybackManager

4

1 回答 1

1

我的错。我play()在上面的代码中调用了两次...... Grrr .. 一次当PlayerViewController完成的演示和第二次在回调AssetPlaybackDelegate中由 KVO in 触发时AssetPlaybackManager。这样,播放器控件停止播放视频,但很可能第二个(音频)流仍在播放。我删除了play()inplayerReadyToPlay回调,现在 Player 中的所有控件都按预期工作。我可以暂停、恢复、搜索、更改音轨。

于 2017-03-31T23:41:45.517 回答