阳光天下

学无止境-与大家共同进步

导航

继承SurfaceView实现视频播放

Posted on 2011-12-11 16:26  阳光天下  阅读(2738)  评论(6编辑  收藏  举报

代码如下,多多交流。

  1 package com.farben.ams.widget;
2
3 import java.io.IOException;
4
5 import android.content.Context;
6 import android.graphics.PixelFormat;
7 import android.media.AudioManager;
8 import android.media.MediaPlayer;
9 import android.media.MediaPlayer.OnCompletionListener;
10 import android.media.MediaPlayer.OnErrorListener;
11 import android.media.MediaPlayer.OnSeekCompleteListener;
12 import android.util.Log;
13 import android.view.SurfaceHolder;
14 import android.view.SurfaceHolder.Callback;
15 import android.view.SurfaceView;
16
17 /**
18 * 视频播放
19 * @package com.farben.ams.widget
20 * @author Keynes Cao
21 * @create 2011-12-5 下午1:44:45 *
22 */
23 public class VideoSurfaceView extends SurfaceView implements Callback {
24
25 private SurfaceHolder mSurfaceHolder = null;
26 //视频地址
27 private String path = null;
28 private MediaPlayer mMediaPlayer = null;
29 //播放次数
30 private int frequency = 0;
31 private static int count = 0;
32 //是否循环播放
33 private boolean loop = true;
34 public VideoSurfaceView(Context context) {
35 super(context);
36 init();
37 }
38 /**
39 * @param context <see>容器</see>
40 * @param path <see>视频地址</see>
41 * <li>默认循环播放</li>
42 */
43 public VideoSurfaceView(Context context,String path) {
44 this(context);
45 this.path = path;
46 }
47 /**
48 * @param context <see>容器</see>
49 * @param path <see>视频地址</see>
50 * @param frequency <see>播放次数</see>
51 */
52 public VideoSurfaceView(Context context,String path,int frequency) {
53 this(context);
54 this.path = path;
55 this.frequency = frequency;
56 this.loop = false;
57
58 }
59
60 public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {}
61
62 public void surfaceCreated(SurfaceHolder arg0) {
63 try {
64 play();
65 } catch (IOException e) {
66 Log.e("VideoSurfaceView",e.getMessage()+"\n"+e);
67 }
68 }
69
70 public void surfaceDestroyed(SurfaceHolder arg0) {
71 path = null;
72 if(mMediaPlayer!=null){
73 if(mMediaPlayer.isPlaying()){
74 mMediaPlayer.stop();
75 mMediaPlayer.release();
76 mMediaPlayer = null;
77 Log.d("VideoSurfaceView","退出视频播放");
78 }
79 }
80 }
81
82 /**
83 * 播放
84 */
85 private void play()throws IOException{
86 if(mMediaPlayer.isPlaying()){
87 mMediaPlayer.reset();
88 }
89 //设置音乐流
90 mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
91 //设置播放容器
92 mMediaPlayer.setDisplay(mSurfaceHolder);
93 //设置播入文件地址
94 mMediaPlayer.setDataSource(path);
95 //播放准备
96 mMediaPlayer.prepare();
97 //开始播放
98 mMediaPlayer.start();
99 }
100
101 /**
102 *初使化
103 */
104 private void init(){
105 mSurfaceHolder = getHolder();
106 mSurfaceHolder.addCallback(this);
107 mSurfaceHolder.setFixedSize(getWidth(), getHeight());
108 mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
109 mMediaPlayer = new MediaPlayer();
110 mMediaPlayer.setLooping(isLoop());
111 //事件
112 mMediaPlayer.setOnSeekCompleteListener(new OnSeekCompleteListener() {
113 public void onSeekComplete(MediaPlayer arg0) {
114 count++;//播放了多少次
115 Log.d("VideoSurfaceView","已经播放了"+count+"次.");
116 if(!isLoop()){
117 if(getFrequency()>0){//预设播放次数是否大于0
118 if(count>=getFrequency()){//如果已经播放的次数大于等于预设次数则退出;
119 setLoop(false);
120 if(mMediaPlayer!=null){
121 //回收资源
122 mMediaPlayer.release();
123 mMediaPlayer = null;
124 Log.d("VideoSurfaceView","已经播入了"+count+"次.退出.");
125 }
126 }
127 }else{
128 try {
129 play();
130 } catch (IOException e) {
131 Log.e("TextSurfaceView",e.getMessage()+"\n"+e);
132 }
133 }
134 }
135 }
136 });
137 mMediaPlayer.setOnErrorListener(new OnErrorListener() {
138 public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
139 mMediaPlayer.reset();
140 Log.d("VideoSurfaceView","播放中发生错误!");
141 return true;
142 }
143 });
144 }
145 /**********************************set get method********************************************/
146 public String getPath() {
147 return path;
148 }
149 public void setPath(String path) {
150 this.path = path;
151 }
152 public int getFrequency() {
153 return frequency;
154 }
155 public void setFrequency(int frequency) {
156 this.frequency = frequency;
157 }
158 public boolean isLoop() {
159 return loop;
160 }
161 /**
162 * @param isLoop
163 * <see>默认为循环播放</see>
164 */
165 public void setLoop(boolean isLoop) {
166 this.loop = isLoop;
167 }
168
169
170 }