使用MediaPlayer和SurfaceView播放视频
暂不写
系统自带软件播放视频
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/test.mp4");
//调用系统自带的播放器
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/mp4");
startActivity(intent);
VideoView播放视频
常用方法
activity_main.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="100dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="play"/>
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="pause"/>
<Button
android:id="@+id/replay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="replay"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity implements View.OnClickListener{
private VideoView videoView;
private Button play;
private Button pause;
private Button replay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
replay = (Button) findViewById(R.id.replay);
videoView = (VideoView) findViewById(R.id.videoView);
play.setOnClickListener(this);
pause.setOnClickListener(this);
replay.setOnClickListener(this);
initVideoPath();
}
private void initVideoPath(){
File file = new File(Environment.getExternalStorageDirectory(),"test.mp4");
videoView.setVideoPath(file.getPath());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
if(!videoView.isPlaying()){
videoView.start();
}
break;
case R.id.pause:
if(videoView.isPlaying()){
videoView.pause();
}
break;
case R.id.replay:
if(videoView.isPlaying()){
videoView.resume();
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(videoView != null){
videoView.suspend();
}
}
注意
1、AndroidManifest.xml中增加权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2、手机中打开存储权限
这些要注意的点和播放音频是一样的→传送门
3、initVideoPath方法可以改为以下代码来加载URL视频
private void initVideoPath(){
videoView.setVideoPath("http://flv.bn.netease.com/videolib3/1511/07/CoPrF6333/HD/CoPrF6333-mobile.mp4");
}
getDuration()获取视频长度
Android VideoView中getDuration()方法可以获得视频长度,但是有以下问题:
调用videoView.getDuration()方法返回值为-1
VideoView.start()后调用videoView.getDuration()方法返回值也为-1,
视频开始播放后调用videoView.getDuration()方法返回值为视频的真实“长度”
原因是必须要在视频准备完成开始播放的时候才能拿到时长。如果视频不需要播放,要直接拿到时长的代码如下:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
int duration = videoView.getDuration();
Log.d("TIME",duration+"");
}
});
获取视频第一帧图片
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
//根据url获取缩略图
retriever.setDataSource(videoUrl, new HashMap());
//获得第一帧图片
bitmap = retriever.getFrameAtTime();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} finally {
retriever.release();
}
ImageView.setImageBitmap(bitmap);
视频列表展示
像这种视频列表的展示,后台如果只返回了视频链接,没有返回第一帧图片,不用再去获取第一帧图片当作封面,速度太慢
可以直接使用 Glide 加载视频链接即可