android SurfaceView 视频播放器

package com.forcetech.android.hotel;


import java.net.URL;


import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver.OnTouchModeChangeListener;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;


public class FullScreenActivity extends Activity implements SurfaceHolder.Callback{
private MediaPlayer mediaPlayer;
private SurfaceView surfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.fullscreen);
super.onCreate(savedInstanceState);
surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
mediaPlayer = new MediaPlayer();
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(500, 500);

surfaceView.getHolder().setKeepScreenOn(true);


surfaceView.getHolder().addCallback(this);


}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

startActivity(new Intent(FullScreenActivity.this, HotelActivity.class));
return super.dispatchKeyEvent(event);
}
private void play(int position) {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource("http://f3.3g.56.com/15/15/JGfMspPbHtzoqpzseFTPGUsKCEqMXFTW_smooth.3gp");
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceView.getHolder());
mediaPlayer.setScreenOnWhilePlaying(true);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
Toast.makeText(FullScreenActivity.this, "开始播放", 2).show();
}
});


} catch (Exception e) {
e.printStackTrace();
}
}




@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
play(0);

}




@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

}


@Override
protected void onPause() {
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
}
super.onPause();
}


}

你可能感兴趣的:(SurfaceView)