如何通过蓝牙麦克风录制音频

时间:2015-04-14 11:33:36

标签: android

我正在尝试通过蓝牙耳机麦克风录制音频,但我只能听到音频,但无法通过蓝牙麦克风录制音频,而不是我可以通过移动麦克风录制语音。

我正在使用以下代码

IntentFilter newintent = new IntentFilter();
newintent.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
mContext.registerReceiver(mSCOHeadsetAudioState, newintent);

AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setBluetoothScoOn(true);
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.startBluetoothSco();

private final BroadcastReceiver mSCOHeadsetAudioState = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {

        int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);

        if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
            DisplayToast("BT Recording is Ready");

            onRecord();

        } else if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
            DisplayToast("BT Recording Disabled");              

        }
    }
};

public void onRecord(){     

    String mFilename = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
    String mBTFileName = mFilename + "/BTRecord.3gp";

    final RecordThread BT = new RecordThread(false,mBTFileName);


    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

    // set title
    alertDialogBuilder.setTitle("Alert");

    // set dialog message
    alertDialogBuilder
        .setMessage("Recording...")
        .setCancelable(false)
        .setNeutralButton("Stop",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, stop recording
                BT.stopRecording();
                mAudioManager.stopBluetoothSco();
                // To do: start separation and play back here

                dialog.dismiss();
            }
        });

    try{
        BT.prepareRecording();
    }catch(Exception e){
        Toast.makeText(this, "Recording prepare failed", Toast.LENGTH_SHORT).show();
        BT.stopRecording();
        this.finish();
    }
    try{
        mAudioManager.stopBluetoothSco();
        BT.start(); //Try to minimize delay here some how...
    }catch(Exception e){
        Toast.makeText(this, "Recording failed", Toast.LENGTH_SHORT).show();
        Log.e(LOG_TAG, "Record failed");
        BT.stopRecording();
        mAudioManager.stopBluetoothSco();
        this.finish();
    }           

    // create alert dialog
    alertDialog = alertDialogBuilder.create();

    // show it
    alertDialog.show();

}

final class RecordThread extends Thread implements Runnable{

    public String mFile;
    protected boolean isBT;

    private Camera access;
    private MediaRecorder mRecorder;
    private static final String LOG_TAG = "Recording thread";

    public RecordThread(boolean isBT, String file){
        this.isBT = isBT;
        this.mFile = file;
    }
    @Override
    public void run() throws RuntimeException{

        try{
            this.mRecorder.start();
        }catch(Exception e){
            Log.e(LOG_TAG,"Run Method Exception");
            RuntimeException re = new RuntimeException();
            throw re;
        }

    }
    public void prepareRecording() throws Exception{
        this.mRecorder = new MediaRecorder();
        this.mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        this.mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        this.mRecorder.setOutputFile(mFile);
        this.mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

        try {
            this.mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
            if(access != null){
                access.release();
            }
            throw e;
        }        
    }   

    public void stopRecording() {
        if(access != null){
            access.release();
        }
        try{
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;
        }catch(Exception e){        
            mRecorder = null;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

.setMode(AudioManager.MODE_IN_CALL);