在 Android 中通过 HTTP/2 POST 流式音频 [英] POST Streaming Audio over HTTP/2 in Android

查看:25
本文介绍了在 Android 中通过 HTTP/2 POST 流式音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景:

我正在尝试在 android 应用程序上开发与语音相关的功能,用户可以使用语音进行搜索,服务器在用户说话时发送中间结果(这反过来更新 UI)和查询时的最终结果完全的.由于服务器只接受 HTTP/2 单套接字连接和 Android HTTPUrlConnection 尚不支持 HTTP/2,我正在使用 Retrofit2.

I am trying to develop a voice-related feature on the android app where a user can search using voice and the server sends intermediate results while user is speaking (which in turn updates the UI) and the final result when the query is complete. Since the server accepts only HTTP/2 single socket connection and Android HTTPUrlConnection doesn't support HTTP/2 yet, I am using Retrofit2.

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

I have looked at this, this and this but each example has fixed length data or the size can be determined beforehand... which is not the case for audio search.

以下是我的 POST 方法:

Here's what my method for POST looks like:

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

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

The method sends configuration file(containing audio parameters - JSON structure) and streaming audio in the following manner. (Expected POST request)

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--

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

Not really sure about how to send streaming(!!) <audio_data> . I tried using Okio to create multipart for audio in this way (From: 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) 方法,这样我就不会每次音频缓冲区中有东西时都发布配置文件.

This didn't work of course. Is this a right way to keep on writing audio samples to ResponseBody? Where exactly should I call Service.post(config, audio) method so that I don't end up posting configuration file every time there is something in the audio buffer.

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

Also, since I have to keep on sending streaming audio, how can I keep the same POST connection open and not close it until user has stopped speaking?

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

I am basically new to OkHttp and Okio. If I have missed anything or part of the code is not clear please let me know and I'll upload that snippet. Thank you.

推荐答案

您也许可以使用 管道从您的音频线程生成数据并在您的网络线程上使用它.

You might be able to use a Pipe to produce data from your audio thread and consume it on your networking thread.

来自 如果您的数据可以作为连续流写入,这种方法将最有效.如果不能,你最好用 BlockingQueue 或类似的东西做类似的事情.

This approach will work best if your data can be written as a continuous stream. If it can’t, you might be better off doing something similar with a BlockingQueue<byte[]> or similar.

这篇关于在 Android 中通过 HTTP/2 POST 流式音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆