如何播放从Android设备传输到服务器的视频?
我有一个将视频/音频从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,MPEG4 容器不仅仅是视频数据的“标头”。
其次,服务器端代码对我来说看起来不正确。我认为它是Java。具体来说:
具体来说,
DataInputStream.read()
不一定读取缓冲区的整个长度。 JavaDocs 说:我怀疑您正在写入磁盘的文件已损坏。您可以通过比较客户端发送的字节数和文件大小来确认这一点。我的直觉告诉我,文件会大得多。
要修复此问题,您需要不使用
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:
Specifically,
DataInputStream.read()
does not necessarily read the entire length of the buffer. The JavaDocs say: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.