您的位置:首页 > 其它

继承SurfaceView实现视频播放

2011-12-11 16:26 351 查看
代码如下,多多交流。

package com.farben.ams.widget;

import java.io.IOException;

import android.content.Context;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

/**
* 视频播放
* @package com.farben.ams.widget
* @author Keynes Cao
* @create 2011-12-5 下午1:44:45 *
*/
public class VideoSurfaceView extends SurfaceView implements Callback {

private SurfaceHolder    mSurfaceHolder     = null;
//视频地址
private String            path                = null;
private MediaPlayer        mMediaPlayer       = null;
//播放次数
private int             frequency         = 0;
private static int         count             = 0;
//是否循环播放
private boolean loop  = true;
public VideoSurfaceView(Context context) {
super(context);
init();
}
/**
* @param context <see>容器</see>
* @param path <see>视频地址</see>
* <li>默认循环播放</li>
*/
public VideoSurfaceView(Context context,String path) {
this(context);
this.path = path;
}
/**
* @param context <see>容器</see>
* @param path <see>视频地址</see>
* @param frequency <see>播放次数</see>
*/
public VideoSurfaceView(Context context,String path,int frequency) {
this(context);
this.path = path;
this.frequency = frequency;
this.loop = false;

}

public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {}

public void surfaceCreated(SurfaceHolder arg0) {
try {
play();
} catch (IOException e) {
Log.e("VideoSurfaceView",e.getMessage()+"\n"+e);
}
}

public void surfaceDestroyed(SurfaceHolder arg0) {
path = null;
if(mMediaPlayer!=null){
if(mMediaPlayer.isPlaying()){
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
Log.d("VideoSurfaceView","退出视频播放");
}
}
}

/**
* 播放
*/
private void play()throws IOException{
if(mMediaPlayer.isPlaying()){
mMediaPlayer.reset();
}
//设置音乐流
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//设置播放容器
mMediaPlayer.setDisplay(mSurfaceHolder);
//设置播入文件地址
mMediaPlayer.setDataSource(path);
//播放准备
mMediaPlayer.prepare();
//开始播放
mMediaPlayer.start();
}

/**
*初使化
*/
private void init(){
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setFixedSize(getWidth(), getHeight());
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setLooping(isLoop());
//事件
mMediaPlayer.setOnSeekCompleteListener(new OnSeekCompleteListener() {
public void onSeekComplete(MediaPlayer arg0) {
count++;//播放了多少次
Log.d("VideoSurfaceView","已经播放了"+count+"次.");
if(!isLoop()){
if(getFrequency()>0){//预设播放次数是否大于0
if(count>=getFrequency()){//如果已经播放的次数大于等于预设次数则退出;
setLoop(false);
if(mMediaPlayer!=null){
//回收资源
mMediaPlayer.release();
mMediaPlayer = null;
Log.d("VideoSurfaceView","已经播入了"+count+"次.退出.");
}
}
}else{
try {
play();
} catch (IOException e) {
Log.e("TextSurfaceView",e.getMessage()+"\n"+e);
}
}
}
}
});
mMediaPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
mMediaPlayer.reset();
Log.d("VideoSurfaceView","播放中发生错误!");
return true;
}
});
}
/**********************************set get method********************************************/
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public int getFrequency() {
return frequency;
}
public void setFrequency(int frequency) {
this.frequency = frequency;
}
public boolean isLoop() {
return loop;
}
/**
* @param isLoop
* <see>默认为循环播放</see>
*/
public void setLoop(boolean isLoop) {
this.loop = isLoop;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: