Best practices for Video Playback in Android

Saurabh Arora
Viki Blog
Published in
4 min readMay 28, 2019

--

Here at Viki, we pride ourselves in offering a great video playback experience. Over the last few years, we have identified a set of experiences that every video playback app should offer to their users.

Here is a summary of what we have learned (in no particular order):

(1) Keep Screen On

This might seem obvious but we do need to tell the Android system that Video Playback is ongoing and that we want the screen to be kept on as long the user is watching the video.

When video playback begins, you can usesetKeepScreenOn(true) to let the system know. Remember to set it to false once the playback is finished or if the user leaves the playback screen.

(2) Use System Volume

Users are conditioned to use the volume rockers keys to control the volume of video playback. Don’t fight the system and create your own volume controls. If your design or specifications have this, try discussing if it can be avoided. It’s definitely one less thing that you need to manage.

(3) Manage Audio Focus

Android allows many apps to play audio simultaneously. Even though this is great, it can be very disruptive for the user, if multiple apps start to play audio at the same time. To avoid such a scenario, Android introduces a concept of Audio Focus. If your app wants to output audio, it should request for audio focus, so that the system can inform other apps to take action accordingly. From the developer docs,

Audio focus is cooperative. Apps are encouraged to comply with the audio focus guidelines, but the system does not enforce the rules.

Hence, when your app is playing audio, be a good citizen and let the system know that you are requesting audio focus. Video playback apps should ideally request AUDIOFOCUS_GAIN when they are about to start playback.

Be a good citizen. Prevent two audio sources from playing together

Also, set up an AudioManager.OnAudioFocusChangeListener to respond to audio focus changes by pausing playback (loss of audio focus) or by reducing the playback volume (ducking). Lastly, remember to abandonAudioFocus() once you are finished with the playback.

(4) Pause/mute when headphones are removed

Most users expect video/audio playback to mute or even pause when they unplug the headphones. If you play videos on your Mac/Windows PC, you’ll observe a similar behavior when you unplug your headphones. Be a good citizen and don’t shock the users. It’s always better to err on the side of caution.

Don’t shock the users

When headphones are unplugged, the Android system sends an ACTION_AUDIO_BECOMING_NOISY broadcast. It is a good idea to listen for this broadcast when your video playback is ongoing and take appropriate action so as not to surprise the user with audio from the speaker.

(5) Support headphone media playback keys

Most headphones/earphones have media playback control keys such as play/pause, seek and next/previous videos. Users expect these keys to work seamlessly with your app. For this, Android offers MediaSession

MediaSession is a framework through which third-party actors can control the media session of your app. This includes not just headphones, but also other possible media controllers such as Android Wear, Google Assistant, Android Auto, etc.

A MediaSession should be created when an app wants to publish media playback information or handle media keys.

Through the Media Session framework, you can provide information about your current media state, such as playing, paused, buffering, etc. and also handle media keys such as play, pause, seek, etc. Third-party actors can then use this framework to create their own MediaController UI to allow the user to control the playback of your app.

(6) Support Split Screen

Android 7.0 added support for showing more than one window at a time. Multi-window allows the user to multitask since two apps can run side-by-side or one-above-the-other in split-screen mode.

Viki video playback in split-screen mode

For video playback apps, we should make sure that we do not pause the video playback when the activity lifecycle changes to onPause. Instead, we should pause video playback in onStop (in multi-window mode, the app not in focus is in pause state). Since the display window is now smaller as compared to full-screen mode, we should also take care to give maximum real estate to video playback and minimize the appearance of other controls/interactions that might appear in full-screen mode (non split screen mode).

(7) Use SensorLandscape for full-screen mode

For landscape or for portrait mode, Android offers multiple orientation options. If your app allows the user to switch between landscape and portrait mode, make sure you use sensor orientation instead of regular orientation.

For example, in SCREEN_ORIENTATION_SENSOR_LANDSCAPE mode, the user can hold the phone in either of the two landscape orientation and the screen will rotate to adjust accordingly. This is extremely handy for users watching videos while charging their phone, or just prefer to hold the phone in a certain manner.

That’s it! I have chosen to focus on learnings which Android developers can adopt out of the box. There are other feature sets that can be considered best practices such as PIP (picture in picture) and Mobile Data Consumption warnings but these can vary across business use cases.

If you feel there is something important that I missed out, let me know in the comment below. Or you can hit me up on twitter at @saurabh_arora90.

--

--