Live Video Stream between two Android Phones

10,841

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.

Share:
10,841
Will W
Author by

Will W

Updated on June 07, 2022

Comments

  • Will W
    Will W almost 2 years

    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