录制时流式传输 MediaRecorder 文件
我正在尝试从 A 点(2.1 android 手机)到 B 点(我的服务器)实时获取视频流。我该怎么做?下面详细介绍了我的尝试(有点长,但简洁!)
目标是在不暂停/停止流的情况下用手机录制一小时长的视频到服务器。最多几分钟的延迟不是问题。我尝试了三种方法来
- 线程化 FileInputStream 来读取 视频的文件描述符 目的地
- 调用 MediaRecoder.setOutputFile “发送者”套接字的 FD。这 套接字连接到一个 LocalSocketServer,其目的地 是一个“接收器”套接字。
- 打开到我的服务器的套接字并 不幸的是,给 setOutputFile 它的 FD
两次尝试都失败了。
- 每当 i 时仅注册 24 个字节 调用 FileInputStream.available(), 和实际字节数 在我调用 Recorder.stop()
后,我得到了这个非常无用的结果 堆栈跟踪
错误/AndroidRuntime(18532):导致:java.lang.RuntimeException:启动失败。 错误/AndroidRuntime(18532):在android.media.MediaRecorder.start(本机方法) 错误/AndroidRuntime(18532):位于 com.example.demovideo.DemoVideo.initializeCamera(...) ...
相同的错误 2
代码片段(省略部分)
1)
fileOut = new FileOutputStream(pathToFile);
...
recorder.setOutputFile(fileOut.getFD());
recorder.prepare()
recorder.start()
// in an Async Thread
fileIn = FileInputStream(fileOut.getFD);
while (recording) {
fos.flush();
Log.w("---", "bytesAvailable: " + fileIn.available()); //always returns 24
Thread.sleep(1000);
}
2)
// in a Thread
server = new LocalServerSocket(SOCKET_ADDRESS);
while (true){
receiver = server.accept();
if (receiver != null){
InputStream input = receiver.getInputStream();
... // processing would go here
} }
sender = new LocalSocket();
sender.connect(new LocalSocketAddress(SOCKET_ADDRESS));
recorder.setOutputFile(sender.getFileDescriptor());
...
recorder.prepare();
recorder.start(); // <- error
- 正确保存未损坏的视频 到 SD
- 如果我使用的话, 可以工作 setOutputFile(pathToFile) 代替。 当我跑步时,插座也可以工作
sender.getOutputStream().write(message.getBytes());
I am trying to get a video stream from point A (2.1 android phone) to point B (my server) in real time. How would I do it? Detailed below are my attempts (a little long, but concise!)
The goal is to get an hour long video recorded with the phone to the server without pausing/stopping the stream. a delay of up to several minutes isn't a problem. I've tried three approaches
- thread a FileInputStream that reads from the
FileDescriptor of the video
destination - call MediaRecoder.setOutputFile on
the FD of a 'sender' socket. this
socket connects to a
LocalSocketServer, whose destination
is a 'receiver' socket. - open a socket to my server and
giving setOutputFile its FD
unfortunately both attempts have failed.
- Only registers 24 bytes whenever i
call FileInputStream.available(),
and the actual number of bytes only
after i call Recorder.stop() gives me this beautifully useless
stacktraceERROR/AndroidRuntime(18532): Caused by: java.lang.RuntimeException: start failed.
ERROR/AndroidRuntime(18532): at android.media.MediaRecorder.start(Native Method)
ERROR/AndroidRuntime(18532): at com.example.demovideo.DemoVideo.initializeCamera(...)
...same error 2
code snippets (parts omitted)
1)
fileOut = new FileOutputStream(pathToFile);
...
recorder.setOutputFile(fileOut.getFD());
recorder.prepare()
recorder.start()
// in an Async Thread
fileIn = FileInputStream(fileOut.getFD);
while (recording) {
fos.flush();
Log.w("---", "bytesAvailable: " + fileIn.available()); //always returns 24
Thread.sleep(1000);
}
2)
// in a Thread
server = new LocalServerSocket(SOCKET_ADDRESS);
while (true){
receiver = server.accept();
if (receiver != null){
InputStream input = receiver.getInputStream();
... // processing would go here
} }
sender = new LocalSocket();
sender.connect(new LocalSocketAddress(SOCKET_ADDRESS));
recorder.setOutputFile(sender.getFileDescriptor());
...
recorder.prepare();
recorder.start(); // <- error
- properly saves an uncorrupted video
to sd - works if I use
setOutputFile(pathToFile) instead.
the sockets also work when i runsender.getOutputStream().write(message.getBytes());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我用这种方法创建了一个移动到服务器的视频流应用程序,它成功了。所以这应该是正确的方法。后来,当我不再参与该项目时,我收到报告称,这种方法不适用于某些较新的手机,尤其是三星 Galaxy S。问题在于,这些手机很少刷新视频数据,可能每分钟刷新一次。您使用什么手机来测试这个?
& 3. MediaRecorder 是本机库的包装器。我假设这个库需要一个具体的文件来写入而不是管道。在文件系统级别文件&管道看起来相同,但不能随机访问管道(查找)。
I created a mobile-to-server video streaming app with this approach and it worked. So this should be the right approach. Later when I was not part of the project anymore I got reports that this approach did not work with some newer phones - most notably Samsung Galaxy S. The problem was that this phones flushed video data sparingly, just once a minute maybe. What phone are you using to test this?
& 3. MediaRecorder is a wrapper around a native library. I assume that this library wants a concrete file to write to not a pipe. On a file-system level files & pipes look the same, but one can not have random access to a pipe (seeking).