将音频从Android设备流式传输到另一个设备 [英] Streaming audio from an Android device to another

查看:291
本文介绍了将音频从Android设备流式传输到另一个设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何通过互联网将音频从一台设备流传输到另一台设备?我知道使用Java套接字发送基本数据,但想知道如何:

How would I go about streaming audio from one device to another over the internet? I'm aware of sending basic data using Java sockets, but wondering how to:

  1. 在文件中途开始播放流(例如,在歌曲中间)
  2. 要发送的数据需要什么格式. MediaPlayer可以使用url作为数据源,那么从服务器端发送音频时应如何表示音频?

谢谢

推荐答案

已实现了音乐流应用程序,我可以与您分享一些内容.

Having implemented a music streaming app, I can share a little with you.

如果您要流式传输和使用Android MediaPlayer类,则MP3或OGG是您最好的格式选择.

If you want to stream and use the Android MediaPlayer class, MP3 or OGG is your best bet for a format.

如果您的体系结构是客户端服务器,即Internet中的真实服务器将流提供给Android设备,则只需通过HTTP流MP3或OGG字节即可.只需将MediaPlayer指向服务器上的URL.

If your architecture is client-server, i.e. real server in the Internet serving streams to Android devices, then just stream MP3 or OGG bytes over HTTP. Just point MediaPlayer to a URL on on your server.

如果您的体系结构与您自己的自定义套接字代码是对等的,则可以创建一个"proxy http"服务器,该服务器在专用线程上侦听localhost.您将MediaPlayer实例指向本地进程内套接字服务器(例如 http://localhost:54321/MyStream.mp3 ).然后,您必须实现代码以解析来自MediaPlayer的HTTP获取请求,然后在自定义P2P套接字协议和连接到本地http服务器的侦听器之间代理流字节.许多无线电流应用程序正是这样做的,以便从MP3流中解析ICECAST元数据. 这是我使用的代码广播流媒体应用.

If your architecture is peer-to-peer with your own custom socket code, you can create a "proxy http" server that listens on localhost on a dedicated thread. You point your MediaPlayer instance to your local in-process socket server (e.g. http://localhost:54321/MyStream.mp3). Then you have to implement code to parse the HTTP get request form MediaPlayer, then proxy the stream bytes between your custom P2P socket protocol and listeners connected to your local http server. A lot of radio streaming apps do exactly this so as to parse the ICECAST metadata from the MP3 stream. Here's the code I use for my radio streaming app that does this.

对于从文件中途开始"情况,您可能会发现我的 MP3流阅读器类很有用.它包装了InputStream(文件,套接字流等),并从您开始的任何位置同步到下一个有效帧.只需调用read_next_chunk即可获取下一个音频块及其格式. MediaPlayer可能会为您完成大部分繁重的工作,因此可能不需要这样做.

For the "start midway through the file" scenario, you might find my MP3 Stream Reader class useful. It wraps an InputStream (file, socket stream, etc..) and syncs to the next valid frame from where ever you started from. Just call read_next_chunk to get the next block of audio and its format. MediaPlayer might do most of this heavy lifting for you, so this might not be needed.

这篇关于将音频从Android设备流式传输到另一个设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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