java - Android 音频记录和播放已损坏

标签 java android

我正在尝试录制一个 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);

最佳答案

您可以尝试(录音?和)立体声回放;如果输出花费的时间是输入的两倍,我会认为这是第一个问题。也许尽管以单声道捕捉,但您正在编写立体声 wav 文件?

如果播放时间不是录制时间的两倍,那么比例是多少?

关于java - Android 音频记录和播放已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3604419/

相关文章:

java - 如何绑定(bind) Activity 中 NavigationView 中的 TextView

java - Eclipse——进度窗口不再显示

android - 在 imageButton 中添加图像

android - 在 Activity 之间共享全局变量的最佳实践

java - 从 Java 创建 MySQL 数据库

java - JScrollPane 中的 JPanel 未显示

java - 删除xml文件中字符串前后不需要的字符串

java - 为什么我的 onTouchListener 会被邻居 View 调用?

android - 如何将表情符号有意发送到whatsapp?

java - 如果数据不可用,则在开始时禁用 Edittexts 并导航到另一个 Activity