一些背景:
我正试图在android应用程序上开发一个与语音相关的功能,用户可以使用语音进行搜索,而服务器在用户说话时发送中间结果(这反过来会更新用户界面)和查询完成后的最终结果。由于服务器只接受HTTP/2单套接字连接和Android HTTPUrlConnection 不支持 HTTP/2,所以我使用的是Retrofit2。
我看过这、这和这,但是每个例子都有固定长度的数据,或者大小可以事先确定。这不是音频搜索的情况。
下面是我的POST方法:
public interface Service{
@Streaming
@Multipart
@POST("/api/1.0/voice/audio")
Call<ResponseBody> post(
@Part("configuration") RequestBody configuration,
@Part ("audio") RequestBody audio);
}该方法以以下方式发送配置文件(包含音频参数- JSON结构)和流音频。(预期员额请求)
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以这种方式为音频创建多部分(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)方法,这样我就不会每次都在音频缓冲区中发布配置文件。
此外,由于我必须继续发送流音频,我如何保持相同的帖子连接打开而不关闭它直到用户停止说话?
我基本上是OkHttp和Okio的新手。如果我错过了任何东西或部分代码是不清楚的,请让我知道,我会上传那个片段。谢谢。
发布于 2017-03-23 01:11:53
您可能可以使用管道从您的音频线程生成数据,并在网络线程上使用它。
/**
* 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[]>或类似的方法来做类似的事情。
https://stackoverflow.com/questions/42963370
复制相似问题