Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream and Play audio from android application to any available Speaker like A2DP,Car speakers etc

From my android application I want to stream audio to any available speaker like A2DP speaker ,docking speakers or car speaker.

for doing this I want to use Bluetooth connection and stream over Bluetooth connection...

I saw following links

On android how to reroute audio from a2dp

setBluetoothA2dpOn (boolean on)

Android 2.3: How do I switch from SCO to A2DP for a capable Bluetooth speaker?

But everywhere I found the same .. i.e.

public void setBluetoothA2dpOn (boolean on)

This method is deprecated.
Do not use. 

Please guide me on how to do this..Or suggest if it can be done using some other way.. Any help/suggestion is appreciated.

EDIT :

PERMISSION in manifest :

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

CODE :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    layout = (RelativeLayout) findViewById(R.id.layout);
    text = (TextView) findViewById(R.id.editText1);
    scoSwitch = (ToggleButton) findViewById(R.id.switch1);
    try {
        mp1 = MediaPlayer.create(this, R.raw.jc_cm);
        mp2 = MediaPlayer.create(this, R.raw.rp);
        amanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        //amanager.setBluetoothA2dpOn(true);
                } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void onSCOswitch(View view){
    if (scoSwitch.isChecked()){
        amanager.setBluetoothScoOn(true);
        amanager.startBluetoothSco();
        Log.d("Bluetooth", "SCO on");
        amanager.setMode(AudioManager.MODE_IN_COMMUNICATION);
    }
    else{
        amanager.stopBluetoothSco();
        amanager.setBluetoothScoOn(false);
        amanager.setBluetoothA2dpOn(true);
        Log.d("Bluetooth", "SCO off");
        amanager.setMode(AudioManager.MODE_NORMAL);
    }
} 
like image 686
Shruti Avatar asked Sep 07 '12 06:09

Shruti


2 Answers

i refer u this link please try this

Android: Playing sound over Sco Bluetooth headset

TTS output always going to A2DP

i thing this link can help u

like image 168
Akash Singh Avatar answered Oct 21 '22 08:10

Akash Singh


Here is an implementation of Bluetooth A2DP in a Service. You can refer this code and build your own.

A2DP Bluetooth Service

like image 21
Ronnie Avatar answered Oct 21 '22 08:10

Ronnie