将MIC声音录制到android中的字节数组中

时间:2013-05-14 14:39:57

标签: android audio-recording bytebuffer

我正试图从MIC直接录制到一个短阵列。

目标不是用音轨写一个文件,只需将它保存在一个短阵列中。

如果我尝试了几种方法,我发现最好的方法是用AudioRecord录音并用AudioTrack播放。我在这里找到了一个很好的课程:

Android: Need to record mic input

这个课程让我所需要的一切,我只需要修改它以达到我想要的结果,但是......我不能很好地理解它,我错过了一些东西......

这是我的修改(根本不工作):

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);
                short[] buff = new short[N];
                recorder.startRecording();
                /*
                 * 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 = buffer[ix++ % buffer.length];
                    N = recorder.read(buff, 0, buff.length);
                }

                recorder.stop();
                recorder.release();

                track = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, 
                        AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, N*10, AudioTrack.MODE_STREAM);
                track.play();

                for (int i =0; i< buff.length;i++) {
                    track.write(buff, i, buff.length);
                }


            } catch(Exception x)    { 
                //Log.e("Audio", x.getMessage());
                x.printStackTrace();
            } finally { 

                track.stop();
                track.release();
            }
        }

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

    }

我需要的是将声音录制在短阵列缓冲区中,当用户按下按钮时,播放它...但是现在,我正在尝试录制声音,当用户按下按钮时,录制停止,声音开始播放...

任何人都可以帮助我?

感谢。

1 个答案:

答案 0 :(得分:0)

您需要重新构建代码以执行您希望它执行的操作。如果我理解正确,你想要在'stopped'设置为true之前读取声音,然后播放数据。

正如您所了解的那样,根据录制时间的长短,可能存在大量缓冲数据。您可以将其写入文件或将一系列缓冲区存储到某种抽象数据类型中。

只是为了得到一些工作来创建一个short []的Vector并在你的'while(!stopped)'循环中分配一个新的short []缓冲区,然后将它填充到向量中。

while循环停止后,您可以迭代向量并将缓冲区写入AudioTrack。

正如你现在所理解的那样,你听到的短片只是音频的最后20ms左右,因为你的缓冲区只保留了最后一点。