在前面的项目中添加SeekBar
<SeekBar android:id="@+id/sb" android:layout_width="match_parent" android:layout_height="wrap_content" />
相关的代码如下:
public class MainActivity extends Activity implements OnClickListener { private EditText et_path; private Button bt_play, bt_replay, bt_pause, bt_stop; private SurfaceView sv; private SeekBar sb; private MediaPlayer mediaPlayer; private int currentPosition; private Boolean isplaying; @Override protected void onCreate(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(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int process = seekBar.getProgress(); if(mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.seekTo(process); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } }); bt_pause.setOnClickListener(this); bt_play.setOnClickListener(this); bt_replay.setOnClickListener(this); bt_stop.setOnClickListener(this); sv.getHolder().addCallback(new Callback() { @Override public void surfaceDestroyed(SurfaceHolder holder) { System.out.println("holder被销毁了"); if (mediaPlayer != null && mediaPlayer.isPlaying()) { currentPosition = mediaPlayer.getCurrentPosition(); stop(); } } @Override public void surfaceCreated(SurfaceHolder holder) { System.out.println("holder被创建了"); if (currentPosition > 0) { play(currentPosition); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { System.out.println("holder的大小变化了"); } }); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_play: play(0); break; case R.id.bt_replay: replay(); break; case R.id.bt_stop: stop(); break; case R.id.bt_pause: pause(); break; default: break; } } /** * 暂停音乐 */ private void pause() { 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 void replay() { if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.seekTo(0); return; } play(0); } /** * 停止播放音乐 */ private void stop() { if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.stop(); mediaPlayer.release(); // 记得释放资源 mediaPlayer = null; bt_play.setEnabled(true); isplaying = false; } } /** * 播放音乐 */ private void play(final int currentPosition) { String path = et_path.getText().toString().trim(); try { mediaPlayer = new MediaPlayer(); 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(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mediaPlayer.start(); int max = mediaPlayer.getDuration(); sb.setMax(max); mediaPlayer.seekTo(currentPosition); new Thread() { public void run() { isplaying = true; while (isplaying) { int position = mediaPlayer.getCurrentPosition(); sb.setProgress(position); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; }.start(); } }); mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { bt_play.setEnabled(true); } }); bt_play.setEnabled(false); } catch (Exception e) { Toast.makeText(this, "播放失败", 0).show(); e.printStackTrace(); } } }