Live Video Stream between two Android Phones
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.VideoE开发者_如何转开发ncoder.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
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();
Android does not natively support video streaming in Android 2.1 or below. What we did was to get the images frame by frame; and break each flame into BYTE[] and send over using Socket
class. And in receiver's side, we rebuild the images using BYTE[]
data received.
精彩评论