MIC录制声音到Android中的字节数组 [英] Record MIC sound into byte array in android

查看:330
本文介绍了MIC录制声音到Android中的字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从MIC direcly记录到短阵。

I'm trying to record from the MIC direcly to a short array.

我们的目标是不写的音轨文件,只需将它保存很短的数组中。

The goal is not to write a file with the audio track, just save it within a short array.

If've试了几种方法,我已经找到了最好与AudioRecord记录和AudioTrack进行播放。我找到了一个好班在这里:

If've tried several methods and the best I've found is recording with AudioRecord and to play it with AudioTrack. I've found a good class here:

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

本类使得所有我需要的,我只需要修改它实现我想要的结果,但是......我没有得到很好,我想的东西...

This class makes all I need, I just have to modify it to achieve my desired result, but...I don't get it well, I'm missing something...

下面的是我的修改(不工作):

Here's is my modification (not working at all):

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;
        }

    }

我需要的是录制的声音在短期数组缓冲区,当用户按下一个按钮,播放...但是现在,我想录制声音,当用户按下一个按钮,记录停止和声音开始播放...

What I need is to record the sound in the short array buffer and when the user push a button, play it...But right now, I'm trying to record the sound and, when user push a button, recording stop and the sound start playing...

任何人都可以帮我吗?

感谢。

推荐答案

您需要调整的code做你想要它做的事情。如果我理解正确你想读的声音,直到停止设置为true,然后播放数据。

You need to restructure the code to do what you want it to do. If I understand correctly you want to read sound until the 'stopped' is set true, then play the data.

只要你明白,可能是取决于录音时间有多长了不少缓冲的数据。你可以把它写入一个文件或一系列的缓冲区存储到一些抽象的数据类型。

Just so you understand that is potentially a lot of buffered data depending on how long that recording time is. You could write it to a file or store a series of buffers into some abstract data type.

刚刚得到的东西的工作创造的短期[]一个Vector和你,而(!停止)'循环分配一个新的短[]缓冲区,然后将它塞进了载体。

Just to get something to work create a Vector of short [] and allocate a new short [] buffer in your 'while(!stopped)' loop and then stuff it into the vector.

后,您可以通过矢量迭代和缓冲区写入AudioTrack。

After the while loop stops you can iterate through the vector and write the buffers to the AudioTrack.

正如你现在明白了,你所听到的昙花一现就是刚刚过去的20毫秒左右的音频,因为你的缓冲区只保留这最后一点。

As you now understand, the blip you were hearing is just the last 20ms or so of audio since your buffer only kept that last little bit.

这篇关于MIC录制声音到Android中的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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