Live Video Stream between two Android Phones

Will W picture Will W · Mar 13, 2011 · Viewed 10.8k times · Source

I am currently working on video streaming between two Android Phone. I wrote an application which is able to record the video to the sd file (Using MediaRecorder); and I wrote another application which is able to display the video of the file. Both applications work perfectly.

I found a website about "Broadcasting video with Android - without writing to local files" in following website. It is exactly what I wanted to do.

http://www.mattakis.com/blog/kisg/20090708/broadcasting-video-with-android-without-writing-to-the-file-system

I modified my code.

For the video recorder, it is:

socket=severSocket.accept();
ParcelFileDescriptor=pfd;
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
recorder.setVideoFrameRate(15);
recorder.setVideoSize(320, 240);
recorder.setPreviewDisplay(holder.getSurface());
pfd = ParcelFileDescriptor.fromSocket(socket);
recorder.setOutputFile(pfd.getFileDescriptor());
recorder.prepare(); 
recorder.start();

For Video Player:

Socket socket = new Socket(IP,PORT);
mMediaPlayer = new MediaPlayer();
pfd = ParcelFileDescriptor.fromSocket(socket);
mMediaPlayer.setDataSource(pfd.getFileDescriptor()); // <-- here is the problem
mMediaPlayer.setDisplay(holder); 
mMediaPlayer.prepare();
mMediaPlayer.setOnBufferingUpdateListener(this);            
mMediaPlayer.setOnCompletionListener(this);            
mMediaPlayer.setOnPreparedListener(this);            
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.start();

Program crush on mMediaPlayer.setDataSource(pfd.getFileDescriptor()); on MediaPlayer I know I didnt setup the DataSource correctly. There must be additional setups for ParcelFileDescriptor to put into MediaPlayer.

Does anyone know how to use ParcelFileDescriptor for MediaPlayer? Any helpful advise or tips would be nice......

Thank You

Will

Answer

user1067958 picture user1067958 · Dec 2, 2011

in the video playing side you must create a welcome socket

ServerSocket welcomeSocket = new ServerSocket(portNumber);
socket soc = welcomeSocket.accept();

and use

mMediaplayer.prepareAsync();

instead of

mMediaplayer.prepare();