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

时间:2022-02-21 07:04:35

此控件继承自 SurfaceView,利用 MediaPlayer 播放视频。

小视频播放界面

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

MoviePlayerView.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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();
  }
 
}

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