Android Streaming Video With ExoPlayer 2 ( With IMA Ads)

Playing videos from storage is a lot more simple than streaming video from the server. There are many tasks need to handled while streaming video like adaptive playback, composition, caching, bandwidth control etc. Streaming video with android low-level API like MediaPlayer is complex. Android Streaming Video With  ExoPlayer 2 provides a big set of customization. Worlds largest video sharing network YouTube, Facebook, PlayMovies also uses ExoPlayer. Furthermore, there are more than 140,000 applications making use of ExoPlayer. In this tutorial, we will develop simple video streaming application with ExoPlayer.

Download Source Code and Application

DOWNLOAD PROJECT

 

Demo Video

Android Streaming Video With ExoPlayer 2

Adding ExoPlayer Libray to Android Studio Project

Create new android studio project or open existing project. Open Gradle Scripts -> build.gradle(Module App). Add ExoPlayer and IMA Extension dependency to your file as shown below.(Note: IAM Extension dependency is optional add it only if you want to show video ads in between your videos like YouTube)

 

Creating Full-Screen Activity

While playing video user needs to get full advantage of the screen that’s why we need to create full-screen immersive activity. Create new class Package Name -> VideoStreamingActivity.java add the following code to it.

Create new layout res->layout->activity_video_streaming.xml. This will be the layout for VideoStreamingActivity. Add following lines to it

Adding SimpleExoPlayerView To Layout

SimpleExoPlayerView is component provides predefined video player skin. This component contains surface view, player controller etc. open res -> layout -> activity_video_streaming.xml and paste following lines inside fullscreen_content  FrameLayout as show below.

Creating Media Source 

ExoPlayer does not take directly Video Url as input it takes media source as its input. There are many types of media sources like ExtractorMediaSourceConcatenatingMediaSource(Used for playing playlist), HlsMediaSource(Used for Live Streaming), DashMediaSource etc. For our application, we will create ExtractorMediaSource. Open Package Name -> VideoStreamingActivity.java . Add following function to it.

Creating Media Source with Ads 

If you are creating quality video content. You want to earn something from it. Then ExoPlayer provides IMA extension. This extension provides video ads integration during video playback. If you want to create media source with ads then Open Package Name -> VideoStreamingActivity.java . Add following function to it.

Creating Video Player Configuration

Create new class for defining Video Player Configuration Package Name -> VideoPlayerConfig.java

  1. MIN_BUFFER_DURATION: Minimum Video you want to buffer while Playing
  2. MAX_BUFFER_DURATION: Max Video you want to buffer during PlayBack
  3. MIN_PLAYBACK_START_BUFFER: Min Video you want to buffer before start Playing it
  4. MIN_PLAYBACK_RESUME_BUFFER: Min video You want to buffer when user resumes video

Initializing ExoPlayer 

Now our Media Source and Video Player Configuration are ready. Define To initialize ExoPlayer create a new function initializePlayer() in Package Name -> VideoStreamingActivity.java. Add following lines to it.

Managing Player with Activity Life Cycle

Now it is important to change player state during different activity lifecycle events like onPause(), onResume() and onDestroy() methods. Open Package Name -> VideoStreamingActivity.java add following lines to it.

Finally

Open Package Name -> VideoStreamingActivity.java initialize player inside onCreate() Method

manifest.xml

colors.xml

attrs.xml

right click res -> values folder and select New ->  Values resource file. Enter the name as attrs and press ok. Paste following lines in it.

strings.xml

add following strings to your res -> values -> strings.xml file.

styles.xml

Add following styles to your res -> values -> styles.xml  file.

Running  Application 

Run the application and see the results.

Android Streaming Video With ExoPlayer 2 - Output Ads

Android Streaming Video With ExoPlayer 2 - Video

If you still have any queries, please post them in the comments section below, I will be happy to help you. If you have any other questions in mind then make sure to visit our ask section.

Share this post

Hello there, My name is Amardeep founder of loopwiki.com. I have experience in many technologies like Android, Java, Php etc. In this variety of technologies, I love Android App Development. If you have any idea and you want me to develop for you then let’s have chat Email: [email protected]

7 Comments

    • Thank You Mate, You can use ConcatenatingMediaSource. Refer this example to create PlayList of video or audio using ExoPlayer

    • Thanks for your question, Facebook and Vimeo don’t provide there media source URLs as per my knowledge. But maybe there is a way to get these Media Source URLs by using there API. Read there API and find a way to obtain Media Source URLs. If you find Media Source URL then it is easy to implement with ExoPlayer.

      Now Media Source is ready, pass it to play with ExoPlayer
      player.prepare(contentMediaSource);
      To start Playing Video
      player.setPlayWhenReady(true);

Leave a Reply

Your email address will not be published. Required fields are marked *