您的位置:首页 > 移动开发 > Android开发

Android3种播放视频功能

2017-10-23 12:03 316 查看

使用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");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐