录制时流式传输 MediaRecorder 文件

发布于 2024-10-11 05:49:38 字数 1825 浏览 5 评论 0原文

我正在尝试从 A 点(2.1 android 手机)到 B 点(我的服务器)实时获取视频流。我该怎么做?下面详细介绍了我的尝试(有点长,但简洁!)


目标是在不暂停/停止流的情况下用手机录制一小时长的视频到服务器。最多几分钟的延迟不是问题。我尝试了三种方法来

  1. 线程化 FileInputStream 来读取 视频的文件描述符 目的地
  2. 调用 MediaRecoder.setOutputFile “发送者”套接字的 FD。这 套接字连接到一个 LocalSocketServer,其目的地 是一个“接收器”套接字。
  3. 打开到我的服务器的套接字并 不幸的是,给 setOutputFile 它的 FD

两次尝试都失败了。

  1. 每当 i 时仅注册 24 个字节 调用 FileInputStream.available(), 和实际字节数 在我调用 Recorder.stop()
  2. 后,我得到了这个非常无用的结果 堆栈跟踪

    错误/AndroidRuntime(18532):导致:java.lang.RuntimeException:启动失败。 错误/AndroidRuntime(18532):在android.media.MediaRecorder.start(本机方法) 错误/AndroidRuntime(18532):位于 com.example.demovideo.DemoVideo.initializeCamera(...) ...

  3. 相同的错误 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
  1. 正确保存未损坏的视频 到 SD
  2. 如果我使用的话, 可以工作 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

  1. thread a FileInputStream that reads from the
    FileDescriptor of the video
    destination
  2. call MediaRecoder.setOutputFile on
    the FD of a 'sender' socket. this
    socket connects to a
    LocalSocketServer, whose destination
    is a 'receiver' socket.
  3. open a socket to my server and
    giving setOutputFile its FD

unfortunately both attempts have failed.

  1. Only registers 24 bytes whenever i
    call FileInputStream.available(),
    and the actual number of bytes only
    after i call Recorder.stop()
  2. 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(...)
    ...

  3. 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
  1. properly saves an uncorrupted video
    to sd
  2. works if I use
    setOutputFile(pathToFile) instead.
    the sockets also work when i run
    sender.getOutputStream().write(message.getBytes());

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

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

发布评论

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

评论(1

十年九夏 2024-10-18 05:49:38
  1. 我用这种方法创建了一个移动到服务器的视频流应用程序,它成功了。所以这应该是正确的方法。后来,当我不再参与该项目时,我收到报告称,这种方法不适用于某些较新的手机,尤其是三星 Galaxy S。问题在于,这些手机很少刷新视频数据,可能每分钟刷新一次。您使用什么手机来测试这个?

  2. & 3. MediaRecorder 是本机库的包装器。我假设这个库需要一个具体的文件来写入而不是管道。在文件系统级别文件&管道看起来相同,但不能随机访问管道(查找)。

  1. 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?

  2. & 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).

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