首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在Android中通过蓝牙录制音频

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

Stack Overflow用户
提问于 2013-06-07 19:56:37
回答 1查看 1.4K关注 0票数 3

我正在写一个用于录制蓝牙音频的Android应用程序,但我无法在android中通过蓝牙录制音频。您可以在以下代码中看到

代码语言:javascript
复制
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();
    }
}

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

EN

回答 1

Stack Overflow用户

发布于 2013-06-07 20:07:43

下面的工作代码

代码语言:javascript
复制
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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16983567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档