(视频播放)默认播放 ,VideoView, MediaPlay
默认播放器
image.png
VideoView
image.png
MediaPlayer
image.png
image.png
默认系统:
MainActivity*******************
···
// SD卡 系统播放 对SD卡操作 加权限
findViewById(R.id.bt_1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Environment.getExternalStorageDirectory() 获取sd卡的路径
//Toast.makeText(MainActivity.this, Environment.getExternalStorageDirectory()+"", Toast.LENGTH_SHORT).show();
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath()+"/gaobaiqiqiu.mp4"));
//网络
//Uri uri = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/mp4");
startActivity(intent);
}
});
···
VideoView :
布局中定义一个VideoView控件
XML
···
<Button
android:id="@+id/bt_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放视频" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<VideoView
android:id="@+id/video_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
···
MainActivtiy中:
···
videoView = (VideoView) findViewById(R.id.video_id);
findViewById(R.id.bt_2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//1.播放资源路径
// Uri uri = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/gaobaiqiqiu.mp4");
//2.给videoView设置路径
videoView.setVideoURI(uri);
//设置进度条
videoView.setMediaController(new MediaController(MainActivity.this));
videoView.start();
}
});
···
MediaPlayer
布局中定义一个SurfaceView
···
<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="${relativePackage}.${activityClass}" >
<SurfaceView
android:id="@+id/sv_id"
android:layout_width="match_parent"
android:layout_height="400dp"
/>
<Button
android:id="@+id/bt_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放" />
<Button
android:id="@+id/bt_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停" />
</LinearLayout>
···
MainActivity中
···
package com.example.ytest7_mediaplayer;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private SurfaceView sv;
private SurfaceHolder holder;
private int currentPosition;
private MediaPlayer mediaPlayer;
private boolean play=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sv = (SurfaceView) findViewById(R.id.sv_id);
holder = sv.getHolder();
mediaPlayer = new MediaPlayer();
holder.addCallback(new Callback() {
@Override//SurfaceView创建
public void surfaceCreated(SurfaceHolder holder) {
Log.e("TAG", "surfaceCreated");
mediaPlayer.setDisplay(holder);//开启画面
mediaPlayer.start();
mediaPlayer.seekTo(currentPosition);//滑动到上次记录的位置
// if (play==false) {
// //创建mediaPlayer
// mediaPlayer = new MediaPlayer();
// mediaPlayer.reset();
// try {
// mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/gaobaiqiqiu.mp4");
// //准备
// mediaPlayer.prepare();
// mediaPlayer.setDisplay(holder);//开启画面
// mediaPlayer.start();
//
// } catch (Exception e) {
// e.printStackTrace();
// }
// play=true;
//
// }else{
// mediaPlayer.setDisplay(holder);//界面
// mediaPlayer.start();
// mediaPlayer.seekTo(currentPosition);
// }
}
@Override//SurfaceView发生改变
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.e("TAG", "surfaceChanged");
}
@Override //SurfaceView销毁时
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e("TAG", "surfaceDestroyed");
if (mediaPlayer!=null) {
mediaPlayer.pause();//可以继续播放
currentPosition = mediaPlayer.getCurrentPosition();//记录当前播放的位置
}
}
});
//播放
findViewById(R.id.bt_1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (play==false) {
//创建mediaPlayer
mediaPlayer = new MediaPlayer();
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/gaobaiqiqiu.mp4");
//准备
mediaPlayer.prepare();
mediaPlayer.setDisplay(holder);//开启画面
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
play=true;
}else{//继续播放时
mediaPlayer.setDisplay(holder);//界面
mediaPlayer.start();
mediaPlayer.seekTo(currentPosition);
}
}
});
//暂停
findViewById(R.id.bt_2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer!=null) {
mediaPlayer.pause();//可以继续播放
currentPosition = mediaPlayer.getCurrentPosition();
}
}
});
}
}
···