Upgrade to Pro — share decks privately, control downloads, hide ads and more …

abema_devcon.pdf

Kyohei Ito
October 15, 2016
7.1k

 abema_devcon.pdf

Kyohei Ito

October 15, 2016
Tweet

Transcript

  1. AVAssetͷplayableͳͲ͸loadValuesAsynchronouslyͰΠϕ ϯτΛड͚͔ͯΒॲཧΛߦ͏ // Load the asset's "playable" key asset.loadValuesAsynchronously(forKeys: ["playable"])

    { var error: NSError? = nil let status = asset.statusOfValue(forKey: "playable", error: &error) switch status { case .loaded: // Sucessfully loaded, continue processing case .failed: // Examine NSError pointer to determine failure case .cancelled: // Loading cancelled default: // Handle all other cases } } ※AppleͷAPI ReferenceΑΓൈਮ
  2. RxͰloadValuesAsynchronouslyΛߪಡ͢Δ extension Reactive where Base: AVAsset { public var playable:

    Observable<Bool> { let keys = ["playable"] return Observable.create { observer in self.base.loadValuesAsynchronously(forKeys: keys) { observer.onNext(self.base.isPlayable) } return Disposables.create() } } }
  3. AVPlayer͸playableͷΠϕϯτΛड͚͔ͯΒ࡞੒͍ͯ͠Δ asset.rx.playable .filter { $0 == true } .observeOn(MainScheduler.instance) .subscribe(onNext:

    { [weak self] _ in self?.player = AVPlayer(playerItem: playerItem) }) .addDisposableTo(disposeBag)
  4. AVPlayerLayerͷreadyForDisplayΛߪಡ͢Δ extension Reactive where Base: AVPlayerLayer { var ready: Observable<Void>

    { let key = "readyForDisplay" return observe(AnyObject.self, key, options: [], retainSelf: false) .map { _ in Void() } .filter { self.base.isReadyForDisplay == true } } }
  5. AVAssetͱAVPlayerItem͸αϒεϨουͰ࡞੒͍ͯ͠Δ class VideoPlayer { init(url: URL) { let asset =

    AVURLAsset(url: url) playerItem = AVPlayerItem(asset: asset) } } rx.streamUrl .observeOn(scheduler) // αϒεϨουͰॲཧ .subscribe(onNext: { [weak self] url in self?.videoPlayer = VideoPlayer(url: videoURL) }) .addDisposableTo(disposeBag)
  6. playable͕trueʹͳͬͨΒϝΠϯεϨουͰAVPlayerΛ࡞੒ ͍ͯ͠Δ videoPlayer.asset.rx.playable .filter { $0 == true } .observeOn(MainScheduler.instance)

    // ϝΠϯεϨουͰॲཧ .subscribe(onNext: { [weak self] _ in self?.player = AVPlayer(playerItem: playerItem) }) .addDisposableTo(disposeBag)
  7. AVPlayerͱAVPlayerLayerͷഁغ͸αϒεϨουͰߦ͍ͬͯΔ deinit { let layer = playerLayer let qos =

    DispatchQoS.QoSClass.background DispatchQueue.global(qos: qos).async { layer.videoPlayer = nil layer.player = nil } }