录制.WAV与Android AudioRecorder [英] Recording .Wav with Android AudioRecorder

查看:713
本文介绍了录制.WAV与Android AudioRecorder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了大量关于Android的AudioRecorder页面。你可以看到下面的问题,它们的列表。

I have read a lot of pages about Android's AudioRecorder. You can see a list of them below the question.

我想录制音频AudioRecorder,但它不尽如人意。

I'm trying to record audio with AudioRecorder, but it's not working well.

public class MainActivity extends Activity {

AudioRecord ar = null;
int buffsize = 0;

int blockSize = 256;
boolean isRecording = false;
private Thread recordingThread = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}

public void baslat(View v)
{
            // when click to START 
    buffsize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
    ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, buffsize);

    ar.startRecording();

    isRecording = true;
    recordingThread = new Thread(new Runnable() {
        public void run() {
            writeAudioDataToFile();
        }
    }, "AudioRecorder Thread");
    recordingThread.start();
}
public void durdur(View v)
{
            // When click to STOP
    ar.stop();
    isRecording = false;
}

private void writeAudioDataToFile() {
    // Write the output audio in byte

    String filePath = "/sdcard/voice8K16bitmono.wav";
    short sData[] = new short[buffsize/2];

    FileOutputStream os = null;
    try {
        os = new FileOutputStream(filePath);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    while (isRecording) {
        // gets the voice output from microphone to byte format

        ar.read(sData, 0, buffsize/2);
        Log.d("eray","Short wirting to file" + sData.toString());
        try {
            // // writes the data to file from buffer
            // // stores the voice buffer
            byte bData[] = short2byte(sData);
            os.write(bData, 0, buffsize);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private byte[] short2byte(short[] sData) {
    int shortArrsize = sData.length;
    byte[] bytes = new byte[shortArrsize * 2];
    for (int i = 0; i < shortArrsize; i++) {
        bytes[i * 2] = (byte) (sData[i] & 0x00FF);
        bytes[(i * 2) + 1] = (byte) (sData[i] >> 8);
        sData[i] = 0;
    }
    return bytes;

}

这是创造一个.wav文件,但是,当我尝试听它,它不开放。我得到一个文件不支持的错误。我试图用相当多的媒体播放器应用程序播放文件。

It's creating a .wav file but, when I try to listen to it, it's not opening. I'm getting a "file not supported" error. I've tried to play the file with quite a few media player applications.

注意:我必须使用AudioRecorder代替MediaRecorder,因为我的应用程序会做另一个进程,同时记录(显示均衡器)。

NOTE : I have to use AudioRecorder instead of MediaRecorder because my app will be doing another process while recording (displaying an equalizer) .

下面是我读过有关这个主题的页面列表:

Here is the list of pages that I've read about this subject:

  1. http://developer.android.com/reference/android/media/AudioRecord.html#read(short[],%20int,%20int)
  2. 的Andr​​oid AudioRecord例如
  3. http://audiorecordandroid.blogspot.in
  4. AudioRecord对象不进行初始化
  5. <一个href="http://stackoverflow.com/questions/4452815/recording-a-wav-file-from-the-mic-in-android-problems">Recording从Android的麦克风wav文件 - 问题
  6. <一个href="http://i-liger.com/article/android-wav-audio-recording">http://i-liger.com/article/android-wav-audio-recording
  7. <一个href="http://stackoverflow.com/questions/4777181/creating-a-wav-file-from-raw-pcm-data-using-the-android-sdk">Creating从原始的PCM数据使用Android SDK
  8. WAV文件
  9. <一个href="http://stackoverflow.com/questions/5511250/capturing-sound-for-analysis-and-visualizing-frequencies-in-android#5511250">Capturing声音进行分析和可视化的频率在Android中
  1. http://developer.android.com/reference/android/media/AudioRecord.html#read(short[],%20int,%20int)
  2. Android AudioRecord example
  3. http://audiorecordandroid.blogspot.in
  4. AudioRecord object not initializing
  5. Recording a wav file from the mic in Android - problems
  6. http://i-liger.com/article/android-wav-audio-recording
  7. Creating a WAV file from raw PCM data using the Android SDK
  8. Capturing Sound for Analysis and Visualizing Frequencies in Android

有很多不同的方式去了解这一点。我试过很多人,但没有为我工作。我一直对这个问题,现在约6小时,所以我会AP preciate一个明确的答案,最好是一些示例code。

There are a lot of different ways to go about this. I've tried lots of them but nothing works for me. I've been working on this problem for about 6 hours now so I would appreciate a definitive answer, ideally some sample code.

推荐答案

<一个href="https://$c$c.google.com/p/simplesound/source/browse/trunk/src/simplesound/pcm/PcmAudioHelper.java?r=4">PCMAudioHelper解决了我的问题。我将修改这个答案和解释,但首先,我有过这个类做一些测试。

PCMAudioHelper solved my problem. I'll modify this answer and explain it but firstly i have to do some tests over this class.

这篇关于录制.WAV与Android AudioRecorder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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