Android视频播放器ExoPlayer

Android视频播放器ExoPlayer

SimpleExoPlayer

1. app gradle

dependencies {

implementation 'com.google.android.exoplayer:exoplayer:2.11.1'

android {

compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
    }

2. AndroidManifest配置权限

权限

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

屏幕允许翻转

<activity android:name=".MainActivity" android:configChanges="orientation">

3. activity布局

<com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/exo_player"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

4. Activity代码

package com.example.mysimpleexo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;

import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;

public class MainActivity extends AppCompatActivity {

    private PlayerView playerView;
    private SimpleExoPlayer player;
    private Context context = MainActivity.this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        playerView = findViewById(R.id.exo_player);
        initializePlayer();

    }

    private void initializePlayer() {
        player = new SimpleExoPlayer.Builder(context).build();
        playerView.setPlayer(player);
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context,
                Util.getUserAgent(context,"myExoPlayer"));
        MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
                .createMediaSource(Uri.parse(Environment.getExternalStorageDirectory() + "/1.mp4"));
        player.prepare(mediaSource);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        player.release();
    }
}

4. 主要代码

//播放器初始化

private void initializePlayer() {
        player = new SimpleExoPlayer.Builder(context).build();
        playerView.setPlayer(player);
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context,
                Util.getUserAgent(context,"myExoPlayer"));
        MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
                .createMediaSource(Uri.parse(Environment.getExternalStorageDirectory() + "/1.mp4"));
        player.prepare(mediaSource);
    }

//资源释放

@Override
    public void onBackPressed() {
        super.onBackPressed();
        player.release();
    }

参考链接:
https://exoplayer.dev/ui-components.html
https://developer.android.google.cn/guide/topics/media/exoplayer

发布了20 篇原创文章 · 获赞 4 · 访问量 5837

猜你喜欢

转载自blog.csdn.net/greatyoulv/article/details/104023285