安卓音频录制无法播放整个PCM文件

3

我使用 PCM 文件进行录制。

try {

        DataOutputStream dos = new DataOutputStream(
                new BufferedOutputStream(
                        new FileOutputStream(recordingFile)));

        int bufferSize = AudioRecord.getMinBufferSize(frequency,
                channelConfiguration, audioEncoding);

        AudioRecord audioRecord = new AudioRecord(
                MediaRecorder.AudioSource.MIC, frequency,
                channelConfiguration, audioEncoding, bufferSize);

        AudioTrack audioPlayer = new AudioTrack(AudioManager.STREAM_MUSIC,
                frequency, AudioFormat.CHANNEL_OUT_STEREO, audioEncoding,
                bufferSize, AudioTrack.MODE_STREAM);

        short[] buffer = new short[blockSize];
        double[] toTransform = new double[blockSize];
        int readBytes = 0;
        int writtenBytes = 0;

        audioRecord.startRecording();
        audioPlayer.play();

        AudioManager audioManager = (AudioManager) ctx
                .getSystemService(Context.AUDIO_SERVICE);

        while (started) {
            int bufferReadResult = audioRecord.read(buffer, 0, blockSize);

            for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
                toTransform[i] = (double) buffer[i] / 32768.0; // signed
                // 16
                dos.writeShort(buffer[i]);

            } // bit

            // filterAudio(bufferSize, toTransform, frequency);
            transformer.ft(toTransform);
            publishProgress(toTransform);

            // capture data and record to file

            if (audioManager.isWiredHeadsetOn()) {
                if (audioPlayer.getPlayState() != AudioTrack.PLAYSTATE_PLAYING) {
                    audioPlayer.play();
                }
                if (AudioRecord.ERROR_INVALID_OPERATION != bufferReadResult) {
                    writtenBytes += audioPlayer.write(buffer, 0,
                            bufferReadResult);
                }
            } else {
                if (audioPlayer.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
                    audioPlayer.pause();
                }
            }

        }

        audioRecord.stop();
        audioPlayer.stop();
        audioRecord.release();
        audioPlayer.release();
        try {
            dos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    } catch (Throwable t) {
        t.printStackTrace();
        Log.e("AudioRecord", "Recording Failed");
    }

    return null;

我是这样做的:

private AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

    @Override
    protected Void doInBackground(Void... params) {
        try {

            musicLength = (int) (recordedFile.length() / 2);
            music = new short[musicLength];

            InputStream is = new FileInputStream(recordedFile);
            BufferedInputStream bis = new BufferedInputStream(is);
            DataInputStream dis = new DataInputStream(bis);

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

            dis.close();

            audioTrackRecorded = new AudioTrack(AudioManager.STREAM_MUSIC,
                    frequency, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
                    AudioFormat.ENCODING_PCM_16BIT, musicLength,
                    AudioTrack.MODE_STREAM);

            audioTrackRecorded.write(music, 0, musicLength);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(Void result) {
        progressBarLayout.setVisibility(View.GONE);
        seekbarLayout.setVisibility(View.VISIBLE);

        isInitialized = true;

    };
};

我在播放这个PCM文件时遇到了问题,只有一半的声音能够播放。希望你能帮助我解决这个问题。谢谢。


1
首先要检查的是:文件是否正常?如果您在计算机上播放它(例如在Audacity中),它是否包含您期望包含的所有音频?接下来,audioEncodingchannelConfiguration设置为什么?您是否碰巧尝试以立体声方式播放单声道内容? - Michael
我已经在我的电脑上使用Audacity进行了播放,PCM文件的回放效果很好。在录制和播放过程中,我使用了AudioFormat.CHANNEL_CONFIGURATION_STEREO和AudioFormat.ENCODING_PCM_16BIT。 - ceosilvajr
1
music.length 是否与文件读取循环中的 shorts 数量匹配?audioTrackRecorder.write 的返回值是否也与此值匹配? - Michael
谢谢您的回复,我已经通过修改musicLength =(int)(recordedFile.length()/ 2);为musicLength =(int)recordedFile.length();解决了我的问题。 - ceosilvajr
1个回答

1

这是我的代码,用于播放PCM音频文件...享受吧 :)

void playRecord() {

    audioTrack = null;

    File file = new File(AudioSavePathInDevice, NameAudio);

    int shortSizeInBytes = Short.SIZE/Byte.SIZE;

    final int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);

    final short[] audioData = new short[bufferSizeInBytes];

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

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

        }

        dataInputStream.close();


        audioTrack = new AudioTrack(3,
                11025,
                2,
                2,
                bufferSizeInBytes,
                1);

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


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

}

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