流式MediaRecorder文件,同时记录 [英] stream a MediaRecorder file while recording
问题描述
我想从A点(2.1 Android手机)的视频流实时B点(我的服务器)。我会怎么做呢?详细以下是我尝试(有点长,但简洁!)
我们的目标是让记录的手机到服务器一小时长的视频,而无需暂停/停止流。长达几分钟的延迟是没有问题的。我已经试过三种方法
- 发一个FileInputStream从读取
视频的FileDescriptor
目的地 - 调用MediaRe coder.setOutputFile上
一个发送插槽的FD。本
插座连接到一个
LocalSocketServer,其目的
是一个接收器插座。 - 打开一个socket到我的服务器和
给setOutputFile其FD
不幸的是这两种尝试都失败了。
- 只注册24个字节每当我
调用FileInputStream.available(),
只有实际的字节数
之后我叫Recorder.stop() -
给我这个没用的美丽
堆栈跟踪ERROR / AndroidRuntime(18532):由引起:了java.lang.RuntimeException:启动失败。
ERROR / AndroidRuntime(18532):在android.media.MediaRecorder.start(本机方法)
ERROR / AndroidRuntime(18532):在com.example.demovideo.DemoVideo.initializeCamera(...)
... -
同样的错误2
code段(部分略)
1)
FILEOUT =新的FileOutputStream(pathToFile);
...
recorder.setOutputFile(fileOut.getFD());
录音机。prepare()
recorder.start()//在一个异步线程
FILEIN =的FileInputStream(fileOut.getFD);
同时,(录音){
fos.flush();
Log.w(---,信息bytesAvailable:+ fileIn.available()); //总是返回24
视频下载(1000);
}
2)
//在一个线程
服务器=新LocalServerSocket(SOCKET_ADDRESS);
而(真){
接收机= server.accept();
如果(接收器!= NULL){
输入的InputStream = receiver.getInputStream();
... //处理会去这里
}}发件人=新LocalSocket();
sender.connect(新LocalSocketAddress(SOCKET_ADDRESS));
recorder.setOutputFile(sender.getFileDescriptor());
...
。录音机prepare();
recorder.start(); //< - 错误
- 妥善保存未损坏的视频
到SD - 工作,如果我使用
setOutputFile(pathToFile)代替。
当我运行的插槽中也行sender.getOutputStream()写(message.getBytes());
-
我创建了一个移动到服务器的视频流应用这种方法,它的工作。所以这应该是正确的做法。后来,当我是不是该项目的一部分了我得到的报告,这种做法并未与一些较新的手机的工作 - 特别是三星Galaxy S的问题是,这个手机刷新视频数据的谨慎,只是每分钟一次,也许。什么是您使用手机进行测试呢?
-
&安培; 3. MediaRecorder是围绕本机库的包装。我认为这个库想要一个具体的文件写入到不是管道。在一个文件系统级别的文件和放大器;管道看起来相同,但可以不必管道(求)的随机存取。
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 stacktrace
ERROR/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 run
sender.getOutputStream().write(message.getBytes());
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).
这篇关于流式MediaRecorder文件,同时记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!