如何从电脑上记录Android(2.3)上的音频?

时间:2015-08-05 11:15:58

标签: android audio audio-streaming

我有单板电脑与android 2.3(我的主板最大版本)。和音频输入/输出(3.5mm)接口。   我需要通过音频接口(不是USB或WI-FI)实时传输来自PC的音频。首先,它可能吗?

我有简单的应用程序,使用AudioRecorder和AudioTrack类。 而这款app车记录了一些数据,但这些数据总是不同的。 此外,当电缆断开时,我得到了一些数据。

app的主要部分:

int sampleRate = 8000;
    int channelConfig = AudioFormat.CHANNEL_IN_MONO;
    int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

    int minInternalBufferSize = AudioRecord.getMinBufferSize(sampleRate,
            channelConfig, audioFormat);
    int internalBufferSize = minInternalBufferSize * 4;
    Log.d(TAG, "minInternalBufferSize = " + minInternalBufferSize
            + ", internalBufferSize = " + internalBufferSize
            + ", myBufferSize = " + myBufferSize);

    audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
            sampleRate, channelConfig, audioFormat, internalBufferSize);
    track = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
            AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, internalBufferSize*4, AudioTrack.MODE_STREAM);
    Log.d(TAG, "init state = " + audioRecord.getState());
    Log.d(TAG, "record start");
    audioRecord.startRecording();
    track.play();
    int recordingState = audioRecord.getRecordingState();
    Log.d(TAG, "recordingState = " + recordingState);

    Log.d(TAG, "read start");
    isReading = true;
    new Thread(new Runnable() {
        @Override
        public void run() {
            if (audioRecord == null)
                return;

            byte[] myBuffer = new byte[myBufferSize];
            int readCount = 0;
            int totalCount = 0;
            int h = 0;
            while (isReading) {
                readCount = audioRecord.read(myBuffer, 0, myBufferSize);
                totalCount += readCount;
                myBuffer2 = myBuffer;
                track.write(myBuffer, 0, myBuffer.length);


                Log.d(TAG, "readCount = " + readCount + ", totalCount = "+ totalCount);
                if (h<5){
                    for (int i=0;i<myBuffer.length;i+=100){
                        Log.d(TAG, "myBuffer index = " + i + ", value = "
                                + myBuffer[i] + " , iter = " + h);
                    }
                    h++;
                }


            }
        }
    }).start();

Board只能使用此audioRecorder配置:8000 - samplerate,mono_channel,encoding_pcm_16bit。

我收到了哪些数据?我不确定,那是来自我的电脑。

虽然track.play();我可以听到一些噪音,但这看起来像一些系统噪音。

任何帮助都会很棒。

0 个答案:

没有答案