简体   繁体   English

如何通过蓝牙将音频从一个Android设备流式传输到另一个Android设备?

[英]How to stream audio from one Android device to another Android device Via Bluetooth?

Is it possible to stream audio over bluetooth? 是否可以通过蓝牙传输音频? During my research I found that is it only possible using A2DP(Advanced Audio Distribution Profile) . 在我的研究过程中,我发现只能使用A2DP(Advanced Audio Distribution Profile) And does every android device support A2DP? 每个Android设备都支持A2DP吗? If not, is it possible to stream audio between two android devices using bluetooth? 如果没有,是否可以使用蓝牙在两个Android设备之间传输音频? Please help me understand this. 请帮我理解这个。

I've looked through the following links: 我查看了以下链接:

Is it possible to stream audio over Bluetooth? 是否可以通过蓝牙传输音频?

Below sort of thread says its possibility that streaming audio over Bluetooth is possible between devices but there was question mark on its success rates till Google official announced that We have fixed the a2dp streaming stutter problem on N7. 下面的线程说明它可能在设备之间通过蓝牙传输音频,但是在谷歌官方宣布我们已经修复了N7上的a2dp流媒体口吃问题之前,它的成功率存在问号 The next release should have the fix. 下一个版本应该有修复。 Sorry about the problem. 抱歉,这个问题。

Fix for poor A2DP quality on 4.2.2 修复了4.2.2的A2DP质量差的问题

Bluetooth sound quality suffer after upgrade to 4.2 with Galaxy Nexus 使用Galaxy Nexus升级到4.2后蓝牙音质受损

Android 4.3 Bluetooth Audio Quality Fix for Nexus 7 Flo 适用于Nexus 7 Flo的Android 4.3蓝牙音频质量修复程序

And does every android device support A2DP? 每个Android设备都支持A2DP吗?

Support compatibility is mention on Android developers site that its added from API Level-11 with appropriate features. Android开发者网站上提到了支持兼容性,它是从API Level-11 with appropriate features.添加的, API Level-11 with appropriate features. But I came across XDA tread where OP mention problem in Android 2.1 ,means its also support in previous api levels but OP facing issues. 但是我遇到了XDA ,其中OP提到Android 2.1问题,意味着它也支持以前的api级别但是OP面临的问题。

Hope it make sense to understand and I would like to recommend to refer XDA forum to get more information about A2DP compatibility and its sucess. 希望理解有意义,我想建议参考XDA论坛,以获得有关A2DP兼容性及其成功的更多信息。

I my opinion, if A2DP doesn't work properly, we'd better move to lower layer, and we can create a basic stream which can be used for sending any form of date. 我认为,如果A2DP无法正常工作,我们最好转移到较低层,我们可以创建一个基本流,可用于发送任何形式的日期。 I succeed in sending byte stream via Bluetooth between J2ME devices. 我成功通过J2ME设备之间的蓝牙发送字节流。

If install an app in both devices is acceptable, I've sample codes to create a Bluetooth server and a client to communicate with each other via socket, once the socket is established, you can send you audio stream :) 如果在两个设备上安装应用程序是可以接受的,我会通过套接字创建蓝牙服务器和客户端进行通信的示例代码,一旦套接字建立,你就可以发送音频流:)

here are some core codes: 这里有一些核心代码:

1) server device: 1)服务器设备:

// you can generate your own UUID and use it as an port to eatablish a socket
private static final UUID MY_UUID = UUID.fromString("20687DAD-B023-F19E-2F60-A135554CC3FD")

BluetoothServerSocket serverSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME_INSECURE, MY_UUID);

now you have the serverSocket, just use it as an ordinary ServerSocket: 现在你有了serverSocket,只需将它用作普通的ServerSocket:

BluetoothSocket socket = serverSocket.accept();

and then you can get InputStream and OutputStream from this BluetoothSocket and send audio stream just like HttpConnection 然后您可以从此BluetoothSocket获取InputStreamOutputStream ,并像HttpConnection一样发送音频流

2) client device: 2)客户端设备:

assume you already got the BluetoothDevice 假设您已经拥有了BluetoothDevice

// you should implement the method getBlutoothDevice
BluetoothDevice device = getBluetoothDevice();
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);

You can also stream audio over a basic SCO connection which is not dependent on any profile. 您还可以通过不依赖于任何配置文件的基本SCO连接来传输音频。 However, be aware that A2DP is a profile that was developed specifically for streaming music. 但请注意,A2DP是专为流媒体音乐开发的配置文件。 So any implementation that you make yourself will suffer in quality compared to A2DP. 因此,与A2DP相比,您自己制作的任何实现都会受到影响。

Unless you are streaming music to a device which does not support A2DP, I would certainly use A2DP. 除非您将音乐流式传输到不支持A2DP的设备,否则我肯定会使用A2DP。 Really your only other option is to use a SCO link. 真的唯一的另一种选择是使用SCO链接。

Try using following code snippet: 尝试使用以下代码段:

    public class AudioStream extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  String url = "http://www.example.mp3";
  MediaPlayer mp = new MediaPlayer();
  try {
   mp.setDataSource(url);
   mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
   mp.prepare();
   mp.start();
  } catch (Exception e) {
   Log.i("Exception", "Exception in streaming = " + e);
  }
 }
}

It's really hard to do that. 这真的很难。 But I did a lot of research and got a way. 但我做了很多研究并找到了方法。 Has several delay, connection and root problems, but apparently works. 有几个延迟,连接和根问题,但显然有效。 For this you will need to have the "AirAudio" application installed on the server and the "AllConnect" receiver. 为此,您需要在服务器和“AllConnect”接收器上安装“AirAudio”应用程序。 Then you open AirAudio on the home screen and then open AllConnect, press on your device, then on sources and choose the server, then choose the streaming mp3. 然后在主屏幕上打开AirAudio,然后打开AllConnect,按设备,然后在源上选择服务器,然后选择流式mp3。 The problem is that the delay is very large and another defect is that to transmit only the sound of the system of the android server you should have root on the phone ... I also tried the receiver of the streaming being the VLC, was less delayed but still So it's uncomfortable. 问题是延迟非常大,另一个缺点就是只传输android服务器系统的声音,你应该在手机上有root ...我也试过流媒体接收器是VLC,少了延迟但仍然如此令人不舒服。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过蓝牙将一台Android设备作为免提设备连接到另一台设备 - Connecting one android device via bluetooth as handsfree to another device Android:如何检测当前正在接收音频的蓝牙设备 stream? - Android: How to detect which Bluetooth device is currently getting audio stream? 从Android中的蓝牙音频设备录制音频 - Recording Audio from a Bluetooth Audio Device in Android 通过蓝牙检测附近的另一个 android 设备 - Detecting another nearby android device via Bluetooth 如何将音频从Android设备重定向到蓝牙Speeker - How to redirect audio from android device to bluetooth speeker 从电吉他到Android设备的音频流 - Audio Stream from Electric Guitar Into Android device 如何通过蓝牙从 Android 设备发送具有相同值的 5 个字节 - How to send 5 bytes with the same value from Android device via bluetooth 如何将音频流传输到Android设备? - How Do I Stream Audio Into An Android Device? 通过蓝牙调试Android设备 - Debug android device via Bluetooth 从Android中的一项活动转移到另一项活动后,如何保持蓝牙低功耗设备的连接状态? - How to persist the Bluetooth Low Energy Device connection state after passing from one activity to another in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM