Skip to content Skip to sidebar Skip to footer

Playing 2 Musics Through 2 Different Sound Cards At Same Time

Trying something pretty out of the box... I have a simple app with a button that when pushed, plays music out of the audio jack of my android tablet. public void btn1 (View view)

Solution 1:

According to the MediaPlayer documentation, you can set the audio device using setPreferredDevice which receive an AudioDeviceInfo as a parameter, see https://developer.android.com/reference/android/media/MediaPlayer.html#setPreferredDevice(android.media.AudioDeviceInfo).

You will then have to create one MediaPlayer to play on each device.

Solution 2:

it works about like this:

protectedvoidplayAudio() {
    this.playByDeviceIdx(0, R.raw.xxx);
    this.playByDeviceIdx(1, R.raw.yyy);
}

protectedvoidplayByDeviceIdx(int deviceIndex, @IdResint resId) {

    /* obtain audio-output device-infos */
    deviceInfos[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);

    /* check, if the desired index is even within bounds */if(deviceInfos.length < deviceIndex) {

        /* create an instance of MediaPlayer */MediaPlayermp= MediaPlayer.create(this, resId);

        /* assign a preferred device to the MediaPlayer instance */
        mp.setPreferredDevice(deviceInfos[deviceIndex]);

       /* start the playback (only if a device exists at the index) */
       mp.start();
    }
}

you could also filter for the headset jack plug/unplug event:

IntentFilterintentFilter=newIntentFilter(Intent.ACTION_HEADSET_PLUG);
Intentintent= context.registerReceiver(null, intentFilter);
booleanisConnected= intent.getIntExtra("state", 0) == 1;

sources: me, based upon the SDK documentation for the MediaPlayer.

Post a Comment for "Playing 2 Musics Through 2 Different Sound Cards At Same Time"