How to stream audio from one Android device to ano

2019-01-21 06:26发布

问题:

Is it possible to stream audio over bluetooth? During my research I found that is it only possible using A2DP(Advanced Audio Distribution Profile). And does every android device support A2DP? If not, is it possible to stream audio between two android devices using bluetooth? Please help me understand this.

I've looked through the following links:

  • Receive audio via Bluetooth in Android,
  • Google confirms bluetooth audio streaming fix for next version of Android 4.2
  • How can I stream audio from another device via Bluetooth?

回答1:

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. The next release should have the fix. Sorry about the problem.

Fix for poor A2DP quality on 4.2.2

Bluetooth sound quality suffer after upgrade to 4.2 with Galaxy Nexus

Android 4.3 Bluetooth Audio Quality Fix for Nexus 7 Flo

And does every android device support A2DP?

Support compatibility is mention on Android developers site that its added from 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.

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.



回答2:

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. I succeed in sending byte stream via Bluetooth between J2ME devices.

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:

// 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:

BluetoothSocket socket = serverSocket.accept();

and then you can get InputStream and OutputStream from this BluetoothSocket and send audio stream just like HttpConnection

2) client device:

assume you already got the BluetoothDevice

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


回答3:

You can also stream audio over a basic SCO connection which is not dependent on any profile. However, be aware that A2DP is a profile that was developed specifically for streaming music. So any implementation that you make yourself will suffer in quality compared to A2DP.

Unless you are streaming music to a device which does not support A2DP, I would certainly use A2DP. Really your only other option is to use a SCO link.



回答4:

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);
  }
 }
}


回答5:

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. 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. 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.