android - 无法在 Android 中通过蓝牙录制音频

标签 android android-intent bluetooth broadcastreceiver audio-recording

我正在编写一个用于从蓝牙录制音频的 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();
    }
}

这在以正常方式使用手机时效果很好。但是,它不会检测到蓝牙耳机的存在,即使插入耳机后仍会使用手机自带的麦克风。

最佳答案

下面的工作代码

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

关于android - 无法在 Android 中通过蓝牙录制音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16983567/

相关文章:

android - 即时运行需要启用 'Tools | Android | Enable ADB integration'

java - 从 PC 上的 Java 应用程序调用 Android 手机上的号码

android - SQLite 和 Android。如何在查询中使用 Stringhaving?

android - 编译并运行航天飞机应用程序

java - 如何以编程方式创建 Android ViewStub?

android - Android 上的刷新选项卡 Activity

android - 如何打开特定应用程序的用户访问权限

android - 如何隐藏/取消默认传入屏幕

bluetooth - 如何获取低功耗蓝牙制造商的名称

objective-c - 将 Swift 函数转换为 Objective-C SubstringToIndex