Share
Embed
I spend a year building Picasso for Video and here is it
Google Slide version (so GIF can play): https://docs.google.com/presentation/d/1Yhrnm22lkl6YoFmu20UvbAUAa5MgtOdul-tMle1jEf4/edit?usp=sharing
To Android Tokyo Meetup attendees: this version has a few slide added (the part about why we need you-know-what) and fix a few text.
Nam Nguyen Hoai
July 23, 2019
Transcript
51 slides
-
I’ve spent a year building Picasso for Video and here
is it Tokyo Android Meetup・2019/07/23 Nam Nguyen (eneim) -
Picasso? A powerful image downloading and caching library for Android
https://square.github.io/picasso/ -
Picasso for Video? A powerful image downloading and caching video
playback library for Android -
MediaPlayer, ExoPlayer, ffmpeg, etc exist. Why do we need another
Video playback library? -
YouTube clone sprint 1 - List of thumbnail - Click
item thumbnail → open Player - One Video in one screen at one time! -
Sprint 1 approach - 1 Activity for list & 1
Activity for player - Click → Parcelize Video information and start PlayerActivity - MediaPlayer or ExoPlayer for playback -
YouTube clone sprint 2 - List of thumbnail Videos -
Fully visible Video on top: plays automatically - Click an item → open Player - Many Videos in one screen at one time! - At most one to play -
Sprint 2 approach - 1 Activity for list & 1
Activity for player - List: RecyclerView with OnScrollListener - onScrolled: - LinearLayoutManager’s findFirstCompletelyVisibleItemPosition() - Start and Pause item properly - MediaPlayer or ExoPlayer for playback -
YouTube clone sprint 3 - Single player UX ⇆ Orientation:
- Portrait: Player + Video detail - Landscape: Fullscreen player - Background playback (to replicate YouTube Premium) - Playback continuity - No reloading/re-buffering -
Sprint 3 approach - Manifest flag: configChanges + Orientation Detector
and/or onConfigurationChanged + if/else 祭り - Background playback: foreground Service - No re-creation → No re-buffering -
Sprint 3 challenges - Manifest flag: configChanges ← How many
flags you will need? - Background playback: foreground Service ← How? - Foreground Service should be alive even if no Activity is -
‘Picasso for Video’ is a powerful Video playback library for
Android Named it Kohii -
// fragment_main.xml // <NestedScrollView id="scrollView"> // <LinearLayoutCompat orientation="vertical"> // <PlayerView
id="playerView"></PlayerView> // <TextView></TextView> // </LinearLayoutCompat> // </NestedScrollView> override fun onActivityCreated(state: Bundle?) { super.onActivityCreated(state) kohii = Kohii[this@MyFragment].also { it.register(this, this.scrollView) } kohii.setUp(videoUrl).bind(playerView) } Easy to start -
// fragment_main.xml // <NestedScrollView id="scrollView"> // <LinearLayoutCompat orientation="vertical"> // <PlayerView
id="firstPlayerView"></PlayerView> // <TextView></TextView> // <PlayerView id="secondPlayerView"></PlayerView> // <TextView></TextView> // </LinearLayoutCompat> // </NestedScrollView> override fun onActivityCreated(state: Bundle?) { super.onActivityCreated(state) kohii = Kohii[this@MyFragment].also { it.register(this, this.scrollView) } kohii.setUp(videoUrl).bind(firstPlayerView) kohii.setUp(otherVideoUrl).bind(secondPlayerView) } Many videos scenario is supported out of the box -
Summary: what Kohii offers? - Works out of the box
- Implement Video playback as quick as possible - Single Video or many Videos - Safe and resource friendly - Cache and reuse playback resource - All cached resources are torn down at reasonable timing - Flexible & Extensible - Powerful abstraction & default impl using ExoPlayer - DIY on top of Kohii’s abstraction -
Application FragmentActivity Fragment Fragment RecyclerView RecyclerView Kohii (BOSS) PlaybackManagerGroup PlaybackManager
TargetHost Target(s) PlayerView -
Kohii PlaybackManagerGroup PlaybackManager_A PlaybackManager_B TargetHost_A TargetHost_B Target_A0 Target_A2 Target_A1 Target_B0
Target_B2 Target_B1 Target_B3 Target_B5 Target_4 Target’s attach/detach/layout events PlaybackManager requests update TargetHost’s scroll/layout event PlaybackManagerGroup talks to all PlaybackManagers: collect all Targets to play and to pause -
Kohii PlaybackManagerGroup PlaybackManager_A PlaybackManager_B TargetHost_A TargetHost_B Target_A0 Target_A2 Target_A1 Target_B0
Target_B2 Target_B1 Target_B3 Target_B5 Target_4 Each PlaybackManager talks to its TargetHosts: select the Target to play, others will be paused. PlaybackManagerGroup then decide the final Target to play and pause all others React to user interaction. Using Handler/Messenger to prevent aggressive update requests. -
TEXT Text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text TEXT Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text Video: not playable -
TEXT Text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text TEXT Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text Video: playable Player provider Pool Creator Player #1 Give me a Player Here you are -
TEXT Text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text Video: playable Player provider Pool Creator Player #1 TEXT Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text TEXT Text text text text text text text text text text text text text text text text text text text text I’m done, thanks! -
TEXT Text text text text text text text text text
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text Video: playable Player provider Pool Creator Player #1 Give me a Player Here you are TEXT Text text text text text text text text text text text text text text text text text text text text text text text text text -
Player provider Pool Creator TEXT Text text text text text
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text TEXT Text text text text text text text text text text text text text text text text text text text text text text text text text Lifecycle.Event.ON_DESTROY Player #1 I’m done, thanks! -
By passing the same resource around, we can switch the
Player without reloading the content -
How ExoPlayer works ExoPlayer PlayerView MediaSource // playerView: PlayerView instance
// player: ExoPlayer instance // mediaSource: MediaSource instance playerView.setPlayer(player) player.prepare(videoSource) player.playWhenReady = true // to play, or false to pause -
One Playable (= one ExoPlayer) Video in List Overlay Player
Background Player -
A: Kohii is extensible. i.e You can support other playback
API on top of Kohii’s abstraction -
Abstraction: Playable, Playback, Pool, etc Default impl using ExoPlayer DIY*
using your own playback API (eg: androidx Media2, YouTube Player API) *Not all API support easy switching Target, eg: YouTube Player API