在安卓系统中无法通过蓝牙录制音频

3

我正在编写一个用于从蓝牙录制音频的Android应用程序,但是我无法通过蓝牙在Android上录制音频。你可以看到下面的代码:

AudioManager am;
am = (AudioManager) getSystemService(AUDIO_SERVICE);
     am.setMode(AudioManager.MODE_IN_CALL);
     am.startBluetoothSco();
     am.setBluetoothScoOn(true);
    Intent intent = getIntent();
 if (intent.getBooleanExtra("privacy", false)) {
        showServerPrompt(true);
        return;
    }

    // If the Ringdroid media select activity was launched via a
    // GET_CONTENT intent, then we shouldn't display a "saved"
    // message when the user saves, we should just return whatever
    // they create.
    mWasGetContentIntent = intent.getBooleanExtra(
        "was_get_content_intent", false);

    mFilename = intent.getData().toString();

    mSoundFile = null;
    mKeyDown = false;

    if (mFilename.equals("record")) {
        try {Intent recordIntent = new Intent(
                MediaStore.Audio.Media.RECORD_SOUND_ACTION);
            startActivityForResult(recordIntent, REQUEST_CODE_RECORD);

        } catch (Exception e) {
            showFinalAlert(e, R.string.record_error);
        }

    }

    mHandler = new Handler();

    loadGui();

    mHandler.postDelayed(mTimerRunnable, 100);

    if (!mFilename.equals("record")) {
        loadFromFile();
    }
}

当使用手机进行正常通话时,这很有效。然而,它不能检测到蓝牙耳机的存在,即使插入了耳机,仍然使用手机自己的麦克风。

1个回答

1
以下是可工作的代码。
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

registerReceiver(new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
        Log.d(TAG, "Audio SCO state: " + state);

        if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) { 
            /* 
             * Now the connection has been established to the bluetooth device. 
             * Record audio or whatever (on another thread).With AudioRecord you can record with an object created like this:
             * new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
             * AudioFormat.ENCODING_PCM_16BIT, audioBufferSize);
             *
             * After finishing, don't forget to unregister this receiver and
             * to stop the bluetooth connection with am.stopBluetoothSco();
             */
            unregisterReceiver(this);
        }

    }
}, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));

Log.d(TAG, "starting bluetooth");
am.startBluetoothSco();

3
这可以实现,但值得一提的是需要以下权限:蓝牙、修改音频设置和发送粘性广播。 - Dmitry Brant
我们需要在注册接收器之前调用StartBlueToothSco()吗? - Taki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接