編輯:關於android開發
在前面的項目中添加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(); } } }
【Android 系統開發】使用 Source InSight 閱讀 Android 源碼 1. 安裝 Source Insight (1) Source Insight
RecyclerView,androidrecyclerview1.簡介 RecyclerView是一種新的視圖組,目標是為任何基於適配器的視圖提供相似的渲染方式。它
andriod 資源文件之存取操作,andriod資源文件來自:http://blog.csdn.net/jianghuiquan/article/details/856
Android Activity生命周期與啟動模式,androidactivityActivity的完整生命周期如下圖: Activity的加載模式有四種: stand
Android開發之Menu:OptionMenu(選項菜單)、Cont