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

Shruti picture Shruti · Sep 7, 2012 · Viewed 7.8k times · Source

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

Answer

Akash Singh picture Akash Singh · Sep 12, 2012

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