android 在线视频播放器实现方法

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());
   }
  }
}
}

za

你可能感兴趣的:(android)