首页 文章

即使插入耳机,如何通过蓝牙扬声器播放音频?

提问于
浏览
10

我将手机连接到蓝牙扬声器并插入了耳机 . 现在我想通过蓝牙扬声器播放音频 . 当我将音频流设置为 AudioManager.STREAM_MUSIC 时,它只是通过耳机播放 .

它是否在耳机上播放并不重要,但我需要它在蓝牙扬声器上播放 .

这怎么可能?应用程序SoundAbout设法做到这一点,所以必须有一种方法 .

EDIT: When I plug in the headphones and only afterwards connect to the Bluetooth speakers all audio plays through the Bluetooth speakers which I want. 但我可以't expect the user to find that out and before having to show them a complicated message I' d而是找到一种方法,使声音在连接到某些时始终通过BT扬声器播放 .

谢谢

(注意这不是同一个问题:How to Play audio through speaker even when headset is plugged in?我想让它在蓝牙音箱上播放,而不是在手机的集成扬声器上播放 . )

5 回答

  • 2

    Solution

    假设您已经在新的媒体播放器实例上测试了STREAM_RING而没有直接设置流类型,并且它没有用完,您需要一个正确的蓝牙设备配置文件 .

    看看this article阅读"Implementing HAL"部分,您可以使用不同的配置文件的很多来源 .

    还有一个简单的解决方案是将您的设备配置文件更改为getServiceConnected()方法中的HEADSET,它将变为保持连接设备,但输出将变为单声道!我记得,对于扬声器来说,这是一个耻辱,A2DP在一些硬件中也可能不受支持,但仍然被有线耳机打断 .

    我建议创建一个新的配置文件并使用它,使用HAL有点棘手,但值得,

    很抱歉,我暂时无法为您提供源代码 .

  • 6

    蓝牙连接可能与以下状态一致 . 收到 BluetoothA2dp.STATE_CONNECTED 后,您可以正常播放音乐 .

    android.bluetooth.BluetoothA2dp.STATE_CONNECTED 的Java代码示例

    public BluetoothHandsfree(Context context, CallManager cm) {
        mCM = cm;
        mContext = context;
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        boolean bluetoothCapable = (adapter != null);
        mHeadset = null;  // nothing connected yet
        mA2dp = new BluetoothA2dp(mContext);
        mA2dpState = BluetoothA2dp.STATE_DISCONNECTED;
        mA2dpDevice = null;
        mA2dpSuspended = false;
    
        mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        mStartCallWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                                       TAG + ":StartCall");
        mStartCallWakeLock.setReferenceCounted(false);
        mStartVoiceRecognitionWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                                       TAG + ":VoiceRecognition");
        mStartVoiceRecognitionWakeLock.setReferenceCounted(false);
    
        mLocalBrsf = BRSF_AG_THREE_WAY_CALLING |
                     BRSF_AG_EC_NR |
                     BRSF_AG_REJECT_CALL |
                     BRSF_AG_ENHANCED_CALL_STATUS;
    
        if (sVoiceCommandIntent == null) {
            sVoiceCommandIntent = new Intent(Intent.ACTION_VOICE_COMMAND);
            sVoiceCommandIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        if (mContext.getPackageManager().resolveActivity(sVoiceCommandIntent, 0) != null &&
                BluetoothHeadset.isBluetoothVoiceDialingEnabled(mContext)) {
            mLocalBrsf |= BRSF_AG_VOICE_RECOG;
        }
    
        mBluetoothPhoneState = new BluetoothPhoneState();
        mUserWantsAudio = true;
        mPhonebook = new BluetoothAtPhonebook(mContext, this);
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        cdmaSetSecondCallState(false);
    
        if (bluetoothCapable) {
            resetAtState();
        }
    
    }
    

    请在下面找到链接:带有示例代码可能会对您有所帮助 .

    Java Code Examples for android.bluetooth.BluetoothHeadset

    Programmatically connect to paired Bluetooth speaker and play audio

  • 2

    如果您的应用程序中有路由逻辑,那么您可以根据该逻辑决定要播放音频的输出 .

    我有一个针对确切目的编写的测试应用程序 . My Github Link

    您还可以根据需要根据需要路由音频 . You can refer this github link for routing

  • 1

    您需要实例化MediaPlayer类的新对象,并在其上使用以下方法

    amediaplayer.setAudioStreamType(AudioManager.STREAM_RING)
    

    不要忘记检查使用蓝牙的授权,如果没有用户权限,您无法通过蓝牙向扬声器发送任何内容 .

  • 0

    Audiomanager将音频覆盖并路由到最新连接的设备(有线耳机或蓝牙耳机) . 在android中,我们没有任何选项可以覆盖此设置,除非它是一个系统应用程序并将音频路由到我们希望路由的任何位置 . 但是,您可以使用反射apis并覆盖此设置 . 如果连接了有线耳机,Audiomanager将暂停蓝牙连接路由(如果已连接),反之亦然 . 你可以看一下代码here .

    因此,使用反射apis,您可以通过调用此method来切换蓝牙音频路由 .

相关问题