ExoPlayer实现网络URL、本地视频播放

EXOPlayer是Google官方开源的一种播放器

效果

 

依赖

implementation 'com.google.android.exoplayer:exoplayer:r2.5.4'

代码

package com.example.mvpfour.activity;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.example.mvpfour.R;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Util;

/**
 * Created by agen on 2018/8/31.
 */

public class ExoPlayerTwo extends AppCompatActivity implements View.OnClickListener {

    private Button button9;
    private SimpleExoPlayerView simpleExoPlayerView;

    static Handler mainHandler = new Handler();
    // step1. 创建一个默认的TrackSelector
    // 创建带宽
    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    // 创建轨道选择工厂
    TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);

    // 创建轨道选择器实例
    TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

    //step2. 创建播放器
    SimpleExoPlayer player;
    private int resumeWindow;
    private long resumePosition;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exoplayer_two);
        initView();
        initExoplayer();
    }

    private void initExoplayer() {
        //step2. 创建播放器
        player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
        // 创建加载数据的工厂
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
                Util.getUserAgent(this, "yourApplicationName"), (TransferListener) bandwidthMeter);

        // 创建解析数据的工厂
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        // 传入Uri、加载数据的工厂、解析数据的工厂,就能创建出MediaSource
        String url = "http://7xstkb.com1.z0.glb.clouddn.com/agen_apple.mp4";
        Uri mp4VideoUri = Uri.parse(url);
        MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri,
                dataSourceFactory, extractorsFactory, null, null);
        // Prepare
        player.prepare(videoSource);
    }


    private void initView() {
        button9 = (Button) findViewById(R.id.button9);
        simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.simpleExoPlayerView);
        button9.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button9:
                startPlayer();
                break;
        }
    }

    private void startPlayer() {
        simpleExoPlayerView.setPlayer(player);
        player.setPlayWhenReady(true);
    }

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

        if (player != null&& player.getCurrentPosition()>0) {
          
            player.setPlayWhenReady(true);
            player.seekTo(resumePosition);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player != null && player.getPlayWhenReady()) {
            resumeWindow = player.getCurrentWindowIndex();
            resumePosition = Math.max(0, player.getContentPosition());
            player.setPlayWhenReady(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //释放播放器
        if (player != null) {
            player.stop();
            player.release();
            player = null;
        }
    }
}

布局




    

参考

Android ExoPlayer实现网络URL、本地视频播放

ExoPlayer开发指南(官方文档翻译)

。。。

你可能感兴趣的:(安卓Media)