- ExoPlayer in Android with Example
- Advantages of Using ExoPlayer
- ExoPlayer vs MediaPlayer
- Step by Step Implementation of ExoPlayer in Android
- Hello world!
- Adding ExoPlayer as a dependency
- Add ExoPlayer modules
- Turn on Java 8 support
- Enable multidex
- Creating the player
- A note on threading
- Attaching the player to a view
- Populating the playlist and preparing the player
- Controlling the player
- ExoPlayer Video Streaming in Android
- ExoPlayer Android Tutorial Sample App
- 1. Create a new project.
- 2. After that add below dependency in build.gradle.
- 3. Open activity_main.xml and add two Button like below code.
- 4.1 Now connect this layout with MainActivity.java
- 5. Now MainActivity.java full code seems like below
- 6. Configure ExoPlayer
- 7. Create a layout file name with activity_exo_player.xml
- 8. Same as Create ExoPlayerActivity.java
- 9. Finally, The full activity (ExoPlayerActivity.java) code looks like this
- Conclusion
ExoPlayer in Android with Example
ExoPlayerView is one of the most used UI components in many apps such as YouTube, Netflix, and many video streaming platforms. ExoPlayerView is used for audio as well as video streaming in Android apps. Many Google apps use ExoPlayerView for streaming audios and videos. ExoPlayer is a media player library that provides a way to play audio and video with lots of customization in it. It is an alternative that is used to play videos and audios in Android along with MediaPlayer. ExoPlayer is a library that is the best alternative source for playing audio and videos on Android. This library will also help you to customize your media player according to our requirements.
Advantages of Using ExoPlayer
- ExoPlayer provides the support for the playlist and with this, you can clip or merge your media.
- With the help of ExoPlayer, you can directly fetch media files such as audios and videos directly from the internet and play them inside the ExoPlayer.
- It provides smooth encryption and streaming of video and audio files.
- ExoPlayer provides you the ability to customize your media player according to your requirements.
ExoPlayer vs MediaPlayer
MediaPlayer
Step by Step Implementation of ExoPlayer in Android
We will be creating a simple video player app in which we will be fetching a video from a URL and play that video inside our ExoPlayer. Note that we are using JAVA for implementing ExoPlayer in Android.
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add dependency to the build.gradle(Module:app)
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
// for core support in exoplayer.
// for adding dash support in our exoplayer.
// for adding hls support in exoplayer.
// for smooth streaming of video in our exoplayer.
// for generating default ui of exoplayer
After adding this dependency sync the project.
Step 3: Add internet permission in your Manifest file
Navigate to the app > manifest folder and write down the following permissions to it.
Источник
Hello world!
Another way to get started is to work through the ExoPlayer codelab.
For simple use cases, getting started with ExoPlayer consists of implementing the following steps:
- Add ExoPlayer as a dependency to your project.
- Create an ExoPlayer instance.
- Attach the player to a view (for video output and user input).
- Prepare the player with a MediaItem to play.
- Release the player when done.
These steps are described in more detail below. For a complete example, refer to PlayerActivity in the main demo app.
Adding ExoPlayer as a dependency
Add ExoPlayer modules
The easiest way to get started using ExoPlayer is to add it as a gradle dependency in the build.gradle file of your app module. The following will add a dependency to the full library:
where 2.X.X is your preferred version (the latest version can be found by consulting the release notes).
As an alternative to the full library, you can depend on only the library modules that you actually need. For example the following will add dependencies on the Core, DASH and UI library modules, as might be required for an app that only plays DASH content:
When depending on individual modules they must all be the same version.
The available library modules are listed below. Adding a dependency to the full ExoPlayer library is equivalent to adding dependencies on all of the library modules individually.
- exoplayer-core : Core functionality (required).
- exoplayer-dash : Support for DASH content.
- exoplayer-hls : Support for HLS content.
- exoplayer-rtsp : Support for RTSP content.
- exoplayer-smoothstreaming : Support for SmoothStreaming content.
- exoplayer-transformer : Media transformation functionality.
- exoplayer-ui : UI components and resources for use with ExoPlayer.
In addition to library modules, ExoPlayer has extension modules that depend on external libraries to provide additional functionality. Some extensions are available from the Maven repository, whereas others must be built manually. Browse the extensions directory and their individual READMEs for details.
More information on the library and extension modules that are available can be found on the Google Maven ExoPlayer page.
Turn on Java 8 support
If not enabled already, you need to turn on Java 8 support in all build.gradle files depending on ExoPlayer, by adding the following to the android section:
Enable multidex
If your Gradle minSdkVersion is 20 or lower, you should enable multidex in order to prevent build errors.
Creating the player
You can create an ExoPlayer instance using ExoPlayer.Builder , which provides a range of customization options. The code below is the simplest example of creating an instance.
A note on threading
ExoPlayer instances must be accessed from a single application thread. For the vast majority of cases this should be the application’s main thread. Using the application’s main thread is a requirement when using ExoPlayer’s UI components or the IMA extension.
The thread on which an ExoPlayer instance must be accessed can be explicitly specified by passing a Looper when creating the player. If no Looper is specified, then the Looper of the thread that the player is created on is used, or if that thread does not have a Looper , the Looper of the application’s main thread is used. In all cases the Looper of the thread from which the player must be accessed can be queried using Player.getApplicationLooper .
If you see IllegalStateException being thrown with the message “Player is accessed on the wrong thread”, then some code in your app is accessing an ExoPlayer instance on the wrong thread (the exception’s stack trace shows you where). You can temporarily opt out from these exceptions being thrown by calling ExoPlayer.setThrowsWhenUsingWrongThread(false) , in which case the issue will be logged as a warning instead. Using this opt out is not safe and may result in unexpected or obscure errors. It will be removed in ExoPlayer 2.16.
For more information about ExoPlayer’s threading model, see the “Threading model” section of the ExoPlayer Javadoc.
Attaching the player to a view
The ExoPlayer library provides a range of pre-built UI components for media playback. These include StyledPlayerView , which encapsulates a StyledPlayerControlView , a SubtitleView , and a Surface onto which video is rendered. A StyledPlayerView can be included in your application’s layout xml. Binding the player to the view is as simple as:
You can also use StyledPlayerControlView as a standalone component, which is useful for audio only use cases.
Use of ExoPlayer’s pre-built UI components is optional. For video applications that implement their own UI, the target SurfaceView , TextureView , SurfaceHolder or Surface can be set using ExoPlayer ’s setVideoSurfaceView , setVideoTextureView , setVideoSurfaceHolder and setVideoSurface methods respectively. ExoPlayer ’s addTextOutput method can be used to receive captions that should be rendered during playback.
Populating the playlist and preparing the player
In ExoPlayer every piece of media is represented by a MediaItem . To play a piece of media you need to build a corresponding MediaItem , add it to the player, prepare the player, and call play to start the playback:
ExoPlayer supports playlists directly, so it’s possible to prepare the player with multiple media items to be played one after the other:
The playlist can be updated during playback without the need to prepare the player again. Read more about populating and manipulating the playlist on the Playlists page. Read more about the different options available when building media items, such as clipping and attaching subtitle files, on the Media items page.
Prior to ExoPlayer 2.12, the player needed to be given a MediaSource rather than media items. From 2.12 onwards, the player converts media items to the MediaSource instances that it needs internally. Read more about this process and how it can be customized on the Media sources page. It’s still possible to provide MediaSource instances directly to the player using ExoPlayer.setMediaSource(s) and ExoPlayer.addMediaSource(s) .
Controlling the player
Once the player has been prepared, playback can be controlled by calling methods on the player. Some of the most commonly used methods are listed below.
- play and pause start and pause playback.
- seekTo allows seeking within the media.
- hasPrevious , hasNext , previous and next allow navigating through the playlist.
- setRepeatMode controls if and how media is looped.
- setShuffleModeEnabled controls playlist shuffling.
- setPlaybackParameters adjusts playback speed and audio pitch.
If the player is bound to a StyledPlayerView or StyledPlayerControlView , then user interaction with these components will cause corresponding methods on the player to be invoked.
Источник
ExoPlayer Video Streaming in Android
We demonstrate video streaming from the server using the ExoPlayer. In fact, youtube player also used ExoPlayer for streaming video. This ExoPlayer android example I will explain how to use ExoPlayer in own android application.
ExoPlayer is an open-source project. This is not a part of Android SDK. In Android, ExoPlayer is application level media player. It’s standard audio and video components are built on Android’s MediaCodec API, which was released in Android.
ExoPlayer is easy to use, maintainable and fully customize.
ExoPlayer Android Tutorial Sample App
1. Create a new project.
Go to File Menu and Create a New Project, fill information like project and package name after that click Next and select target SDK and Finish
2. After that add below dependency in build.gradle.
In the gradle.xml file, We have 2 major dependencies. One for ExoPlayer and second as know about ButterKnife already. Basically, the butterknife is injected of view in the java file.
3. Open activity_main.xml and add two Button like below code.
While creating project Android Studio is provide default template like EmptyActivity . So MainActivity.java and activity_main.xml is automatically created. So open activity_main.xml and add to button
As per design, we have created two buttons one for play default video. The second user can put own URL in dialog prompt.
4.1 Now connect this layout with MainActivity.java
Open the main activity, we generate view inject code by using ButterKnief injection and set on click listeners.
4.2. Let’s bind butter knife in onCreate Activity
4.3. -As per above, We are using two buttons. So If you want to play a default video then we call intent with default URL of ExoPlayer activity.
like Below
4.3. – Button for playing its own video by entering video URL. So taking user URL we have created alert dialog with edit text.
Just create a layout file for dialog view. In res, folder create file named dialog_prompts.xml and add few components
Open again MainActivity.java and put this code for creating dialog
5. Now MainActivity.java full code seems like below
6. Configure ExoPlayer
Before going to Exo Player activity we define followings configuration of ExoPlayer.
MIN_BUFFER_DURATION – Minimum Video you want to buffer while Playing. for now, I have set 3 second
MAX_BUFFER_DURATION – You can set Max Video you want to buffer during PlayBack.
MIN_PLAYBACK_START_BUFFER – Set Min Video you want to buffer before start Playing it
MIN_PLAYBACK_RESUME_BUFFER – Set Min video You want to buffer when the user resumes video
DEFAULT_VIDEO_URL – Set Default video url for demo
7. Create a layout file name with activity_exo_player.xml
Create layout file inside res-> layout-> activity_exo_player.xml. and some components Progressbar(buffering indicator) and ExoPlayerView
8. Same as Create ExoPlayerActivity.java
Go to create an Activity with ExoPlayerActivity.java and set content layout is activity_exo_player.xml. Furthermore generates ButteKnief Injection by select set ContentView and right click and click generate. Now one popup appeared select view and set ids and click ok.
8.1. Define some variables and do some fullscreen configuration
8.2. We are using vector icon so enable
8.3. Now initialise video player configuration
8.3. After inilizaing player create a media source using below methods
8.4. Implement Player.EventListener and override unimplemented methods
Just implement Player.EventListener you have to implement override methods. onPlayerStateChanged() methods will return all player state.
9. Finally, The full activity (ExoPlayerActivity.java) code looks like this
Mainly we are doing below things in this activity
- Initialize Player with configuration .
- Prepare source files and settings on the player.
- Add Player Event listener for listening to different player state
- Manage player state with activity life cycler eg. pausePlayer (), resumePlayer(), releasePlayer ()
Conclusion
Wow! Great job! You just finished the ExoPlayer android example. You learned about integrating ExoPlayer in your application.
Download Sample Project- Video Streaming ExoPlayer in Android
If you have any queries, feel free to connect us.
Источник