ExoPlayerを使用して、動画を再生するためにGLSurfaceViewを利用します。

GLSurfaceView上でExoPlayerを使用してビデオを再生するには、次の手順を完了する必要があります。

  1. プロジェクトにExoPlayerライブラリの依存関係を追加してください。次のコードをbuild.gradleファイルのdependenciesブロックに追加できます。
implementation 'com.google.android.exoplayer:exoplayer-core:2.X.X'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.X.X'

2.X.XをExoPlayerのバージョンに置き換えてください。

  1. レイアウトファイルにGLSurfaceViewコントロールを追加してください。例えば、
<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. ActivityやFragment内で、ExoPlayerとGLSurfaceViewを初期化し、RendererとSurfaceViewを設定します。以下はサンプルコードです:
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クラスは、GLSurfaceViewのRendererインタフェースを実装し、onSurfaceCreatedメソッドでSurfaceTextureとExoPlayerを関連付けるために使用されます。

  1. エクストラクターメディアソース
MediaSource mediaSource = buildMediaSource(Uri.parse("http://example.com/video.mp4"));
player.prepare(mediaSource);
player.setPlayWhenReady(true);

buildMediaSourceメソッドは適切なMediaSourceオブジェクトを構築するために使用されます。必要に応じて、異なるMediaSourceの実装を選択することができます。

  1. AndroidManifest.xmlファイルに、ネットワークアクセス権限などの必要な権限が含まれていることを確認してください。

GLSurfaceViewとExoPlayerを使用してビデオを再生する基本的な手順でした。必要に応じてカスタマイズや調整が可能です。

bannerAds