安卓:需要记录麦克风输入麦克风

2023-09-12 01:29:11 作者:闭眸

有没有一种方法来记录在Android的麦克风输入,而它正在处理进行播放/ preVIEW实时?我试图用 AudioRecord 和AudioTrack要做到这一点,但问题是,我的设备不能播放录制的音频文件。其实,任何Android的播放器应用程序不能播放录制的音频文件。

Is there a way to record mic input in android while it is being process for playback/preview in real time? I tried to use AudioRecord and AudioTrack to do this but the problem is that my device cannot play the recorded audio file. Actually, any android player application cannot play the recorded audio file.

在另一方面,使用Media.Recorder到记录生成可以由任何播放器应用可以起到了良好的记录的音频文件。但事实是,我不能让一个preVIEW / palyback同时记录实时麦克风输入。

On the other hand, Using Media.Recorder to record generates a good recorded audio file that can be played by any player application. But the thing is that I cannot make a preview/palyback while recording the mic input in real time.

任何反馈是非常AP preciated! 在此先感谢!

Any feedback is very much appreciated! Thanks in advance!

推荐答案

要录制和播放音频(几乎)实时可以启动一个单独的线程,并使用 AudioRecord AudioTrack

To record and play back audio in (almost) real time you can start a separate thread and use an AudioRecord and an AudioTrack.

只是要小心的反馈。如果扬声器打开了声音足够大的设备上,反馈可以得到快速pretty的讨厌pretty的。

Just be careful with feedback. If the speakers are turned up loud enough on your device, the feedback can get pretty nasty pretty fast.

/*
 * Thread to manage live recording/playback of voice input from the device's microphone.
 */
private class Audio extends Thread
{ 
    private boolean stopped = false;

    /**
     * Give the thread high priority so that it's not canceled unexpectedly, and start it
     */
    private Audio()
    { 
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
        start();
    }

    @Override
    public void run()
    { 
        Log.i("Audio", "Running Audio Thread");
        AudioRecord recorder = null;
        AudioTrack track = null;
        short[][]   buffers  = new short[256][160];
        int ix = 0;

        /*
         * Initialize buffer to hold continuously recorded audio data, start recording, and start
         * playback.
         */
        try
        {
            int N = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
            recorder = new AudioRecord(AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, N*10);
            track = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, 
                    AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, N*10, AudioTrack.MODE_STREAM);
            recorder.startRecording();
            track.play();
            /*
             * Loops until something outside of this thread stops it.
             * Reads the data from the recorder and writes it to the audio track for playback.
             */
            while(!stopped)
            { 
                Log.i("Map", "Writing new data to buffer");
                short[] buffer = buffers[ix++ % buffers.length];
                N = recorder.read(buffer,0,buffer.length);
                track.write(buffer, 0, buffer.length);
            }
        }
        catch(Throwable x)
        { 
            Log.w("Audio", "Error reading voice audio", x);
        }
        /*
         * Frees the thread's resources after the loop completes so that it can be run again
         */
        finally
        { 
            recorder.stop();
            recorder.release();
            track.stop();
            track.release();
        }
    }

    /**
     * Called from outside of the thread in order to stop the recording/playback loop
     */
    private void close()
    { 
         stopped = true;
    }

}

修改

音频是不是真的记录到文件中。该 AudioRecord 目标连接codeS音频与 16位PCM数据和在缓冲器中放置它。那么 AudioTrack 对象读取缓冲区中的数据,并通过扬声器播放。有SD卡,你可以稍后访问任何文件。

The audio is not really recording to a file. The AudioRecord object encodes the audio as 16 bit PCM data and places it in a buffer. Then the AudioTrack object reads the data from that buffer and plays it through the speakers. There is no file on the SD card that you will be able to access later.

您无法读取和写入从SD卡中的文件在同一时间获得播放/ preVIEW是实时的,所以你要使用的缓冲区。

You can't read and write a file from the SD card at the same time to get playback/preview in real time, so you have to use buffers.