1:VideoView
video = (VideoView)findViewById(R.id.videoView);
video.setVideoURI(Uri.fromFile(new File("/storage/emulated/0/video/banana.mp4")));
video.setMediaController(new MediaController(this));
video.start();
<VideoView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/videoView"
/>
Video
// video.getCurrentPosition();
// video.getDuration();
// video.seekTo(0);
// video.start();
// video.pause();
2: 网络请求:
video.setVideoURI(Uri.parse("http://qiubai-video.qiushibaike.com/91B2TEYP9D300XXH_3g.mp4"));
<uses-permission android:name="android.permission.INTERNET"/>
其实VideoView 是封装了MediaPlayer。
----------------------------------------------------
其实VideoView 就是MediaPlayer 和SurfaceView 来做的。
其实是视频就是音频和图像的结合。所以可以理解为MediaPlayer和SurfaceView 来做的。
----------------------------------------------------
3:SurfaceView主要用于两个方面,用于游戏,用于视频的播放。
摄像头也是相当于视频播放,只是视频的来源不同。
SurfaceView 也可以用来进行绘制,但是绘制的效率比普通的View 要高,
但是比GLSurfaceView的绘制效率要低。
GL主要用于3D的绘制的。
现在SurfaceView 只是用于进行视频的播放
使用SurfaceView 做一个画板
可以在子线程中 绘制 。
------------------------------------------------------------------
各自有两个优点,又有两个缺点。所以Surface
SurfaceView
View
绘制效率高
绘制效率低
可以在子线程中绘制
只能在UI线程中绘制。
不可配置
自定义的View 可以有自定义的属性的,是可配置的。
复用性弱
复用性强,可以直接拷贝过去用。
-------------------------------------------------------------------
4:Holder 的翻译就是持有人的意思
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceView.getHolder().addCallback(this);
继承3个方法:
@Override
public void surfaceCreated(SurfaceHolder holder) {
//例如游戏开始之后开始处理线程。
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//一开始的时候就会调用一次。宽和高有可能在运行当中发生改变,一旦改变了,例如横竖屏切换,调用这个方法
//我们可以知道新的宽高是什么。
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
使用SurfaceView 做一个画板,可以在子线程中 绘制 。子线程中绘制,子线程中绘制。
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private VideoView video;
private SurfaceView surfaceView;
private Thread thread;
private DrawRunable runnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// video = (VideoView)findViewById(R.id.videoView);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceView.getHolder().addCallback(this);
runnable = new DrawRunable(surfaceView.getHolder());
surfaceView.setOnTouchListener(runnable);
// video.setVideoURI(Uri.fromFile(new File("/storage/emulated/0/video/banana.mp4")));
// video.setVideoURI(Uri.parse("http://qiubai-video.qiushibaike.com/91B2TEYP9D300XXH_3g.mp4"));
// video.setMediaController(new MediaController(this));
// video.start();
// video.getCurrentPosition();
// video.getDuration();
// video.seekTo(0);
// video.start();
// video.pause();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// runnable = new DrawRunable(holder);
thread = new Thread(runnable);
thread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
runnable.setRun(false);
}
public static class DrawRunable implements Runnable, View.OnTouchListener {
private SurfaceHolder holder;
private boolean isRun;
private Paint paint;
private Path path;
public DrawRunable(SurfaceHolder holder) {
this.holder = holder;
isRun=true;
paint=new Paint();
path = new Path();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
}
@Override
public void run() {
while(isRun){
Canvas canvas = holder.lockCanvas();//得到锁定的 画布 ,但是 这个 画布 有可能为空
//当SurfaceView没有创建成功的时候有可能为空 。
if(canvas!=null){
canvas.drawColor(0xFF000000);//先用黑色 把这个图全部 染掉
canvas.drawPath(path,paint);
holder.unlockCanvasAndPost(canvas);
}
}
}
public void setRun(boolean run){
isRun=run;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(),event.getY());
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
path.lineTo(event.getX(),event.getY());
break;
}
return true;
// return false;
}
}
}
----------------------------------------------------------------------------------
5:MediaPlayer 和 SurfaceView 一起用:
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private SurfaceView surfaceView;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceView.getHolder().addCallback(this);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("/storage/emulated/0/video/banana.mp4");
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer.setDisplay(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
-----------------------------------------------------------------------------
6:如果是网络加载:
因为是网络所以需要异步加载,而且需要调用onPrepared方法来处理加载完后的start的操作。
mediaPlayer.setOnPreparedListener(this);
同时设置:surfaceView 的大小为视频的原始尺寸大小:
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener {
private SurfaceView surfaceView;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceView.getHolder().addCallback(this);
mediaPlayer = new MediaPlayer();
try {
// mediaPlayer.setDataSource("/storage/emulated/0/video/banana.mp4");
mediaPlayer.setDataSource("http://qiubai-video.qiushibaike.com/91B2TEYP9D300XXH_3g.mp4");
// mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync();
// mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer.setDisplay(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
ViewGroup.LayoutParams layoutParams = surfaceView.getLayoutParams();
layoutParams.height=mediaPlayer.getVideoHeight();
layoutParams.width=mediaPlayer.getVideoWidth();
surfaceView.setLayoutParams(layoutParams);
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
添加了播放和暂停已经停止的按钮:
<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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<SurfaceView android:id="@+id/surfaceView" android:layout_width="fill_parent" android:layout_height="300dp" />
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" >
<ImageButton android:id="@+id/btnplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btnPlay" android:src="@drawable/play" />
<ImageButton android:id="@+id/btnpause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btnPause" android:src="@drawable/pause" />
<ImageButton android:id="@+id/btnstop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/stop" android:onClick="btnStop"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener {
ImageButton btnplay, btnstop, btnpause;
SurfaceView surfaceView;
MediaPlayer mediaPlayer;
int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceView.getHolder().addCallback(this);
mediaPlayer = new MediaPlayer();
try {
//本地的视频 // mediaPlayer.setDataSource("/storage/emulated/0/video/banana.mp4"); // mediaPlayer.prepare(); // mediaPlayer.start(); //网络请求的 mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setDataSource("http://qiubai-video.qiushibaike.com/91B2TEYP9D300XXH_3g.mp4");
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
//下面的如果不设置那么就会没有显示的视频了 mediaPlayer.setDisplay(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
// ViewGroup.LayoutParams layoutParams = surfaceView.getLayoutParams(); // layoutParams.height=mediaPlayer.getVideoHeight(); // layoutParams.width=mediaPlayer.getVideoWidth(); // surfaceView.setLayoutParams(layoutParams); }
public void btnPause(View view) {
mediaPlayer.pause();
}
public void btnPlay(View view) {
mediaPlayer.start();
}
public void btnStop(View view) {
// mediaPlayer.stop(); mediaPlayer.pause();
mediaPlayer.seekTo(0);
}
}
-------------------------------------------------------------------------------