通过2个不同的声卡同时播放2音乐 [英] Playing 2 musics through 2 different sound cards at same time

查看:393
本文介绍了通过2个不同的声卡同时播放2音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试一些开箱即用的东西...我有一个简单的应用程序,带有一个按钮,当按下该按钮时,它可以从Android平板电脑的音频插孔中播放音乐.

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) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.xxx);
    mp.start();
}

我现在已经添加了USB音频接口(通过微型USB适配器),并且可以听到音频.

I've now added a usb audio interface (through a micro usb adapter) and I can hear audio out of it.

我可以用它列出声卡

AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);

for (AudioDeviceInfo device : devices) {
    int b = device.getId();
    int d = device.getType();
    CharSequence productName = device.getProductName();
}

如何路由音乐,以便一次播放2种不同的音乐,一种通过USB,另一种通过耳机插孔?

How do I route music so that I can play 2 different music at once, one through usb and the other through the headphone jack?

推荐答案

它的工作原理如下:

protected void playAudio() {
    this.playByDeviceIdx(0, R.raw.xxx);
    this.playByDeviceIdx(1, R.raw.yyy);
}

protected void playByDeviceIdx(int deviceIndex, @IdRes int 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 */
        MediaPlayer mp = 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:

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
Intent intent = context.registerReceiver(null, intentFilter);
boolean isConnected = intent.getIntExtra("state", 0) == 1;

来源:,基于 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆