ExoPlayer utilizes GLSurfaceView for video playback.

To play videos using ExoPlayer on a GLSurfaceView, you need to complete the following steps:

  1. Add the ExoPlayer library dependency to your project. You can add the following code line in the dependencies block of the build.gradle file.
implementation 'com.google.android.exoplayer:exoplayer-core:2.X.X'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.X.X'

Replace 2.X.X with the ExoPlayer version you want to use.

  1. Add a GLSurfaceView widget to your layout file, for example:
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.opengl.GLSurfaceView
        android:id="@+id/glsurfaceview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
  1. Initialize ExoPlayer and GLSurfaceView in your Activity or Fragment, and set the Renderer and SurfaceView. Here is an example code:
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.util.Util;

public class MainActivity extends AppCompatActivity {

    private GLSurfaceView glSurfaceView;
    private SimpleExoPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        glSurfaceView = findViewById(R.id.glsurfaceview);
        glSurfaceView.setEGLContextClientVersion(2);

        player = ExoPlayerFactory.newSimpleInstance(
                new DefaultRenderersFactory(this),
                new DefaultTrackSelector(),
                new DefaultLoadControl());

        PlayerView playerView = new PlayerView(this);
        playerView.setPlayer(player);

        glSurfaceView.setRenderer(new VideoRenderer(player));
        glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

    @Override
    protected void onResume() {
        super.onResume();
        glSurfaceView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        glSurfaceView.onPause();
    }

    private static class VideoRenderer implements GLSurfaceView.Renderer {

        private SimpleExoPlayer player;

        public VideoRenderer(SimpleExoPlayer player) {
            this.player = player;
        }

        @Override
        public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
            Surface surface = new Surface(/* your SurfaceTexture */);
            player.setVideoSurface(surface);
        }

        @Override
        public void onSurfaceChanged(GL10 gl10, int width, int height) {
            // Adjust the player's video output to match the GLSurfaceView's dimensions.
        }

        @Override
        public void onDrawFrame(GL10 gl10) {
            // Render the video frame using OpenGL.
        }
    }
}

VideoRenderer class, which implements the Renderer interface of GLSurfaceView, is used to associate SurfaceTexture with ExoPlayer in the onSurfaceCreated method.

  1. Media source extractor
MediaSource mediaSource = buildMediaSource(Uri.parse("http://example.com/video.mp4"));
player.prepare(mediaSource);
player.setPlayWhenReady(true);

The buildMediaSource method is used to create the appropriate MediaSource object, and you can choose different MediaSource implementations as needed.

  1. Make sure you have included the necessary permissions in the AndroidManifest.xml file, such as internet access permission.

The above are the basic steps for playing videos using GLSurfaceView and ExoPlayer. You can customize and adjust as needed.

bannerAds