Some background:
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 whil
You might be able to use a Pipe to produce data from your audio thread and consume it on your networking thread.
From a 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());
}
}
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
or similar.