abema_devcon.pdf

4d40d82cc3c676e8a67ffc2a473bf423?s=47 Kyohei Ito
October 15, 2016
6k

 abema_devcon.pdf

4d40d82cc3c676e8a67ffc2a473bf423?s=128

Kyohei Ito

October 15, 2016
Tweet

Transcript

  1. AbemaTV Developer Conference 2016 2016/10/15

  2. About Me ҏ౻ɹګฏ Github : KyoheiG3 Twitter : @KyoheiG3

  3. ΠϯλϥΫςΟϒͳUIͷ ͨΊͷAVPlayerϕετ ϓϥΫςΟε

  4. iOSΞϓϦͷ֓ཁ • 3ͭͷಈըΛ࠶ੜ͍ͯ͠ΔϑΟʔυ • ൪૊ද

  5. ։ൃ౰ॳͷiOSΞϓϦ

  6. ݱࡏͷiOSΞϓϦ

  7. ԣදࣔΞϓϦͷ೉͍͠ͱ͜Ζ • βοϐϯάதͰ΋ࠨӈͷಈըΛ࠶ੜ͢Δඞཁ͕͋Δ

  8. ԣදࣔΞϓϦͷ೉͍͠ͱ͜Ζ • ӅΕΔಈըͷഁغ͕ॏ͍

  9. ԣදࣔΞϓϦͷ೉͍͠ͱ͜Ζ • ݱΕΔಈըͷ४උ͕ॏ͍

  10. ղܾ͢΂͘ߦͬͨ͜ͱ • ࠶ੜॲཧͷ࠷దԽ • ϚϧνεϨουॲཧ

  11. ࠶ੜॲཧͷ࠷దԽ

  12. ಈըΛ࠶ੜ͢Δ·Ͱͷ͓͞Β͍

  13. AVAssetͷ஫ҙ఺ Ξηοτʢ·ͨ͸τϥοΫʣΛॳظԽͯ͠΋ɺͦͷΞΠςϜʹ ؔͯ͠औಘ͍ͨ͢͠΂ͯͷ৘ใ͕͙͢ʹಘΒΕΔͱ͸ݶΓ·ͤ Μɻ৔߹ʹΑͬͯ͸ɺΞΠςϜͷ࠶ੜ࣌ؒ΋ܭࢉ͢Δඞཁ͕͋ Γ·͢ʢͨͱ͑͹ɺMP3ϑΝΠϧʹ͸ཁ໿৘ใؚ͕·Ε͍ͯͳ ͍৔߹͕͋Γ·͢ʣɻ1 1 AVFoundationϓϩάϥϛϯάΨΠυΑΓҾ༻

  14. AVAssetͷ஫ҙ఺ AVAsset͸৔߹ʹΑͬͯ͸࣮ߦεϨουΛϒϩοΫͯ͠Ͱ΋ί ϯςϯπͷಡΈࠐΈΛߦ͓͏ͱ͢Δ৔߹͕͋Δ!! AVAssetͱ͸ɺϝσΟΞσʔλʹؔ͢Δ৘ใ͕·ͱ·ͬͨΦϒδ ΣΫτɻ

  15. 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ΑΓൈਮ
  16. 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() } } }
  17. AVPlayer͸playableͷΠϕϯτΛड͚͔ͯΒ࡞੒͍ͯ͠Δ asset.rx.playable .filter { $0 == true } .observeOn(MainScheduler.instance) .subscribe(onNext:

    { [weak self] _ in self?.player = AVPlayer(playerItem: playerItem) }) .addDisposableTo(disposeBag)
  18. 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 } } }
  19. readyForDisplay͕trueʹͳͬͨΒදࣔॲཧΛߦ͍ͬͯΔ rx.ready .observeOn(MainScheduler.instance) .subscribe(onNext: { [weak self] in self?.hidden =

    false }) .addDisposableTo(disposeBag)
  20. ಈըͷಡΈࠐΈ։࢝࣌͸௿BitRateʹઃఆ • ௿BitRateͰɺ࠶ੜ։࢝·Ͱͷ࣌ؒΛ୹ॖ • ಈըͷղ૾౓͸௿͍΄͏͕CPUͷෛՙ͸௿͍ͷͰɺ྆࿬ͷಈ ըͷղ૾౓͸௿͘ઃఆ

  21. replaceCurrentItem(with:)͸࢖Θͳ͍ • ϝΠϯεϨουͰ࢖͏ʹ͸ύϑΥʔϚϯε͕ΠϚΠν • ॳظԽͯ͠࢖͏৔߹ͱ࠶ར༻ͯ͠࢖͏৔߹ͷ2ຊཱͯʹͳͬͯ ͠·͏ • αϒεϨουͰద੾ʹॲཧͰ͖ͳ͍ʢ˞ޙड़

  22. ϚϧνεϨουॲཧ

  23. ϚϧνεϨουॲཧͷ஫ҙ఺ UI͸ϝΠϯεϨουͰॲཧ͢Δ!! ϝΠϯεϨουҎ֎ͰUIΛૢ࡞ͨ͠৔߹ͷڍಈ͸อূ͞Ε͍ͯ ͳ͍ɻʢৗࣝͰ͢Ͷ

  24. ϚϧνεϨουॲཧͷ஫ҙ఺ ҋӢʹαϒεϨουʹ౤͛ͳ͍!! αϒεϨουͷॲཧ͸ɺεΫϩʔϧͳͲUIૢ࡞ʹର͢ΔϝΠϯ εϨουॲཧͷ༏ઌ౓ʹෛ͚ͯ͠·͍ɺಈըࢹௌͷUXΛ௿Լ͞ ͤΔ৔߹͕͋Δɻ

  25. 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)
  26. 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)
  27. AVPlayerͱAVPlayerLayerͷഁغ͸αϒεϨουͰߦ͍ͬͯΔ deinit { let layer = playerLayer let qos =

    DispatchQoS.QoSClass.background DispatchQueue.global(qos: qos).async { layer.videoPlayer = nil layer.player = nil } }
  28. AVPlayerͱAVPlayerLayer͸ηοτͰഁغ͢Δ AVPlayer͚ͩΛαϒεϨουͰഁغ͢Δͱɺͦͷ͏ͪಈը͕࠶ ੜ͞Εͳ͘ͳΔࣄ͕͋ͬͨ!! AVPlayerͷഁغͷॲཧ͕ޙճ͠ʹ͞ΕͯɺAVPlayer࡞੒਺ͷ্ ݶʹୡ͍ͯ͠ΔΑ͏ͳڍಈɻ

  29. replaceCurrentItem(with:)͸࢖Θͳ͍ • AVPlayerͷ࡞੒͸ϝΠϯεϨουͷํ͕ྑͦ͞͏ • AVPlayerͷഁغ͸αϒεϨουͷํ͕ྑͦ͞͏ • replaceCurrentItem(with:)͸྆ํͷئ͍Λ׎͑ͯ͘ΕΔ ͜ͱ͸ແ͔ͬͨ

  30. ·ͱΊ

  31. ·ͱΊ • AVAssetͷ஋͸ϩʔυ͕׬͔ྃͯ͠Βࢀর͢Δ • ద੾ͳBitRateʹઃఆ͢Δ • αϒεϨουͰద੾ʹॲཧ͢Δʢಛʹഁغʣ • replaceCurrentItem(with:)͸࢖Θͳ͍

  32. Thanks!

  33. ͓·͚ • CIʹ͍ͭͯ • Swift3ʹ͍ͭͯ