概述
public class MainActivity extends Activity implementsOnClickListener {privateEditText et_path;privateButton bt_play, bt_replay, bt_pause, bt_stop;privateSurfaceView sv;privateSeekBar sb;privateMediaPlayer mediaPlayer;private intcurrentPosition;privateBoolean isplaying;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sv=(SurfaceView) findViewById(R.id.sv);
et_path=(EditText) findViewById(R.id.et_path);
bt_play=(Button) findViewById(R.id.bt_play);
bt_replay=(Button) findViewById(R.id.bt_replay);
bt_pause=(Button) findViewById(R.id.bt_pause);
bt_stop=(Button) findViewById(R.id.bt_stop);
sb=(SeekBar) findViewById(R.id.sb);
sb.setOnSeekBarChangeListener(newOnSeekBarChangeListener() {
@Overridepublic voidonStopTrackingTouch(SeekBar seekBar) {int process =seekBar.getProgress();if(mediaPlayer != null &&mediaPlayer.isPlaying()) {
mediaPlayer.seekTo(process);
}
}
@Overridepublic voidonStartTrackingTouch(SeekBar seekBar) {//TODO Auto-generated method stub
}
@Overridepublic void onProgressChanged(SeekBar seekBar, intprogress,booleanfromUser) {//TODO Auto-generated method stub}
});
bt_pause.setOnClickListener(this);
bt_play.setOnClickListener(this);
bt_replay.setOnClickListener(this);
bt_stop.setOnClickListener(this);
sv.getHolder().addCallback(newCallback() {
@Overridepublic voidsurfaceDestroyed(SurfaceHolder holder) {
System.out.println("holder被销毁了");if (mediaPlayer != null &&mediaPlayer.isPlaying()) {
currentPosition=mediaPlayer.getCurrentPosition();
stop();
}
}
@Overridepublic voidsurfaceCreated(SurfaceHolder holder) {
System.out.println("holder被创建了");if (currentPosition > 0) {
play(currentPosition);
}
}
@Overridepublic void surfaceChanged(SurfaceHolder holder, intformat,int width, intheight) {
System.out.println("holder的大小变化了");
}
});
}
@Overridepublic voidonClick(View v) {switch(v.getId()) {caseR.id.bt_play:
play(0);break;caseR.id.bt_replay:
replay();break;caseR.id.bt_stop:
stop();break;caseR.id.bt_pause:
pause();break;default:break;
}
}/*** 暂停音乐*/
private voidpause() {if ("继续".equals(bt_pause.getText().toString().trim())) {
mediaPlayer.start();
bt_pause.setText("暂停");return;
}if (mediaPlayer != null &&mediaPlayer.isPlaying()) {
mediaPlayer.pause();
bt_pause.setText("继续");return;
}
}/*** 重新播放*/
private voidreplay() {if (mediaPlayer != null &&mediaPlayer.isPlaying()) {
mediaPlayer.seekTo(0);return;
}
play(0);
}/*** 停止播放音乐*/
private voidstop() {if (mediaPlayer != null &&mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();//记得释放资源
mediaPlayer = null;
bt_play.setEnabled(true);
isplaying= false;
}
}/*** 播放音乐*/
private void play(final intcurrentPosition) {
String path=et_path.getText().toString().trim();try{
mediaPlayer= newMediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);/*设置Video影片以SurfaceHolder播放*/mediaPlayer.setDisplay(sv.getHolder());
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();//might take long! (for buffering, etc)//mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(newOnPreparedListener() {
@Overridepublic voidonPrepared(MediaPlayer mp) {
mediaPlayer.start();int max =mediaPlayer.getDuration();
sb.setMax(max);
mediaPlayer.seekTo(currentPosition);newThread() {public voidrun() {
isplaying= true;while(isplaying) {int position =mediaPlayer.getCurrentPosition();
sb.setProgress(position);try{
Thread.sleep(500);
}catch(InterruptedException e) {//TODO Auto-generated catch blocke.printStackTrace();
}
}
};
}.start();
}
});
mediaPlayer.setOnCompletionListener(newOnCompletionListener() {
@Overridepublic voidonCompletion(MediaPlayer mp) {
bt_play.setEnabled(true);
}
});
bt_play.setEnabled(false);
}catch(Exception e) {
Toast.makeText(this, "播放失败", 0).show();
e.printStackTrace();
}
}
}
最后
以上就是大力大船为你收集整理的android 关闭视播放器,Android 视频播放器进度的处理的全部内容,希望文章能够帮你解决android 关闭视播放器,Android 视频播放器进度的处理所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复