Live-stream video from one android phone to another over WiFi

前端 未结 4 1558
北海茫月
北海茫月 2020-11-29 14:53

I have searched the internet for days now on how to implement a video streaming feature from an android phone to another android phone over a WiFi connection but I can\'t se

相关标签:
4条回答
  • 2020-11-29 15:04

    You can use IP Webcam, or perhaps use DLNA. For example Samsung devices come with an app called AllShare which can share and access DLNA enabled devices on the network. I think IP Webcam is your best bet, though. You should be able to open the stream it creates using MX Video player or something like that.

    0 讨论(0)
  • 2020-11-29 15:09

    If you do not need the recording and playback functionality in your app, using off-the-shelf streaming app and player is a reasonable choice.

    If you do need them to be in your app, however, you will have to look into MediaRecorder API (for the server/camera app) and MediaPlayer (for client/player app).

    Quick sample code for the server:

    // this is your network socket
    ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);
    mCamera = getCameraInstance();
    mMediaRecorder = new MediaRecorder();
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    // this is the unofficially supported MPEG2TS format, suitable for streaming (Android 3.0+)
    mMediaRecorder.setOutputFormat(8);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    mediaRecorder.setOutputFile(pfd.getFileDescriptor());
    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
    mMediaRecorder.prepare();
    mMediaRecorder.start();
    

    On the player side it is a bit tricky, you could try this:

    // this is your network socket, connected to the server
    ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setDataSource(pfd.getFileDescriptor());
    mMediaPlayer.prepare();
    mMediaPlayer.start();
    

    Unfortunately mediaplayer tends to not like this, so you have a couple of options: either (a) save data from socket to file and (after you have a bit of data) play with mediaplayer from file, or (b) make a tiny http proxy that runs locally and can accept mediaplayer's GET request, reply with HTTP headers, and then copy data from the remote server to it. For (a) you would create the mediaplayer with a file path or file url, for (b) give it a http url pointing to your proxy.

    See also:

    Stream live video from phone to phone using socket fd

    MediaPlayer stutters at start of mp3 playback

    0 讨论(0)
  • 2020-11-29 15:09

    I did work on something like this once, but sending a video and playing it in real time is a really complex thing. I suggest you work with PNG's only. In my implementation What i did was capture PNGs using the host camera and then sending them over the network to the client, Which will display the image as soon as received and request the next image from the host. Since you are on wifi that communication will be fast enough to get around 8-10 images per-second(approximation only, i worked on Bluetooth). So this will look like a continuous video but with much less effort. For communication you may use UDP sockets(Faster and less complex) or DLNA (Not sure how that works).

    0 讨论(0)
  • 2020-11-29 15:26

    You can check the android VLC it can stream and play video, if you want to indagate more, you can check their GIT to analyze what their do. Good luck!

    0 讨论(0)
提交回复
热议问题