赞
踩
ExoPlayer 是 google 开源的应用级媒体播放器,github源码 → https://github.com/google/ExoPlayer。相较于 MediaPlayer,ExoPlayer 具有以下优点:
ExoPlayer 框架中组件主要有:
(1)添加仓库
在项目 build.gradle 中添加 google(),如下:
- repositories {
- google()
- jcenter()
- }
(2)添加依赖
在模块 build.gradle 中的 dependencies 下添加 ExoPlayer 框架依赖,如下:
implementation 'com.google.android.exoplayer:exoplayer:2.8.1'
(3)添加权限
在 AndroidManifest.xml 中的 manifest 标签(application 同级标签)下添加外部存储读写权限,如下:
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
(4)主类
MainActivity.java
- package com.zhyan8.exoplayer;
-
- import android.net.Uri;
- import android.os.Build;
- import android.os.Bundle;
- import android.support.v7.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.DefaultDataSourceFactory;
-
- public class MainActivity extends AppCompatActivity {
- private PlayerView playerView;
- private SimpleExoPlayer player;
-
- public static final String[] permissions = {
- "android.permission.WRITE_EXTERNAL_STORAGE",
- "android.permission.READ_EXTERNAL_STORAGE"};
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }
-
- private void init() {
- if(Build.VERSION.SDK_INT>=23){
- requestPermissions(permissions,1);
- }
- playerView = (PlayerView) findViewById(R.id.video_view);
- initializePlayer();
- }
-
- private void initializePlayer() {
- player = ExoPlayerFactory.newSimpleInstance(
- new DefaultRenderersFactory(this),
- new DefaultTrackSelector(),
- new DefaultLoadControl());
- playerView.setPlayer(player);
- Uri playUri = Uri.parse("file:///sdcard/Pictures/WeiXin/a.mp4");
- MediaSource mediaSource = buildMediaSource(playUri);
- player.prepare(mediaSource, true, false);
- }
-
- private MediaSource buildMediaSource(Uri uri) {
- DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, "com.example.exoplayerdemo");
- MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
- return videoSource;
- }
- }
(5)布局
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.zhyan8.exoplayer.MainActivity">
-
- <com.google.android.exoplayer2.ui.PlayerView
- android:id="@+id/video_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
-
- </RelativeLayout>
(1)循环播放
- MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
- LoopingMediaSource loopingSource = new LoopingMediaSource(videoSource, 5); // 循环播放 5 次
- player.prepare(loopingSource, true, false);
(2)视频拼接
- MediaSource videoSource1 = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri_a);
- MediaSource videoSource2 = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri_b);
- ConcatenatingMediaSource concatenatedSource = new ConcatenatingMediaSource(videoSource1, videoSource2); // 无缝拼接资源
- player.prepare(concatenatedSource, true, false);
(3)倍速播放
- PlaybackParameters params = new PlaybackParameters(3, 3); // 3 倍速播放,参数1:帧率,参数2:频率
- player.setPlaybackParameters(params);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。