Flutter [只有音频] – 访问正在播放的流链接的原始字节数据

huangapple go评论92阅读模式
英文:

Flutter [Just audio] - Access the raw byte data from a stream link which is being played

问题

我有一个简单的用例,需要能够记录正在播放的直播流,以便在用户请求时倒带它。

我尝试了两种方法:

  1. 从简单的HTTP调用中获取StreamedResponse并将数据写入临时的mp3文件。这种方法可以工作,但问题在于我认为将会有两次对同一流链接的网络调用,因为在底层的某个地方,我假设只有音频需要执行相同的操作。

  2. 打开声音录制器并尝试录制仅从正在播放的音频中录制的原始流。这也可以工作,消除了第一个问题,但由于录制了背景噪音,录制的质量与原始的明显不同。

是否有一种方法可以从仅音频库中获取字节数据的访问权。这将允许我按照第一次尝试的方式进行操作,而无需进行网络调用。我尝试过查看仅音频包的Dart特定实现,但未能找到任何访问点。很有可能我错过了或者不知道如何从现有内容中提取它。

英文:

I've a simple use case where there's a need to be able to record a live stream which is being played with the purpose of rewinding it on the request of a user.

There were 2 attempts which I tried:

  1. Get a StreamedResponse from a simple http call and write the data to a temporary mp3 file. It works but the issue with this approach however is that I believe there'll be 2 network calls to the same stream link since somewhere under the hood I assume just audio will have to do the same

  2. Open a sound recorder and try recording the original stream from just audio which is being played. This works as well eliminating the first issue but with this, background noise is recorded as well due to which the quality of the recording is noticeably different from the original.

Is there a way in which we could get the access the byte data from the just audio library itself. It'll allow me to follow the same approach as the first try, without the network call. I tried going through the dart specific implementation of the just audio package but was not able to find any point of access. It's very much possible that I missed it or am not aware of a method to extract it from what's available.

答案1

得分: 0

一个可能的方法是创建一个自定义的StreamAudioSource子类,然后将其设置为您的音频源。这是一种将自己的字节流输入到just_audio并让其播放的方式。在您的实现中,您需要打开自己的HTTP连接到URL,并在读取响应流时将所有这些字节保存到您自己的文件中,但您还需要将这些字节作为返回值返回到您的StreamAudioSource中,以便将这些相同的字节馈送到just_audio播放器中。这样,字节只会下载一次。

该插件还附带了一个名为LockCachingAudioSourceStreamAudioSource子类的示例,几乎完全符合此要求,但它仅设计和测试用于有限时长的音频。您可以尝试在您的URL上测试它是否有效,如果不起作用,您可以将该类用作实现自己子类的灵感。

英文:

One possibility is to create a custom subclass of StreamAudioSource and then set it as your audio source. This is a way of feeding your own stream of bytes into just_audio and get it to play it. In your implementation, you'll need to open your own HTTP connection to the URL, and as you read the response stream, you'll save a copy of all of those bytes into your own file, but you'll also return those bytes as the return value in your StreamAudioSource so that those same bytes will be fed into the just_audio player. This way, the bytes are only downloaded once.

The plugin also comes with an example subclass of StreamAudioSource called LockCachingAudioSource which does almost exactly this, except it is only designed and tested for finite duration audio. You could test it on your URL to see if it works, or if it doesn't, you could use that class as inspiration for implementing your own subclass.

huangapple
  • 本文由 发表于 2023年6月19日 22:23:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定