- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
一些背景:
我正在尝试在 Android 应用程序上开发一个与语音相关的功能,用户可以在其中使用语音进行搜索,服务器会在用户说话时发送中间结果(这反过来会更新 UI),并在查询时发送最终结果完全的。由于服务器只接受 HTTP/2 单套接字连接和 Android HTTPUrlConnection doesn't support还没有 HTTP/2,我正在使用 Retrofit2。
我看过this , this和 this但是每个示例都有固定长度的数据,或者可以预先确定大小……音频搜索不是这种情况。
这是我的 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/
我正在使用 Twitter4j 流 API。我正在使用地理定位约束来仅从特定区域获取推文。这是代码的那部分: twitterStream.addListener(listener);
我正在使用 WCF 并希望将大文件从客户端上传到服务器。我已经调查并决定遵循 http://msdn.microsoft.com/en-us/library/aa717050.aspx 中概述的分块方
我和提问者的情况很相似: Can I serve MP3 files with PHP?基本上我试图保护 mp3 文件不被直接下载,因此用户必须首先通过 php 进行身份验证。这是我的代码: head
我连接了 Red5 oflaDemo 并且工作正常 当我尝试使用 ffmpeg rtmp 流式传输 flv 文件时,我遇到了这个错误 root@zzz-OptiPlex-170L:~# ffmpeg
我正在尝试压缩一堆文件并通过流使数据可用。 我希望内存占用尽可能小。 我的想法是实现一个 Stream,其中我有一堆 FileStream 对象作为数据成员。当我的 Stream 上的 Read 方法
只是想知道是否有人知道自动执行此操作和/或作为 50 mp4 视频的批处理的脚本或程序。我说的是使用 CLI mediafilesegmenter 命令将视频分段的过程,以便从 Amazon S3 流
我有一个带有摄像头模块的 Raspberry Pi,我想将 PiCamera 的视频返回从 Python 流式传输到另一台运行 Python 服务器的计算机。 我成功地使用 OpenCV 返回了视频(
我正在使用 ffmpeg sdk 在 youtube live 上流式传输实时视频 av_interleaved_write_frame(m_pAvFmtCntx, pkt); av_interlea
我正在尝试使用 Tweepy 流式传输 Twitter,我想知道是否可以在不提供关键字的情况下进行流式传输?因此,我将能够流式传输所有推文,而不仅仅是具有给定关键字的推文。我正在关注的代码可以在这里找
我是 Hazelcast 的新手,我正在尝试使用它来将数据存储在太大而无法容纳在单台计算机上的 map 中。 我需要实现的过程之一是检查 map 中的每个值并对其执行某些操作 - 不是累积或聚合,而且
我需要设置从多个网络摄像头到互联网(在浏览器中)的实时流传输,并且这些流应该仅对特定用户可见。 IE。用户 A 使用他或她的登录名/密码登录我的系统,转到视频流页面,并查看来自特定摄像头的流,而其他用
我在看 Twitter Streaming API获取实时提要。但我不希望它存储在我的服务器上。我只是希望它从服务器中提取,浏览器页面将从我的服务器的 twitter 提取 URL 检索数据。但我想避
我想创建一个P2P视频传输的应用程序。更详细地说,我需要以下内容: 用户可以通过点击应用程序中的按钮来打开网络摄像头。 应用程序开始从网络摄像头拍摄图像并将其发送到特定位置(由端口和 IP 地址指定)
我们有一个在 Tomcat 下运行的 Java Web 应用程序(应用程序 #1)。 我们的要求:我们想为流媒体设置一个单独的服务器。我们的媒体文件已经转换为 flv(我们支持的唯一格式)...我们只
我是 RTMP 流媒体的新手,正在寻求帮助。足以让我开始。 我已经在谷歌上搜索了大约 5-7 个小时,但仍然无法确定我的答案! Red5的文档有限,根本找不到任何支持!甚至与我类似的问题在 stack
我正在尝试编写一个简单的 map only hadoop streaming job 从 hdfs 读取数据并将其推送到 vertica。 我写了几个shell脚本如下 加载.sh hadoop
我是 Android 的新手,正在尝试: 从相机流式传输原始数据 (ImageFormat RAW_SENSOR) 处理原始数据 交互显示处理结果 原始数据捕获似乎只能在静态捕获模式下使用。这是正确的
我需要将音频从麦克风流式传输到 http 服务器。 这些录音设置是我需要的: NSDictionary *audioOutputSettings = [NSDictionary dictionaryW
我需要了解 WCF 在发送消息时是如何工作的。 WCF 在发送之前是否序列化所有内容? 我的另一个问题是使用 Streaming 有什么好处?更大的消息是否更好,比如说 1Mb 到 2Mb?我可以发送
是否可以直接从 API 初始化新的环聊直播广播/流式传输?我尝试将环聊直播用于我网站的直播系统,但我在 YouTube Live API 中没有看到任何允许通过环聊直播创建流的 API 方法(看起来需
我是一名优秀的程序员,十分优秀!