公共类RTSPViewer扩展了Activity实现 MediaPlayer.OnPreparedListener,SurfaceHolder.Callback { final static String USERNAME ="&#34 ;; final static String PASSWORD ="&#34 ;; private String RTSP_URL; // 私人MediaPlayer _mediaPlayer; private SurfaceHolder _surfaceHolder; // 私人SharedPreferences偏好; private String PREFS_NAME =" LoginPrefs";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.setBackgroundDrawableResource(android.R.color.white);
setContentView(R.layout.rtspviewer);
preference = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
surfaceHandling();
}
public void surfaceHandling() {
RTSP_URL = /* "rtsp://192.168.1.6:8554/CAM1"; */getIntent()
.getExtras().getString("CAMERA_IP").toString();
Toast.makeText(getApplicationContext(), "" + RTSP_URL,
Toast.LENGTH_LONG).show();
Log.d("---CAMVIEW---", "" + RTSP_URL);
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
_surfaceHolder = surfaceView.getHolder();
_surfaceHolder.addCallback(this);
_surfaceHolder.setFixedSize(320, 240);
}
@Override
public void surfaceChanged(SurfaceHolder sh, int f, int width, int height) {
Log.v("2", "surfaceChanged");
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder sh) {
// TODO Auto-generated method stub
Log.v("1", "surfaceCreated");
_mediaPlayer = new MediaPlayer();
_mediaPlayer.setDisplay(_surfaceHolder);
Context context = getApplicationContext();
// Map<String, String> headers = getRtspHeaders();
Uri source = Uri.parse(RTSP_URL);
Log.i("headers----->", "Source String:" + source);
try {
// Specify the IP camera's URL and auth headers.
_mediaPlayer.setDataSource(context, source, null/* headers */);
// Begin the process of setting up a video stream.
_mediaPlayer.setOnPreparedListener(this);
_mediaPlayer.prepareAsync();
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
Log.v("nnnnn", "surfaceDestroyed");
// TODO Auto-generated method stub
_mediaPlayer.release();
}
@Override
public void onPrepared(MediaPlayer mp) {
Log.v("0", "onPrepared");
// TODO Auto-generated method stub
// mp.reset();
// mp.start();
// _mediaPlayer.reset();
_mediaPlayer.start();
}
}