gpt4 book ai didi

java - 在 Android 中通过 HTTP/2 发布流媒体音频

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:13:17 25 4
gpt4 key购买 nike

一些背景:

我正在尝试在 Android 应用程序上开发一个与语音相关的功能,用户可以在其中使用语音进行搜索,服务器会在用户说话时发送中间结果(这反过来会更新 UI),并在查询时发送最终结果完全的。由于服务器只接受 HTTP/2 单套接字连接和 Android HTTPUrlConnection doesn't support还没有 HTTP/2,我正在使用 Retrofit2。

我看过this , thisthis但是每个示例都有固定长度的数据,或者可以预先确定大小……音频搜索不是这种情况。

这是我的 POST 方法:

  public interface Service{
@Streaming
@Multipart
@POST("/api/1.0/voice/audio")
Call<ResponseBody> post(
@Part("configuration") RequestBody configuration,
@Part ("audio") RequestBody audio);
}

该方法通过以下方式发送配置文件(包含音频参数 - JSON 结构)和流式音频。 (预期的 POST 请求)

Content-Type = multipart/form-data;boundary=----------------------------41464684449247792368259
//HEADERS
----------------------------414646844492477923682591
Content-Type: application/json; charset=utf-8
Content-Disposition: form-data; name="configuration"
//JSON data structure with different audio parameters.
----------------------------414646844492477923682591
Content-Type: audio/wav; charset=utf-8
Content-Disposition: form-data; name="audio"
<audio_data>
----------------------------414646844492477923682591--

不太确定如何发送流媒体(!!) <audio_data> .我尝试使用 Okio 以这种方式为音频创建多部分(来自:https://github.com/square/okhttp/wiki/Recipes#post-streaming)

public RequestBody createPartForAudio(final byte[] samples){
RequestBody requestBody = new RequestBody() {
@Override
public MediaType contentType() {
return MediaType.parse("audio/wav; charset=utf-8");
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
//Source source = null;
sink.write(samples);

}
};

return requestBody;
}

这当然行不通。这是继续将音频样本写入 ResponseBody 的正确方法吗?我到底应该在哪里打电话 Service.post(config, audio)方法,这样我就不会在每次音频缓冲区中有内容时都发布配置文件。

此外,由于我必须继续发送流式音频,我怎样才能保持同一个 POST 连接打开并且在用户停止说话之前不关闭它?

我基本上是 OkHttp 和 Okio 的新手。如果我遗漏了任何内容或部分代码不清楚,请告诉我,我会上传该 fragment 。谢谢。

最佳答案

您可以使用 Pipe从您的音频线程生成数据并在您的网络线程上使用它。

来自newly-created OkHttp recipe :

/**
* This request body makes it possible for another
* thread to stream data to the uploading request.
* This is potentially useful for posting live event
* streams like video capture. Callers should write
* to {@code sink()} and close it to complete the post.
*/
static final class PipeBody extends RequestBody {
private final Pipe pipe = new Pipe(8192);
private final BufferedSink sink = Okio.buffer(pipe.sink());

public BufferedSink sink() {
return sink;
}

@Override public MediaType contentType() {
...
}

@Override public void writeTo(BufferedSink sink) throws IOException {
sink.writeAll(pipe.source());
}
}

如果您的数据可以作为连续流写入,则此方法最有效。如果不能,您最好使用 BlockingQueue<byte[]> 做类似的事情或类似的。

关于java - 在 Android 中通过 HTTP/2 发布流媒体音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42963370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com