从蓝牙耳机实现录音

时间:2015-06-24 15:04:13

标签: android bluetooth audio-recording

我需要该应用程序能够使用蓝牙设备麦克风录制音频。 我从@Stephan找到了代码,但我无法让它工作:

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

链接: How to record sound using bluetooth headset

有人可以告诉我如何在下面的活动或服务代码中实现此代码,因为我对编程知之甚少。

CODE(活动):

public class MainActivity extends Activity {

public  ResponseReceiver receiver;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent msgIntent = new Intent(MainActivity.this, SimpleIntentService.class);
    startService(msgIntent);

    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            System.exit(0);
        }
    });

            IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    receiver = new ResponseReceiver();
    registerReceiver(receiver, filter);

}

public class ResponseReceiver extends BroadcastReceiver {
    public static final String ACTION_RESP = "alar.alar.com.rahelividinakes2.MESSAGE_PROCESSED";

    @Override
    public void onReceive(Context context, Intent intent) {
        TextView result = (TextView) findViewById(R.id.textView);
        String text = intent.getStringExtra(SimpleIntentService.PARAM_OUT_MSG);
        result.setText(Html.fromHtml(text + "<br>" + result.getText()));
        {

        }
    }
}

}

代码(服务):

public class SimpleIntentService extends IntentService {

public static final String PARAM_OUT_MSG = "omsg";

public SimpleIntentService() {
    super("SimpleIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {


    final int RECORDER_SAMPLERATE = 8000;
    final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
    int frequency;
    AudioRecord recorder;
    int numCrossing, p, numSamples;
    short audioData[];
    boolean recording;

    int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
            RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);

    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSize);

    recorder.startRecording();
    recording = true;
    audioData = new short[bufferSize];
    int[] values;
    int k = 0, t = 0;

    values = new int[2];

    while (recording) {
        numCrossing = 0;
        numSamples = 0;
        recorder.read(audioData, 0, bufferSize);
        int mod = (bufferSize / 4) * 4;

        for (p = 0; p < mod; p += 4) {
            if (audioData[p] > 0 && audioData[p + 1] <= 0) numCrossing++;
            if (audioData[p] < 0 && audioData[p + 1] >= 0) numCrossing++;
            if (audioData[p + 1] > 0 && audioData[p + 2] <= 0) numCrossing++;
            if (audioData[p + 1] < 0 && audioData[p + 2] >= 0) numCrossing++;
            if (audioData[p + 2] > 0 && audioData[p + 3] <= 0) numCrossing++;
            if (audioData[p + 2] < 0 && audioData[p + 3] >= 0) numCrossing++;
            if (audioData[p + 3] > 0 && audioData[p + 4] <= 0) numCrossing++;
            if (audioData[p + 3] < 0 && audioData[p + 4] >= 0) numCrossing++;
            numSamples += 4;
        }
        for (p = 0; p < bufferSize; p++) {
            if (audioData[p] > 0 && audioData[p + 1] <= 0) numCrossing++;
            if (audioData[p] < 0 && audioData[p + 1] >= 0) numCrossing++;
            numSamples++;
        }

        frequency = (8000 / numSamples) * numCrossing;

        Log.d("proov", String.valueOf(frequency));

...(while loop continues)

1 个答案:

答案 0 :(得分:0)

由于应用程序启动时应检测到蓝牙,因此应该在OnCreate下的MainActivity中。

希望它有所帮助。

public class MainActivity extends Activity {

    public  ResponseReceiver receiver;
    private static final String TAG = MainActivity.class.getName();
    AudioManager am;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        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();
  }
}