通过 WiFi 将视频从一部 Android 手机直播到另一部 [英] Live-stream video from one android phone to another over WiFi

查看:39
本文介绍了通过 WiFi 将视频从一部 Android 手机直播到另一部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在互联网上搜索了几天,了解如何通过 WiFi 连接实现从一部安卓手机到另一部安卓手机的视频流功能,但我似乎找不到任何有用的东西.我查看了 android 开发人员的示例代码、stackoverflow、google、android 博客,但一无所获.我所能找到的只是某种用于流式传输的电话到桌面或桌面到电话的解决方案,但在我的实现中我无法借用任何东西.

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 seem to find anything useful. I looked on android developers for sample code, stackoverflow, google, android blogs but nothing. All I can find are some sort of phone-to-desktop or desktop-to-phone solutions for streaming, but nothing that I can borrow in my implementation.

我需要使用 arduino ADK 来控制机器人,所以我使用了 2 部手机,一部将安装在机器人上,另一部将接收来自机器人的视频流.我提到这个是因为我试图在播放时间和观看时间之间实现最小的延迟.

I need to control a robot using an arduino ADK, so I am using 2 phones, one which will be mounted on the robot and another which will receive the video stream from the robot. I am mentioning this because I am trying to achieve the smallest delay between the broadcast time and the viewing time.

我正在编写 2 个应用程序,一个用于控制机器人的主应用程序(来自手持电话),它将控制从属应用程序并接收流,而第二个从属应用程序将在带有机器人的手机上运行,​​控制机器人电机/执行器/流到主应用程序.不幸的是,我无法使用第三方应用程序.我需要将视频流代码集成到我的 2 个应用中.

I am writing 2 apps, one master app to control the robot(from the handheld phone) which will control the slave app and receive the stream, and the second slave app which will run on the robot-strapped phone, controlling the motors/actuators/streaming to master app. I can not use third party apps unfortunately. I need to integrate the video stream code into my 2 apps.

有哪些选择可以实现这一目标?这也是很难做到的,因为我从来没有使用过视频流,我在 Java 和 Android 开发方面都做得很好.我应该如何编码/解码流,如何启动连接,我需要使用 UDP 而不是 TCP/IP 吗?我真的不知道从哪里开始,任何地方都没有示例代码.我很确定这是可以实现的.我只是找不到任何有用的东西来让我朝着正确的方向开始.

What options are there for achieving this? Also is it very hard to do because I never worked with videostreaming, tough I am doing pretty good in both Java and Android development. How should I encode/decode the stream, how do I initiate the connection, will I need to work with UDP instead of TCP/IP ? I really don't know where to start, with no sample code anywhere. I am pretty sure this can be achieved. I just can't find anything useful to get me started in the right direction.

我偶然发现了 spydroid 但它在桌面上使用 VLC 所以它没有对我有好处.

I stumbled across spydroid but it is using VLC on a desktop so its no good for me.

查看 Cagney Moreau 的博客.他详细介绍了如何实现这一点.

Check out Cagney Moreau's blog. He goes into details about implementing this.

推荐答案

如果您的应用不需要录制和播放功能,使用现成的流媒体应用和播放器是一个合理的选择.

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.

如果您确实需要在您的应用中使用它们,则您必须查看 MediaRecorder API(用于服务器/相机应用)和 MediaPlayer(用于客户端/播放器应用).

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();

不幸的是,媒体播放器往往不喜欢这样,所以你有几个选择:(a) 将数据从套接字保存到文件和(在你有一些数据之后)从文件播放媒体播放器,或者 (b) 制作一个在本地运行的小型 http 代理,可以接受媒体播放器的 GET 请求,回复 HTTP 标头,然后将数据从远程服务器复制到它.对于 (a) 您将使用文件路径或文件 url 创建媒体播放器,对于 (b) 为其提供一个指向您的代理的 http url.

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.

另见:

使用流式传输从手机到手机的实时视频套接字 fd

MediaPlayer 在 mp3 播放开始时断断续续

这篇关于通过 WiFi 将视频从一部 Android 手机直播到另一部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆