Location>code7788 >text

FFmpeg development notes (fifty-six) using Media3's Exoplayer playback network video

Popularity:339 ℃/2024-10-13 11:15:39
Android's early MediaPlayer control for network video compatibility is very poor, so later introduced a separate Exoplayer library to enhance support for network video, in the "Android Studio Development in Action: From Zero Basics to the App on the line (3rd Edition)" book chapter 14 of the "14.3.3 new ExoPlayer" in chapter 14 of the book "Android Studio Development: Zero Basics to App Launch (3rd Edition)", "14.3.3 New ExoPlayer" describes the detailed use of Exoplayer library.

Now Android officially upgraded Exoplayer again and incorporated it into Jetpack's Media3 third-generation media library as a unified processing engine for audio and video related operations. The upgraded Exoplayer becomes the unified media playback engine of Media3, providing application-level components for audio and video playback, intending to unify the world in the direction of audio and video rendering.
According to the Android official website, ExoPlayer also gets rid of device and OS fragmentation issues, allowing app code to run in a consistent way across the Android ecosystem. Because Media3 is a very, very new Jetpack library, it requires a relatively high development environment, and the following conditions need to be met in order to properly introduce Media3.
1, development tools to choose Android Studio Jellyfish (small jellyfish version) or higher.
2. Gradle version is not less than 8.6.
3. Inside the App module, compileSdk and targetSdk should be upgraded to version 34 or higher.
After you have prepared the development environment according to the above conditions, you can introduce Media3's ExoPlayer library as described below, and the detailed steps are described as follows.

I. Modification of the module

Inside the dependencies node, add the following package-guide statement to add all the relevant libraries used by ExoPlayer.

implementation "androidx.media3:media3-exoplayer:1.4.0"
implementation "androidx.media3:media3-exoplayer-hls:1.4.0"
implementation "androidx.media3:media3-exoplayer-rtsp:1.4.0"
implementation "androidx.media3:media3-ui:1.4.0"
implementation "androidx.media3:media3-common:1.4.0"
implementation "androidx.media3:media3-session:1.4.0"
implementation "androidx.media3:media3-datasource:1.4.0"
implementation "androidx.media3:media3-datasource-rtmp:1.4.0"

II. Editing the layout file of the playback interface

Open the XML layout file for the playback interface and add the following property configuration to the root layout:

xmlns:app="/apk/res-auto"

Next, add the PlayerView control node from the ExoPlayer library inside the layout, as shown in the example below:

<! -- use_controller whether or not to show the control bar, show_timeout the interval between the disappearance of the control bar, show_buffering whether or not to show the buffer, resize_mode resizing mode -->.
<androidx.
    android.
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:show_buffering="always"
    app:show_timeout="5000"
    app:use_controller="true"
    app:resize_mode="fit" />

Third, the event page code to add ExoPlayer processing code

First add the following line of code to declare the PlayerView object:

private ExoPlayer mPlayer; // declare a new type of player object

Then add the following code inside the onCreate method to get the PlayerView object and set the ExoPlayer player object to it:

    PlayerView pv_content = findViewById(.pv_content);
    mPlayer = new (this).build();
    pv_content.setPlayer(mPlayer); // set the player object for the player view

Then add the following playback method to start playing the web video of the specified link.

// Play the video
private void playVideo(Uri uri) {
     factory = new (this); // Create a media object with the specified address.
    // Create a media object with the specified address
    MediaItem videoItem = new ().setUri(uri).build();
    // Create the media source based on the factory object and the media object
    MediaSource videoSource;
    if (().endsWith("m3u8")) { // hls link
        videoSource = new (factory)
                .createMediaSource(videoItem);
    } else if (().startsWith("rtsp")) { // rtsp links
        videoSource = new ()
                .createMediaSource(videoItem);
    } else if (().startsWith("rtmp")) { // rtmp link
        videoSource = new (new ())
                .createMediaSource(videoItem);
    } else { // other links (normal video links that start with http or https)
        videoSource = new (factory)
                .createMediaSource(videoItem); } else { // Other links (regular video links that start with http or https).
    }
    (videoSource); // Set the media source for the player.
    (); // Player is ready
    (); // Player starts playing
}

From the above playback code, it can be seen that four playback forms of ExoPlayer are used here, which are: play HLS link, play RTMP link, play RTSP link, and play normal network video.

IV. Granting additional Internet access

Open and add the following internet permission configuration:

<uses-permission android:name="" />

Finally compile and run the app, the player effect seen on the real machine is shown below.

You can see that you successfully ran Media3's ExoPlayer library and played the web video normally.

For more details on FFmpeg development seeFFmpeg Development in Action: From Zero Basics to Short Video OnlineA book.