1:mediaplayer就是播放视频音频(文件或者流)并且有回调的播放器
播放器状态state讲解:
Idle 闲置状态
End状态
Error状态
Initialized 初始化状态
prepre()调用同步准备进入准备好状态 异步中间多了一部准备中状态 此时准备好会有一个准备好的回调
preparing 准备中状态
直接进入准备好状态
prepred 准备好状态
调用start()进入播放中状态
start 播放中状态
pause 暂停状态 (相当于已准备好状态)
可以直接调用start()进入播放状态
stop 停止状态 (相当于初始化状态)
需要再次prepare
注意点:
1:当mediaplayer刚new出来或者调用reset之后 都会进入闲置状态,
调用release之后 进入end状态,
2:当mediaplayer进入闲置状态 是不可以调用getCurrentPosition()
, getDuration()
, getVideoHeight()
, getVideoWidth()
,
setAudioStreamType(int)
, setLooping(boolean)
, setVolume(float, float)
, pause()
, start()
, stop()
, seekTo(int)
, prepare()
or
prepareAsync()
3:当不再使用mediaplayer的时候,调用release方法释放资源,Resource may include singleton resources such as hardware acceleration components 当mediaplayer进入end状态的时候 他就不可以再被调用了
4:当我们调用new的时候,mediaplayer进入闲置状态 create方法创建的时候 如果success 这个时候 可能直接进入prepred状态
5: 播放失败原因: 分辨率过高,流超时,视频资源格式不对
6:当mediaplayer进入错误状态的时候,这个时候回复mediaplayer应该先调用reset方法,在重新初始化mediaplayer
7:setDataSource()prepare() prepareAsync() 应该将异常抛出来
8:判断mediaplayer是否进入started状态可以使用isPlaying()方法判断
9:已经进入started状态 在此调用start方法没有效果
10:start pause 状态转换可能需要时间,已经处于pause状态再调用pause方法就没有效果。
11:setOnSeekCompleteListener(OnSeekCompleteListener)
.监听seekto回调
可以在paused completed playing prepred状态 也可以在这些状态下获取getCurrentPosition()
进度
12:playback completes 播放结束状态 调用start()可以重新开始 也可以seekTo
13:常用api使用条件限制
getCurrentPosition getVideoHeight getVideoWidth isPlaying setAudioAttributes setAudioStreamType
setLooping setVolume
以上api只要不是在error状态都可以调用
getDuration
{Prepared, Started, Paused, Stopped, PlaybackCompleted} | {Idle, Initialized, Error} |
{Started, Paused, PlaybackCompleted} | {Idle, Initialized, Prepared, Stopped, Error} |
{Initialized, Stopped} | {Idle, Prepared, Started, Paused, PlaybackCompleted, Error} |
{Initialized, Stopped} | {Idle, Prepared, Started, Paused, PlaybackCompleted, Error} |
any | {} |
{Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error} | {} |
{Prepared, Started, Paused, PlaybackCompleted} | {Idle, Initialized, Stopped, Error} |
{Idle} | {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error} |
any | {} |
any | {} |
any | {} |
设置监听器 在任何时刻都可以
setPlaybackParams
{Initialized, Prepared, Started, Paused, PlaybackCompleted, Error} | {Idle, Stopped} |
any | {} |
{Prepared, Started, Paused, PlaybackCompleted} | {Idle, Initialized, Stopped, Error} |
{Prepared, Started, Stopped, Paused, PlaybackCompleted} | {Idle, Initialized, Error} |
surfaceview绘制图案:
1:首先将surfaceview作为一个简单的view添加到一个viewgroup里面。
2:创建并添加回调
SurfaceHolder.Callback imageSurfaceCallback = new SurfaceHolder.Callback()
在不同的回调时机做不同的操作。
3:获取canvas做相应的绘制操作
//注意这个绘制尽量在单线程执行 多线程需要考虑外加同步锁。
canvas = advSurfaceView.getHolder().lockCanvas(null); if (bitmap != null) { Paint paint = new Paint(); paint.setAntiAlias(true); canvas.drawColor(0xffffffff); matrix.postRotate(i*5%360,bitmap.getWidth()/2,bitmap.getHeight()/2); canvas.drawBitmap(bitmap,matrix,paint); }
advSurfaceView.getHolder().unlockCanvasAndPost(canvas);
//设置surfaceview背景是透明的
advSurfaceView.setZOrderOnTop(true); advSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
eg:
setDisplay
{} |
package yinhe.test1; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Looper; import android.util.AttributeSet; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.widget.RelativeLayout; public class PicPlayer extends RelativeLayout { private static final String TAG = "AdvVideoPlayer"; private int mW, mH; private static final int MSG_SHOW_BITMAP = 2; private SurfaceView advSurfaceView; private boolean isImageSurfaceCreated = false; // private RelativeLayout childSurface; private int picRes[] = new int[]{R.mipmap.one,R.mipmap.two,R.mipmap.three,R.mipmap.four, R.mipmap.five,R.mipmap.six,R.mipmap.seven,R.mipmap.eight,R.mipmap.one,R.mipmap.two,R.mipmap.three,R.mipmap.four, R.mipmap.five,R.mipmap.six,R.mipmap.seven,R.mipmap.eight,R.mipmap.one,R.mipmap.two,R.mipmap.three,R.mipmap.four, R.mipmap.five,R.mipmap.six,R.mipmap.seven,R.mipmap.eight,R.mipmap.one,R.mipmap.two,R.mipmap.three,R.mipmap.four, R.mipmap.five,R.mipmap.six,R.mipmap.seven,R.mipmap.eight,R.mipmap.one,R.mipmap.two,R.mipmap.three,R.mipmap.four, R.mipmap.five,R.mipmap.six,R.mipmap.seven,R.mipmap.eight,R.mipmap.one,R.mipmap.two,R.mipmap.three,R.mipmap.four, R.mipmap.five,R.mipmap.six,R.mipmap.seven,R.mipmap.eight,R.mipmap.one,R.mipmap.two,R.mipmap.three,R.mipmap.four, R.mipmap.five,R.mipmap.six,R.mipmap.seven,R.mipmap.eight}; private static Matrix matrix = new Matrix(); private Bitmap bitmap; public void setFlag(boolean flag) { this.flag = flag; } private boolean flag=true; public PicPlayer(Context context) { super(context); initView(context); } public PicPlayer(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } public PicPlayer(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } private void initView(Context context) { // if (childSurface == null) { // childSurface = new RelativeLayout(getContext()); // this.addView(childSurface, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); // mW = getResources().getDisplayMetrics().widthPixels; // mH = getResources().getDisplayMetrics().heightPixels; // Log.d(TAG, "mW:" + mW + ",mH:" + mH); // } advSurfaceView = new SurfaceView(getContext()); advSurfaceView.getHolder().addCallback(imageSurfaceCallback); advSurfaceView.setZOrderOnTop(true); advSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT); addView(advSurfaceView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } private Bitmap getReduceBitmap(Bitmap bitmap, int degree) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); // Matrix matrix = new Matrix(); // float wScale = ((float) w / width); // float hScale = ((float) h / height); // matrix.postScale(wScale, hScale); matrix.postRotate(degree); return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); } private SurfaceHolder.Callback imageSurfaceCallback = new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { isImageSurfaceCreated = true; play(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.d(TAG, "surfaceDestroyed: "); isImageSurfaceCreated = false; if(bitmap!=null&&!bitmap.isRecycled()){ bitmap.recycle(); } } }; public void play() { Log.d(TAG, "play: TYPE_AD_PIC, isImageSurfaceCreated=" + isImageSurfaceCreated); // if (advSurfaceView == null) { // advSurfaceView = new SurfaceView(getContext()); // advSurfaceView.getHolder().addCallback(imageSurfaceCallback); // childSurface.addView(advSurfaceView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); // } if (!isImageSurfaceCreated) { return; } flag=true; new Thread(new Runnable() { @Override public void run() { for (int i = 0;flag; i++) { // if (bitmap != null && !bitmap.isRecycled()) { // bitmap.recycle(); // } SurfaceHolder holder=advSurfaceView.getHolder(); if(bitmap==null){ Resources res = getContext().getResources(); // Drawable drawable = res.getDrawable(picRes[i%picRes.length]);//获取drawable Drawable drawable = res.getDrawable(R.mipmap.ic_launcher); BitmapDrawable bd = (BitmapDrawable) drawable; bitmap = bd.getBitmap(); } if (Thread.currentThread() == Looper.getMainLooper().getThread()) { Log.d(TAG, "onNewResultImpl: filter---current thread is main thread, " + Thread.currentThread()); } else { Log.d(TAG, "onNewResultImpl: filter--current thread is async thread, " + Thread.currentThread()); } if (bitmap != null) { Log.d(TAG, "onNewResultImpl: 000"); //Bitmap dest = getReduceBitmap(bitmap, mW, mH); Log.d(TAG, "onNewResultImpl: 111"); Canvas canvas = null; try { if(advSurfaceView==null){ Log.d(TAG, "run: surfaceview为空"); } canvas = advSurfaceView.getHolder().lockCanvas(null); if (bitmap != null) { Paint paint = new Paint(); paint.setAntiAlias(true); canvas.drawColor(0xffffffff); matrix.postRotate(i*5%360,bitmap.getWidth()/2,bitmap.getHeight()/2); canvas.drawBitmap(bitmap,matrix,paint); } } catch (Exception e) { // TODO: handle exception Log.d(TAG, "onNewResultImpl: e=" + e); } finally { if (canvas != null) { advSurfaceView.getHolder().unlockCanvasAndPost(canvas); } } try { Thread.sleep(80); } catch (InterruptedException e) { e.printStackTrace(); } } } } }).start(); } public void stopRotate(){ flag=false; } public boolean isPlaying(){ return flag; } }