Android:需要记录麦克风输入 [英] Android: Need to record mic input

查看:53
本文介绍了Android:需要记录麦克风输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 android 中记录麦克风输入,同时实时播放/预览?我尝试使用 AudioRecordAudioTrack 来执行此操作,但问题是我的设备无法播放录制的音频文件.实际上,任何安卓播放器应用程序都无法播放录制的音频文件.

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 进行录制会生成一个可以被任何播放器应用程序播放的良好录制的音频文件.但问题是我无法在实时录制麦克风输入时进行预览/回放.

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.

推荐答案

要(几乎)实时录制和播放音频,您可以启动一个单独的线程并使用 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.

请注意反馈.如果您的设备上的扬声器开得足够大,反馈会很快变得非常糟糕.

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 对象将音频编码为 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 卡读取和写入文件以实时播放/预览,因此您必须使用缓冲区.

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.

这篇关于Android:需要记录麦克风输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆