简体   繁体   English

将实时视频流式传输到MediaPLayer

[英]Stream live video to MediaPLayer

I'm trying to implement a video streaming between two devices with Android(API 9+) connected to the same WiFi network. 我正在尝试在连接到同一WiFi网络的Android(API 9+)的两台设备之间实现视频流。 First device operating as a small http server that records the video from the camera using MediaRecorder. 第一台设备作为小型http服务器运行,使用MediaRecorder记录来自摄像机的视频。 The second device is trying to get the data using the method setDataSource from MediaPlayer class. 第二个设备正在尝试使用MediaPlayer类中的setDataSource方法获取数据。

mediaPlayer.setDataSource("http://serverIP:port")

I know that first I need to send the correct header otherwise MediaPlayer will not be able to view the data. 我知道首先我需要发送正确的标头,否则MediaPlayer将无法查看数据。 These are my questions: 这些是我的问题:

  1. How should look correct header sent to the MediaPlayer? 应该如何将正确的标题发送到MediaPlayer?
  2. Is there a simpler solution for streaming video between android devices with API 9+ ? 在使用API​​ 9+的Android设备之间是否有更简单的流视频解决方案?

I will be grateful for all the advice. 我将感激所有的建议。

Edit: 编辑:

Since my questions were too general I will try to be more specific. 由于我的问题太笼统,我会尝试更具体。 Based on this thread: Live-stream video from one android phone to another over WiFi I decided to create a small http server (using ServerSocket). 基于这个线程: 通过WiFi从一个Android手机到另一个Android手机直播视频我决定创建一个小型http服务器(使用ServerSocket)。 When second device trying to connect via MediaPlayer I can send data to it from server using 当第二个设备尝试通过MediaPlayer连接时,我可以使用从服务器向其发送数据

socket.getOutputStream().write(buff, 0, readBytes);

But before sending the data, I must add the header to the response. 但在发送数据之前,我必须将标头添加到响应中。

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("HTTP/1.1 206 Partial Content\r\n");
stringBuilder.append("Content-Type: video/mp4\r\n");
stringBuilder.append("Accept-Ranges: bytes\r\n");
stringBuilder.append("Content-Length: XXXX\r\n\r\n");
socket.getOutputStream().write(stringBuilder.toString().getBytes());

Here is my problem. 这是我的问题。 I do not know what information should be added to the header for the message to be understandable by the MediaPLayer. 我不知道应该在标题中添加哪些信息,以便MediaPLayer可以理解该消息。 At the moment in the logs I see the following messages: 在日志中,我看到以下消息:

I/MediaHTTPConnection: response code = 206
V/MediaPlayer: message received msg=100, ext1=1, ext2=-2147483648
E/MediaPlayer: error (1, -2147483648)

To send headers you have to put them in a Map as follows: 要发送标头,您必须将它们放在Map中,如下所示:

 Map<String, String> headers = new HashMap<>();
 headers.put("Header", "Value");

 media_player.setDataSource(
     getApplicationContext(),
     Uri.parse("http://url:port"),
        headers
 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM