by Martin Aliverti
An innovative design, cutting edge animations, beautiful imagery, and an outstanding user experience. All of these things can make your app stand out from the common bunch, but sometimes, be it because these things just won’t be enough or because your content so demands it, you need to embed a plain old video in your android app. What are your options in such cases? Let’s find out.
Heads up: we will only show the tip of the iceberg here, no ready-to-use solution. You’ll have to dive a bit deeper in order to get production-ready code.
If you need a quick solution to get out of the way, you might just go full Mandrake and trick your users into thinking there is a video where there is none. You can include an ImageView with a snapshot of the video and add a click listener to it, with a link pointing to the platform where the video is actually hosted. The plus side here is that there is no need to have our own streaming server with the uploaded videos.
Take into account that if the snapshots are remote URLs you’ll have to use Picasso or any other async image loader to display them.
This is by far the easiest approach but the least interesting in terms of development and may result in a poor user experience since you depend on the target platform hosting the video, so let’s see what else we can do.
A second option, one which allows us to actually play the video within our app, is to use the MediaController and VideoView classes provided by the Android SDK. The solution is fairly simple and you depend on no third party library, but that comes at a price. You have no easy control customization for the video player nor advanced options, you would have to build such features yourself. It goes somewhat like this:
So if you need very raw video playback capabilities this might do the trick. You will have access to playback controls that fade away but customization is a bit trickier. Take into account you need to provide the actual URI to the video file, you cannot use youtube links. In order to be able to provide YouTube video content you would have to go over our last option in this post.
This is a third party library forked from EasyVideoPlayer, which is built on top of our previous option, namely the native MediaController + VideoView classes, but adds a bunch of really cool features to it. BetterVideoPlayer does not differ much from EasyVideoPlayer but it does add gesture controls, ‘loading wheel’ customization and, in my opinion, a better default UI. All in all, you get:
Better playback controls and user feedback options.
Control buttons look & feel customization (colors and drawables)
Gesture controls to fast-forward/rewind and also for volume
Clean API to command video playback and receive events
Once you’ve added the dependency to your Gradle file, you can have something like this up and running in no time:
You can also configure it via XML, but as far as I’ve seen some do not work perfectly, like setting the loading style.
Once again, in order to use this solution we have to provide URIs to the actual video files (e.g. http://my-server.com/cool-video.mp4), no YouTube links allowed here.
There is yet another option, with its own pros and cons. The YouTube app exposes an API your application may communicate with. The cool part is that not only you can reference particular videos but you can perform searches on the YouTube contents: videos, playlists, artists and so on.
This is a pretty powerful way to incorporate videos to your app with several options to do so. You might have a video embedded in your activity, or you can make use of a provided YouTubeFragment to handle the video playback, or you may even open a completely separate activity to play the video for you. Development is not as obvious as the previous options and depends on how you want to make use of it, so we won’t have any code here but instead point you to the google docs.
The downside here is the dependency upon the presence of the YouTube app in the devices of your users, if they don’t have YouTube app installed then you can’t access its data.
So there you have it, four different ways to add video playback capabilities to your app. Like we said, we’ve presented the basics to start developing your video app, but there is a lot more to cover within each option. Have fun.