欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java制作仿微信视频播放控件

程序员文章站 2024-03-04 08:36:47
此控件继承自 surfaceview,利用 mediaplayer 播放视频。 小视频播放界面 movieplayerview.java import j...

此控件继承自 surfaceview,利用 mediaplayer 播放视频。

小视频播放界面

java制作仿微信视频播放控件

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();
  }
 
}

以上所述就是本文的全部内容了,希望大家能够喜欢。