public class VideoActivity extends Activity {
private static final String TAG = "VideoActivity";
private EditText filenameText;
private SurfaceView surfaceView;
private MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  ButtonClickListener listener = new ButtonClickListener();
  ImageButton playButton = (ImageButton) this.findViewById(R.id.play);
  playButton.setOnClickListener(listener);
  ImageButton pauseButton = (ImageButton) this.findViewById(R.id.pause);
  pauseButton.setOnClickListener(listener);
  ImageButton resetButton = (ImageButton) this.findViewById(R.id.reset);
  resetButton.setOnClickListener(listener);
  ImageButton stopButton = (ImageButton) this.findViewById(R.id.stop);
  stopButton.setOnClickListener(listener);
  // 重點這個個地方需要設定回調函數 繼承Callback類實現三個方法
  surfaceView.getHolder().addCallback(new myCallback());
  filenameText = (EditText) this.findViewById(R.id.filename);
  surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
  surfaceView.getHolder() .setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  // 設定這個大小
  surfaceView.getHolder().setFixedSize(176, 144);
  mediaPlayer = new MediaPlayer();
}
private class myCallback implements android.view.SurfaceHolder.Callback {
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) {
   // TODO Auto-generated method stub
  }
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
   // TODO Auto-generated method stub
  }
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
   // TODO Auto-generated method stub
  }
}
//當來電的時候需要停止播放任務
@Override
protected void onPause() {
  if (mediaPlayer.isPlaying())
   mediaPlayer.pause();
  super.onPause();
}
@Override
protected void onResume() {
  if (mediaPlayer.isPlaying())
   mediaPlayer.pause();
  super.onResume();
}
@Override
protected void onDestroy() {
  if (mediaPlayer.isPlaying())
   mediaPlayer.stop();
  //  釋放資源
  mediaPlayer.release();
  super.onStop();
}
private class ButtonClickListener implements View.OnClickListener {
  @Override
  public void onClick(View v) {
   try {
    String filename = filenameText.getText().toString();
    switch (v.getId()) {
    case R.id.play:
     // 得到文件名稱 
     String filenames = filenameText.getText().toString();
     // 第一步給對象進行重設
     mediaPlayer.reset();
     // 設定串流的類型
     mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
     // 設定資料來源
     mediaPlayer.setDataSource("/sdcard/" + filenames); // 設定要播放的視訊
     // 把視訊輸出到顯示物件上
     mediaPlayer.setDisplay(surfaceView.getHolder());
     // 播放時前需要緩衝
     mediaPlayer.prepare();
     mediaPlayer.start();
     break;
    case R.id.pause:
     if (mediaPlayer.isPlaying()) {
      mediaPlayer.pause();
     } else {
      mediaPlayer.start();
     }
     break;
    case R.id.reset:
     if (!mediaPlayer.isPlaying()) {
      mediaPlayer.start();
     }
     // 跳回到起始的地方重新播放
     mediaPlayer.seekTo(0);
     break;
    case R.id.stop:
     if (mediaPlayer.isPlaying()) {
      mediaPlayer.stop();
     }
     break;
    }
   } catch (Exception e) {
    Log.e(TAG, e.toString());
   }
  }
}
}

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()