POST Streaming Audio over HTTP/2 in Android

后端 未结 1 1431
南旧
南旧 2021-01-19 19:59

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

相关标签:
1条回答
  • 2021-01-19 20:24

    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<byte[]> or similar.

    0 讨论(0)
提交回复
热议问题