1*30877f79SAndroid Build Coastguard Worker---
2*30877f79SAndroid Build Coastguard Workertitle: UI components
3*30877f79SAndroid Build Coastguard Worker---
4*30877f79SAndroid Build Coastguard Worker
5*30877f79SAndroid Build Coastguard WorkerAn app playing media requires user interface components for displaying media and
6*30877f79SAndroid Build Coastguard Workercontrolling playback. The ExoPlayer library includes a UI module that contains
7*30877f79SAndroid Build Coastguard Workera number of UI components. To depend on the UI module add a dependency as shown
8*30877f79SAndroid Build Coastguard Workerbelow.
9*30877f79SAndroid Build Coastguard Worker
10*30877f79SAndroid Build Coastguard Worker~~~
11*30877f79SAndroid Build Coastguard Workerimplementation 'com.google.android.exoplayer:exoplayer-ui:2.X.X'
12*30877f79SAndroid Build Coastguard Worker~~~
13*30877f79SAndroid Build Coastguard Worker{: .language-gradle}
14*30877f79SAndroid Build Coastguard Worker
15*30877f79SAndroid Build Coastguard WorkerThe most important component is `StyledPlayerView`, a view for media
16*30877f79SAndroid Build Coastguard Workerplaybacks. It displays video, subtitles and album art during playback, as
17*30877f79SAndroid Build Coastguard Workerwell as playback controls.
18*30877f79SAndroid Build Coastguard Worker
19*30877f79SAndroid Build Coastguard Worker`StyledPlayerView` has a `setPlayer` method for attaching and detaching (by
20*30877f79SAndroid Build Coastguard Workerpassing `null`) player instances.
21*30877f79SAndroid Build Coastguard Worker
22*30877f79SAndroid Build Coastguard Worker## StyledPlayerView ##
23*30877f79SAndroid Build Coastguard Worker
24*30877f79SAndroid Build Coastguard Worker`StyledPlayerView` can be used for both video and audio playbacks. It renders
25*30877f79SAndroid Build Coastguard Workervideo and subtitles in the case of video playback, and can display artwork
26*30877f79SAndroid Build Coastguard Workerincluded as metadata in audio files. You can include it in your layout files
27*30877f79SAndroid Build Coastguard Workerlike any other UI component. For example, a `StyledPlayerView` can be included
28*30877f79SAndroid Build Coastguard Workerwith the following XML:
29*30877f79SAndroid Build Coastguard Worker
30*30877f79SAndroid Build Coastguard Worker~~~
31*30877f79SAndroid Build Coastguard Worker<com.google.android.exoplayer2.ui.StyledPlayerView
32*30877f79SAndroid Build Coastguard Worker    android:id="@+id/player_view"
33*30877f79SAndroid Build Coastguard Worker    android:layout_width="match_parent"
34*30877f79SAndroid Build Coastguard Worker    android:layout_height="match_parent"
35*30877f79SAndroid Build Coastguard Worker    app:show_buffering="when_playing"
36*30877f79SAndroid Build Coastguard Worker    app:show_shuffle_button="true"/>
37*30877f79SAndroid Build Coastguard Worker~~~
38*30877f79SAndroid Build Coastguard Worker{: .language-xml}
39*30877f79SAndroid Build Coastguard Worker
40*30877f79SAndroid Build Coastguard WorkerThe snippet above illustrates that `StyledPlayerView` provides several
41*30877f79SAndroid Build Coastguard Workerattributes. These attributes can be used to customize the view's behavior, as
42*30877f79SAndroid Build Coastguard Workerwell as its look and feel. Most of these attributes have corresponding setter
43*30877f79SAndroid Build Coastguard Workermethods, which can be used to customize the view at runtime. The
44*30877f79SAndroid Build Coastguard Worker[`StyledPlayerView`][] Javadoc lists these attributes and setter methods in
45*30877f79SAndroid Build Coastguard Workermore detail.
46*30877f79SAndroid Build Coastguard Worker
47*30877f79SAndroid Build Coastguard WorkerOnce the view is declared in the layout file, it can be looked up in the
48*30877f79SAndroid Build Coastguard Worker`onCreate` method of the activity:
49*30877f79SAndroid Build Coastguard Worker
50*30877f79SAndroid Build Coastguard Worker~~~
51*30877f79SAndroid Build Coastguard Worker@Override
52*30877f79SAndroid Build Coastguard Workerprotected void onCreate(Bundle savedInstanceState) {
53*30877f79SAndroid Build Coastguard Worker  super.onCreate(savedInstanceState);
54*30877f79SAndroid Build Coastguard Worker  // ...
55*30877f79SAndroid Build Coastguard Worker  playerView = findViewById(R.id.player_view);
56*30877f79SAndroid Build Coastguard Worker}
57*30877f79SAndroid Build Coastguard Worker~~~
58*30877f79SAndroid Build Coastguard Worker{: .language-java}
59*30877f79SAndroid Build Coastguard Worker
60*30877f79SAndroid Build Coastguard WorkerWhen a player has been initialized, it can be attached to the view by calling
61*30877f79SAndroid Build Coastguard Worker`setPlayer`:
62*30877f79SAndroid Build Coastguard Worker
63*30877f79SAndroid Build Coastguard Worker~~~
64*30877f79SAndroid Build Coastguard Worker// Instantiate the player.
65*30877f79SAndroid Build Coastguard Workerplayer = new ExoPlayer.Builder(context).build();
66*30877f79SAndroid Build Coastguard Worker// Attach player to the view.
67*30877f79SAndroid Build Coastguard WorkerplayerView.setPlayer(player);
68*30877f79SAndroid Build Coastguard Worker// Set the media item to be played.
69*30877f79SAndroid Build Coastguard Workerplayer.setMediaItem(mediaItem);
70*30877f79SAndroid Build Coastguard Worker// Prepare the player.
71*30877f79SAndroid Build Coastguard Workerplayer.prepare();
72*30877f79SAndroid Build Coastguard Worker~~~
73*30877f79SAndroid Build Coastguard Worker{: .language-java}
74*30877f79SAndroid Build Coastguard Worker
75*30877f79SAndroid Build Coastguard Worker### Choosing a surface type ###
76*30877f79SAndroid Build Coastguard Worker
77*30877f79SAndroid Build Coastguard WorkerThe `surface_type` attribute of `StyledPlayerView` lets you set the type of
78*30877f79SAndroid Build Coastguard Workersurface used for video playback. Besides the values `spherical_gl_surface_view`
79*30877f79SAndroid Build Coastguard Worker(which is a special value for spherical video playback) and
80*30877f79SAndroid Build Coastguard Worker`video_decoder_gl_surface_view` (which is for video rendering using extension
81*30877f79SAndroid Build Coastguard Workerrenderers), the allowed values are `surface_view`, `texture_view` and `none`. If
82*30877f79SAndroid Build Coastguard Workerthe view is for audio playback only, `none` should be used to avoid having to
83*30877f79SAndroid Build Coastguard Workercreate a surface, since doing so can be expensive.
84*30877f79SAndroid Build Coastguard Worker
85*30877f79SAndroid Build Coastguard WorkerIf the view is for regular video playback then `surface_view` or `texture_view`
86*30877f79SAndroid Build Coastguard Workershould be used. `SurfaceView` has a number of benefits over `TextureView` for
87*30877f79SAndroid Build Coastguard Workervideo playback:
88*30877f79SAndroid Build Coastguard Worker
89*30877f79SAndroid Build Coastguard Worker* Significantly lower power consumption on many devices.
90*30877f79SAndroid Build Coastguard Worker* More accurate frame timing, resulting in smoother video playback.
91*30877f79SAndroid Build Coastguard Worker* Support for secure output when playing DRM protected content.
92*30877f79SAndroid Build Coastguard Worker* The ability to render video content at the full resolution of the display on
93*30877f79SAndroid Build Coastguard Worker  Android TV devices that upscale the UI layer.
94*30877f79SAndroid Build Coastguard Worker
95*30877f79SAndroid Build Coastguard Worker`SurfaceView` should therefore be preferred over `TextureView` where possible.
96*30877f79SAndroid Build Coastguard Worker`TextureView` should be used only if `SurfaceView` does not meet your needs. One
97*30877f79SAndroid Build Coastguard Workerexample is where smooth animations or scrolling of the video surface is required
98*30877f79SAndroid Build Coastguard Workerprior to Android N, as described below. For this case, it's preferable to use
99*30877f79SAndroid Build Coastguard Worker`TextureView` only when [`SDK_INT`][] is less than 24 (Android N) and
100*30877f79SAndroid Build Coastguard Worker`SurfaceView` otherwise.
101*30877f79SAndroid Build Coastguard Worker
102*30877f79SAndroid Build Coastguard Worker`SurfaceView` rendering wasn't properly synchronized with view animations until
103*30877f79SAndroid Build Coastguard WorkerAndroid N. On earlier releases this could result in unwanted effects when a
104*30877f79SAndroid Build Coastguard Worker`SurfaceView` was placed into scrolling container, or when it was subjected to
105*30877f79SAndroid Build Coastguard Workeranimation. Such effects included the view's contents appearing to lag slightly
106*30877f79SAndroid Build Coastguard Workerbehind where it should be displayed, and the view turning black when subjected
107*30877f79SAndroid Build Coastguard Workerto animation. To achieve smooth animation or scrolling of video prior to Android
108*30877f79SAndroid Build Coastguard WorkerN, it's therefore necessary to use `TextureView` rather than `SurfaceView`.
109*30877f79SAndroid Build Coastguard Worker{:.info}
110*30877f79SAndroid Build Coastguard Worker
111*30877f79SAndroid Build Coastguard WorkerSome Android TV devices run their UI layer at a resolution that's lower than the
112*30877f79SAndroid Build Coastguard Workerfull resolution of the display, upscaling it for presentation to the user. For
113*30877f79SAndroid Build Coastguard Workerexample, the UI layer may be run at 1080p on an Android TV that has a 4K
114*30877f79SAndroid Build Coastguard Workerdisplay. On such devices, `SurfaceView` must be used to render content at the
115*30877f79SAndroid Build Coastguard Workerfull resolution of the display. The full resolution of the display (in its
116*30877f79SAndroid Build Coastguard Workercurrent display mode) can be queried using [`Util.getCurrentDisplayModeSize`][].
117*30877f79SAndroid Build Coastguard WorkerThe UI layer resolution can be queried using Android's [`Display.getSize`] API.
118*30877f79SAndroid Build Coastguard Worker{:.info}
119*30877f79SAndroid Build Coastguard Worker
120*30877f79SAndroid Build Coastguard Worker### Overriding drawables ###
121*30877f79SAndroid Build Coastguard Worker
122*30877f79SAndroid Build Coastguard WorkerWe don't guarantee that the customizations described in the following section
123*30877f79SAndroid Build Coastguard Workerwill continue to work in future versions of the library. The resource IDs may
124*30877f79SAndroid Build Coastguard Workerchange name, or some may be deleted entirely. This is indicated by the
125*30877f79SAndroid Build Coastguard Worker[resource IDs being marked 'private'][].
126*30877f79SAndroid Build Coastguard Worker{:.info}
127*30877f79SAndroid Build Coastguard Worker
128*30877f79SAndroid Build Coastguard Worker`StyledPlayerView` uses `StyledPlayerControlView` to display the playback
129*30877f79SAndroid Build Coastguard Workercontrols and progress bar. The drawables used by `StyledPlayerControlView` can
130*30877f79SAndroid Build Coastguard Workerbe overridden by drawables with the same names defined in your application. See
131*30877f79SAndroid Build Coastguard Workerthe [`StyledPlayerControlView`][] Javadoc for a list of control drawables that
132*30877f79SAndroid Build Coastguard Workercan be overridden.
133*30877f79SAndroid Build Coastguard Worker
134*30877f79SAndroid Build Coastguard Worker## Further customization ##
135*30877f79SAndroid Build Coastguard Worker
136*30877f79SAndroid Build Coastguard WorkerWhere customization beyond that described above is required, we expect that app
137*30877f79SAndroid Build Coastguard Workerdevelopers will implement their own UI components rather than use those provided
138*30877f79SAndroid Build Coastguard Workerby ExoPlayer's UI module.
139*30877f79SAndroid Build Coastguard Worker
140*30877f79SAndroid Build Coastguard Worker[`StyledPlayerView`]: {{ site.exo_sdk }}/ui/StyledPlayerView.html
141*30877f79SAndroid Build Coastguard Worker[`StyledPlayerControlView`]: {{ site.exo_sdk }}/ui/StyledPlayerControlView.html
142*30877f79SAndroid Build Coastguard Worker[resource IDs being marked 'private']: https://developer.android.com/studio/projects/android-library#PrivateResources
143*30877f79SAndroid Build Coastguard Worker[`SDK_INT`]: {{ site.android_sdk }}/android/os/Build.VERSION.html#SDK_INT
144*30877f79SAndroid Build Coastguard Worker[`Util.getCurrentDisplayModeSize`]: {{ site.exo_sdk }}/util/Util.html#getCurrentDisplayModeSize(android.content.Context)
145*30877f79SAndroid Build Coastguard Worker[`Display.getSize`]: {{ site.android_sdk }}/android/view/Display.html#getSize(android.graphics.Point)
146