简体   繁体   English

记录pcm文件并通过Audiotrack崩溃播放

[英]Recording pcm file and playing it through Audiotrack crashing

i am recording a pcm file thorugh AudioRecorder and create a file in sdcard. 我正在通过AudioRecorder录制pcm文件,并在sdcard中创建文件。 when recording is done i play the same file with Audiotrack but it crash. 录制完成后,我使用Audiotrack播放相同的文件,但崩溃。 in logcat it says "Invalid audio buffer size". 在logcat中,它说“无效的音频缓冲区大小”。 also my file size on sdcard is 0.0kb ,which i didn't understand why. 我在sdcard上的文件大小也是0.0kb,我不明白为什么。 here is the code. 这是代码。 ` `

public void startRecord() {

        File file = new File(Environment.getExternalStorageDirectory(),
                "test.pcm");

        int sampleFreq = 11025;

        try {
            file.createNewFile();

            OutputStream outputStream = new FileOutputStream(file);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                    outputStream);
            DataOutputStream dataOutputStream = new DataOutputStream(
                    bufferedOutputStream);

            int minBufferSize = AudioRecord.getMinBufferSize(sampleFreq,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT);

            short[] audioData = new short[minBufferSize];

            AudioRecord audioRecord = new AudioRecord(
                    MediaRecorder.AudioSource.MIC, sampleFreq,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, minBufferSize);

            audioRecord.startRecording();

            while (recording) {
                int numberOfShort = audioRecord.read(audioData, 0,
                        minBufferSize);
                for (int i = 0; i < numberOfShort; i++) {
                    dataOutputStream.writeShort(audioData[i]);
                }
            }

            audioRecord.stop();
            dataOutputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void PLaying(int explosion) {

        File file = new File(Environment.getExternalStorageDirectory(),
                "test.pcm");

        int shortSizeInBytes = Short.SIZE / Byte.SIZE;

        int bufferSizeInBytes = (int) (file.length() / shortSizeInBytes);
        short[] audioData = new short[bufferSizeInBytes];

        try {
            InputStream inputStream = new FileInputStream(file);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(
                    inputStream);
            DataInputStream dataInputStream = new DataInputStream(
                    bufferedInputStream);

            int i = 0;
            while (dataInputStream.available() > 0) {
                audioData[i] = dataInputStream.readShort();
                i++;
            }

            dataInputStream.close();

            int sampleFreq = explosion;

            AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                    sampleFreq, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes,
                    AudioTrack.MODE_STREAM);

            audioTrack.play();
            audioTrack.write(audioData, 0, bufferSizeInBytes);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

` `

This code looks dodgy 这段代码看起来很狡猾

while (recording) {
      int numberOfShort = audioRecord.read(audioData, 0,
                    minBufferSize);
      for (int i = 0; i < numberOfShort; i++) {
                dataOutputStream.writeShort(audioData[i]);
      }
}

What is the value of recording . recording的价值是什么。 Where does the value change. 价值在哪里变化。 If it does not change it will be an endless loop (unless and exception is thrown). 如果不更改,则将是一个无限循环(除非抛出异常)。

Do You have integrated the stop method? 您是否集成了stop方法? Please Show us a Little bit more code. 请向我们显示更多代码。 The Problem is, if You haven´t implemented a correct stop mehtod, it will Loop forever..Have You done something like this: 问题是,如果您没有实施正确的停止方法,它将永远循环。

   yourStopButton.setOnClickListener(new OnClickListener(){

         @Override
         public void onClick(View view){

             recording=false;
      }

   });

And then, don´t Forget to do the same in onStart and set recording to true. 然后,不要忘记在onStart中执行相同的操作并将录音设置为true。

构造AudioRecord实例时,将最小缓冲区大小除以2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM