在实时流播放器不起作用的情况下反应Native Swift Bridge

时间:2018-09-20 14:05:45

标签: ios swift react-native

嗨,我需要一些帮助。我有一个使用Ionic 1完成的实时流和按需播放APP,并且播放器是我使用Swift Code制作的CordovaPlugin。我正在将该应用程序迁移到React Native,并且我桥接了该插件。除直播外,其他一切正常。两种应用程序中都使用相同的代码,但是React Native程序向我抛出“操作无法完成”错误。我认为这可能是一种配置问题,但是我的Swift知识有限

谢谢!

这是视频播放器的代码。我正在使用苹果公司的FairPlay实现。重复一遍,此代码作为Cordova插件在我的Ionic应用程序中运行。但是,在React Native中,只有VOD在起作用。没有直播。

import UIKit
import AVFoundation
import AVKit

class MyPlayerViewController: UIViewController {
  var playerController = AVPlayerViewController()

  private var pendingContentKeyRequests = [String: Asset]()

  override func viewDidLoad() {
    UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
    try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])

    self.addChildViewController(playerController)
    self.view.addSubview(playerController.view)
    playerController.view.frame = self.view.frame

    AssetPlaybackManager.sharedManager.delegate = self

    setVideo(url: "http://rt-esp.secure.footprint.net/1102-inadv-qidx-1k_v3.m3u8", drm: DRM(header_name: "", header_body: "", license_server: ""), certificate: "")
  }

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.landscape
  }

  func downloadFairPlayApplicationCertificate(certificate: String?) {
    let downloader = FairPlayApplicationCertificateDownloader(certificate_url: certificate)
    downloader.maybeDownloadApplicationCertificate(completion: { (success, errorMessage, errorDetailObject) -> Void in ()})
  }

  func playVideo(){
    let asset = AssetListManager.sharedManager.asset(at: 0)
    AssetPlaybackManager.sharedManager.setAssetForPlayback(asset)
  }

  func handleAssetListManagerDidLoadNotification(_: Notification) {
    DispatchQueue.main.async {
    }
  }

  func handleAssetLoaderDelegateDidPersistContentKeyNotification(notification: Notification) {
    guard let assetName = notification.userInfo?[Asset.Keys.name] as? String, let asset = self.pendingContentKeyRequests.removeValue(forKey: assetName) else {
      return
    }
    AssetPersistenceManager.sharedManager.downloadStream(for: asset)
  }

  func setVideo(url: String, drm: DRM, certificate: String) {
    AssetListManager.sharedManager.removeAll()

    downloadFairPlayApplicationCertificate(certificate: certificate)
    AssetListManager.sharedManager.addAsset(url: url, header_name: drm.header_name, header_body: drm.header_body, license: drm.license_server)

    NotificationCenter.default.addObserver(self, selector: #selector(handleAssetListManagerDidLoadNotification(_:)), name: AssetListManager.didLoadNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(handleAssetLoaderDelegateDidPersistContentKeyNotification(notification:)), name: AssetLoaderDelegate.didPersistContentKeyNotification, object: nil)

    self.playVideo()
  }
}

extension MyPlayerViewController: AssetPlaybackDelegate {
  func streamPlaybackManager(_ streamPlaybackManager: AssetPlaybackManager, playerReadyToPlay player: AVPlayer) {
    NSLog("---------- PLAY START ----------")
    self.playerController.player = player
    player.play()
  }

  func streamPlaybackManager(_ streamPlaybackManager: AssetPlaybackManager, playerCurrentItemDidChange player: AVPlayer) {
    NSLog("---------- OK ----------")
  }

  func streamPlaybackManager(_ streamPlaybackManager: AssetPlaybackManager, playerError error: String) {
    NSLog("---------- ERROR ----------")
    NSLog(error)
  }
}

0 个答案:

没有答案
相关问题