安卓音频录制和播放出现问题

8
我正在尝试录制一个PCM音频文件并进行回放。当我回放时,声音听起来很慢,播放时间比录制时间长。我不确定错误是在记录还是播放代码中。有什么想法吗?
我主要从这个示例复制了代码:http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html 以下是记录代码(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();

这是一个示例代码。

          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);
2个回答

0
你可以尝试使用立体声录制和播放;如果输出时间是输入时间的两倍,那么这可能是第一个问题。也许尽管以单声道捕获,但你正在编写立体声wav文件?
如果播放时间不是录制时间的两倍,那么比例是多少?

有趣的是它的长度是应该的两倍,但切换到立体声播放并没有起作用。它听起来非常奇怪。 - Andrew Thomas
如果你尝试以立体声录制它,会有什么变化吗? - dash-tom-bang

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接