android 下的一段视屏播放的逻辑只是Activity的一部分逻辑源码
DemoActivity:
public class DemoActivity extends Activity implements OnClickListener {
EditText et_path;
Button bt_start;
Button bt_pause;
Button bt_restart;
Button bt_stop;
SurfaceView sv;
SurfaceHolder holder;
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et_path = (EditText) this.findViewById(R.id.et_path);
bt_start = (Button) this.findViewById(R.id.bt_start);
bt_pause = (Button) this.findViewById(R.id.bt_pause);
bt_restart = (Button) this.findViewById(R.id.bt_restart);
bt_stop = (Button) this.findViewById(R.id.bt_stop);
sv = (SurfaceView) this.findViewById(R.id.sv);
bt_start.setOnClickListener(this);
bt_pause.setOnClickListener(this);
bt_restart.setOnClickListener(this);
bt_stop.setOnClickListener(this);
holder = sv.getHolder();
// 设置缓冲区的内容向holder推送的方式
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(new MyHolderCallback());
}
/**
* surface的声明周期
* @author Administrator
*
*/
private class MyHolderCallback implements SurfaceHolder.Callback{
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
}
@Override
public void onClick(View v) {
String path = et_path.getText().toString().trim();
switch(v.getId()){
case R.id.bt_start:
player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDisplay(holder);
if("".equals(path)){//如果输入的链接为空的话
Toast.makeText(this, "文件为空,请输入文件的链接", Toast.LENGTH_SHORT).show();
return;
}
try {
player.setDataSource(path);
player.prepare();
} catch (Exception e) {
Toast.makeText(this, "文件访问出错", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
player.setOnCompletionListener(new OnCompletionListener(){//当媒体播放是否完成的监听器
@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(DemoActivity.this, "播放完成", 0).show();
player.release();//播放完成的时候,要释放资源
player = null;
bt_start.setEnabled(true);
}
});
player.setOnErrorListener(new OnErrorListener(){
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(DemoActivity.this, "播放完成出现错误", 0).show();
player.release();//出现错误的时候要释放资源
player = null;
bt_start.setEnabled(true);
return false;
}
});
player.start();
bt_start.setEnabled(false);
break;
case R.id.bt_pause:
if(player != null && player.isPlaying()){//如果媒体正在播放的话
player.pause();
bt_pause.setText(">>");
return;
}
if(player != null && ">>".equals(bt_pause.getText())){
player.start();
bt_pause.setText("||");
return;
}
break;
case R.id.bt_restart:
if(player != null && player.isPlaying()){//媒体正在播放的话
player.seekTo(0);
}else{
Toast.makeText(DemoActivity.this,"需要重新博凡", Toast.LENGTH_SHORT).show();
}
break;
case R.id.bt_stop:
if(player != null && player.isPlaying()){//媒体正在播放的话
player.stop();//如果是停止播放一段媒体的话,肯定是要先停止,然后再释放资源
player.release();
player = null;
bt_start.setEnabled(true);//设置开始按钮为可点击
} else{
player.release();
player = null;
bt_start.setEnabled(true);//设置开始按钮为可点击
bt_start.setClickable(true);
}
break;
}
}
}
android 下的一段视屏播放的逻辑,布布扣,bubuko.com
原文:http://blog.csdn.net/yubaoma2014/article/details/27176783