java制造仿微信視頻播放控件。本站提示廣大學習愛好者:(java制造仿微信視頻播放控件)文章只能為提供參考,不一定能成為您想要的結果。以下是java制造仿微信視頻播放控件正文
此控件繼續自 SurfaceView,應用 MediaPlayer 播放視頻。
藐視頻播放界面
MoviePlayerView.java
import java.io.IOException; import android.content.Context; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.util.AttributeSet; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; /** * 視頻播放控件 *@author liuyinjun * @date 2015-2-8 */ public class MoviePlayerView extends SurfaceView { private SurfaceHolder mSurfaceHolder; private MediaPlayer mPlayer; public MoviePlayerView(Context context) { this(context, null); } public MoviePlayerView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MoviePlayerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mSurfaceHolder = this.getHolder(); mSurfaceHolder.addCallback(mSurfaceHolderCallback); // holder參加回調接口 mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// setType必需設置,要不失足. } /** * 播放視頻 * @param fileName 文件途徑 */ public void play(String fileName, final OnPlayCompletionListener completionListener) { mPlayer = new MediaPlayer(); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mPlayer.setDisplay(mSurfaceHolder); // 界說一個SurfaceView播放它 mPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer arg0) { stop(); if(completionListener != null) completionListener.onPlayCompletion(); // canvas.drawColor(Color.TRANSPARENT, // PorterDuff.Mode.CLEAR); } }); try { mPlayer.setDataSource(fileName); mPlayer.prepare(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mPlayer.start(); } private void stop() { if (mPlayer != null) { mPlayer.release(); mPlayer = null; } } /** * 釋放資本 */ public void release() { if (mPlayer != null) { mPlayer.release(); mPlayer = null; } } private Callback mSurfaceHolderCallback = new Callback() { @Override public void surfaceDestroyed(SurfaceHolder arg0) { mSurfaceHolder = null; } @Override public void surfaceCreated(SurfaceHolder arg0) { // TODO Auto-generated method stub mSurfaceHolder = arg0; } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub mSurfaceHolder = arg0; } }; /** * 播放勝利回調 *@author liuyinjun * @date 2015-2-9 */ public interface OnPlayCompletionListener{ public void onPlayCompletion(); } }
以上所述就是本文的全體內容了,願望年夜家可以或許愛好。