简单的说下一Android的几种视频播放功能:
1、VideoView:最简单的视频播放
<FrameLayout 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=".fragment.HotFragment">
<VideoView
android:id="@+id/vv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
private void initView(View v) {
VideoView videoView = v.findViewById(R.id.vv);
String uri="/storage/emulated/0/Download/APPNEWCARDEMO.mp4";
videoView.setVideoPath(uri);
videoView.start();
}
VideoView全屏:
public class SplashVideoView extends VideoView {
public SplashVideoView(Context context) {
super(context);
}
public SplashVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SplashVideoView(Context context, AttributeSet attrs, int
defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int
heightMeasureSpec) {
//我们重新计算高度
int width = getDefaultSize(0, widthMeasureSpec);
int height = getDefaultSize(0, heightMeasureSpec);
setMeasuredDimension(width, height);
}
@Override
public void
setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
super.setOnPreparedListener(l);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
}
2、SurfaceView+MediaPlayer 播放视频:使用方式
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
private SurfaceView surfaceView;
private MediaPlayer player;
private SurfaceHolder holder;
private void initView2(View v) {
surfaceView = v.findViewById(R.id.surfaceView);
String uri="/storage/emulated/0/Download/APPNEWCARDEMO.mp4";
player = new MediaPlayer();
try {
player.setDataSource(uri);
holder=surfaceView.getHolder();
holder.addCallback(new MyCallBack());
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
player.start();
player.setLooping(true);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
private class MyCallBack implements SurfaceHolder.Callback {
@Override
public void surfaceCreated(SurfaceHolder holder) {
player.setDisplay(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
3、TextureView+MediaPlaye 播放视频:使用方式;
<TextureView
android:id="@+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
private TextureView textureView;
private MediaPlayer player;
private void initView(View v) {
textureView = v.findViewById(R.id.textureView);
String uri="/storage/emulated/0/Download/APPNEWCARDEMO.mp4";
player = new MediaPlayer();
try {
player.setDataSource(uri);
textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
player.setSurface(new Surface(surfaceTexture));
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
player.start();
player.setLooping(true);
}
});
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
以上是Android基本的视频播放方式,有什么不同我就先不介绍了,看项目需要;
下面介绍几种主流的视频开源项目:
1、ExoPlyer 谷歌的开源框架,使用方式:
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
private void initializePlayer(View v) {
playerView = (v.findViewById(R.id.video_view));
// 创建带宽
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
// 创建轨道选择工厂
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
// 创建轨道选择实例
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
// 创建播放器实例
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(getActivity(), trackSelector);
playerView.setPlayer(player);
// 创建加载数据的工厂
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(getActivity(), Util.getUserAgent(getActivity(), "MyApplication"), null);
// 创建资源
Uri uri = Uri.parse("/storage/emulated/0/Download/APPNEWCARDEMO.mp4");
ExtractorMediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
player.prepare(mediaSource, false, true);
player.setPlayWhenReady(true);
}
ExoPlayer项目地址:https://github.com/google/ExoPlayer
2、JiaoZiVideoPlayer 视频播放、使用方式;
<cn.jzvd.JzvdStd
android:id="@+id/videoplayer"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
JzvdStd jzvdStd = (JzvdStd) findViewById(R.id.videoplayer);
jzvdStd.setUp("http://jzvd.nathen.cn/c6e3dc12a1154626b3476d9bf3bd7266/6b56c5f0dc31428083757a45764763b0-5287d2089db37e62345123a1be272f8b.mp4"
, "视频标题", Jzvd.SCREEN_WINDOW_NORMAL);
jzvdStd.thumbImageView.setImage("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"); //占位图
饺子饺子播放器开源地址:https://github.com/lipangit/JiaoZiVideoPlayer