Wednesday 6 July 2011

Video streaming using RTSP in android

public class VideoViewDemo extends Activity {
private static final String TAG = "VideoViewDemo";
private VideoView mVideoView;
private EditText mPath;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.path);
//mPath.setText("http://daily3gp.com/vids/747.3gp");
mPath.setText("rtsp://v3.cache1.c.youtube.com/CjgLENy73wIaLwkEj-R-63EflhMYESARFEIJbXYtZ29vZ2xlSARSB3JlbGF0ZWRgvJG0icWC58hNDA==/0/0/0/video.3gp");

mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
}
});
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
System.out.println("path "+path);
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {


System.out.println("else ");
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
System.out.println("mVideoView.start() ");

mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoURI(Uri.parse(path));
mVideoView.start();
mVideoView.requestFocus();
}
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}