java制作仿微信视频播放控件
程序员文章站
2024-03-31 17:02:10
此控件继承自 surfaceview,利用 mediaplayer 播放视频。
小视频播放界面
movieplayerview.java
import j...
此控件继承自 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(); } }
以上所述就是本文的全部内容了,希望大家能够喜欢。
下一篇: java实现插入排序算法