如何播放从Android设备传输到服务器的视频?

发布于 2024-12-08 15:59:23 字数 1112 浏览 0 评论 0原文

我有一个将视频/音频从 Android 设备流式传输到服务器的应用程序 流媒体很好,但是当我从 MediaRecorder 保存流媒体数据时,我无法播放文件

Android 代码:

String hostname = "000.000.000.000";
int port = 0000;
Socket socket = null;
try {
socket = new Socket(InetAddress.getByName(hostname), port);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);
MediaRecorder recorder =  new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(pfd.getFileDescriptor());
recorder.start();

服务器端:

Socket userSocket = socket.accept();
//DataInputStream dis;
dis = new  DataInputStream(userSocket.getInputStream());
while(true){
        dis.read(buf , 0 , buf.length);
        saveBufferToFile(buf);
}

现在我使用 FileOutStream .write(); 保存缓冲区;方法但输出文件根本无法播放。

经过研究,我明白我需要在向文件写入数据之前将 mp4 标头添加到文件中,但我不知道该怎么做!?

问候,

I have an application that streaming Video/Audio from Android device to Server
Streaming are fine but when I save the streamed data fro MediaRecoreder I can't play the file

Android code :

String hostname = "000.000.000.000";
int port = 0000;
Socket socket = null;
try {
socket = new Socket(InetAddress.getByName(hostname), port);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);
MediaRecorder recorder =  new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(pfd.getFileDescriptor());
recorder.start();

server side :

Socket userSocket = socket.accept();
//DataInputStream dis;
dis = new  DataInputStream(userSocket.getInputStream());
while(true){
        dis.read(buf , 0 , buf.length);
        saveBufferToFile(buf);
}

Now the I save the buffer using FileOutStream .write(); method but the out put file can't be played at all.

after research I understand that I need to add the mp4 headers to the file before I write the data on it BUT I don't know how to do this !?

Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑咖 2024-12-15 15:59:23

首先,MPEG4 容器不仅仅是视频数据的“标头”。

其次,服务器端代码对我来说看起来不正确。我认为它是Java。具体来说:

dis = new  DataInputStream(userSocket.getInputStream());
while(true){
    dis.read(buf , 0 , buf.length);
    saveBufferToFile(buf);
}

具体来说, DataInputStream.read() 不一定读取缓冲区的整个长度。 JavaDocs 说:

尝试读取 len 个字节,但数量较少
可以读取,可能为零。

我怀疑您正在写入磁盘的文件已损坏。您可以通过比较客户端发送的字节数和文件大小来确认这一点。我的直觉告诉我,文件会大得多。

要修复此问题,您需要不使用 read() 读取的字节,而只将这些字节写入磁盘。缓冲区的其余部分实际上是垃圾,请忽略它。

Firstly, the MPEG4 container is more than just "headers" to video data.

Secondly, the server side code does not look correct to me. I assume it is Java. Specifically:

dis = new  DataInputStream(userSocket.getInputStream());
while(true){
    dis.read(buf , 0 , buf.length);
    saveBufferToFile(buf);
}

Specifically, DataInputStream.read() does not necessarily read the entire length of the buffer. The JavaDocs say:

An attempt is made to read as many as len bytes, but a smaller number
may be read, possibly zero.

I suspect the file you are writing to disk is corrupted. You can confirm this by comparing the number of bytes sent by the client and the size of file. My gut say the file will be significantly larger.

To fix it you'll need to take not of the bytes read in by read(), and only write those to bytes to disk. The rest of the buffer is effectively garbage, ignore it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文