简体   繁体   English

Android音频录制和播放已损坏

[英]Android Audio record and playback is corrupt

I am trying to record a pcm sound file and play it back. 我正在尝试录制一个pcm声音文件并播放它。 When I play it back, it sounds slow and takes longer than it did to record. 当我播放它时,它听起来很慢并且比记录时间更长。 I'm not sure if the error is in the record or play code. 我不确定错误是在记录还是播放代码中。 Any ideas what the problem is? 有什么想法是什么问题?

I largely copied code from this example: http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html 我在很大程度上复制了这个例子中的代码: http//emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html

Here is the record code (isRecording flag is set by stop button in gui thread). 这是记录代码(isRecording标志由gui线程中的停止按钮设置)。

        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
        int sampleRateInHz = 8000;//8000 44100, 22050 and 11025
        int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
        int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

        File sd = Environment.getExternalStorageDirectory();
        File file = new  File(sd, "msg.wav");

        if (file.exists())
            file.delete();

        try {
            file.createNewFile();
        } catch (IOException e) {
            Log.e("create file:", e.toString());
        }

        try {

            OutputStream os = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            DataOutputStream dos = new DataOutputStream(bos);

            int bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz,channelConfig, audioFormat);
            short[] buffer = new short[bufferSize];
            audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 
                    sampleRateInHz,channelConfig, audioFormat,bufferSize);

            audioRecord.startRecording();

            isRecording = true;
            while (isRecording) {
                int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
                for (int i = 0; i < bufferReadResult; i++) 
                {
                    dos.writeShort(buffer[i]);
                }
            }
            dos.close();

This is the play code. 这是播放代码。

          File file = new File(SendAlert.voiceFile);
          // Get the length of the audio stored in the file (16 bit so 2 bytes per short)
          // and create a short array to store the recorded audio.
          int musicLength = (int)(file.length()/2);
          short[] music = new short[musicLength];


          try {
            // Create a DataInputStream to read the audio data back from the saved file.
            InputStream is = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(is);
            DataInputStream dis = new DataInputStream(bis);

            // Read the file into the music array.
            int i = 0;
            while (dis.available() > 0) {
              music[i] = dis.readShort();
              i++;
            }


            // Close the input streams.
            dis.close();     


            // Create a new AudioTrack object using the same parameters as the AudioRecord
            // object used to create the file.
            AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 
                                                   8000, 
                                                   AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                                   AudioFormat.ENCODING_PCM_16BIT, 
                                                   musicLength, 
                                                   AudioTrack.MODE_STREAM);
            // Start playback
            audioTrack.play();

            // Write the music buffer to the AudioTrack object
            audioTrack.write(music, 0, musicLength);

You could try (recording? and) playing back in stereo; 您可以尝试(录制?和)以立体声播放; if the output takes twice as long as the input it would make me think that this is the first problem. 如果输出的时间是输入的两倍,那么我会认为这是第一个问题。 Maybe despite capturing in mono you're writing a stereo wav file? 也许尽管捕获单声道你正在写一个立体声wav文件?

If it's not exactly twice as long a playback as it is to record, what is the ratio? 如果它的回放时间不是记录的两倍,那么比率是多少?

问题是将采样率从示例中的11025更改为8000。

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

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