录音有问题的音频

问题描述 投票:0回答:1

我正在使用 AudioRecord API 来录制内部音频。有时此代码可以完美地记录音频,但有时会出现故障。我尝试了不同的记录技术,但没有一个完全有效,背景有时仍然会出现故障。

录制不良音频的链接:https://youtu.be/Ccundu91Kjs

                AudioPlaybackCaptureConfiguration configuration = new AudioPlaybackCaptureConfiguration.Builder(mediaProjection)
                        .addMatchingUsage(AudioAttributes.USAGE_MEDIA)
                        .build();

                audioFormat = new AudioFormat.Builder()
                        .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                        .setSampleRate(44100)
                        .setChannelMask(AudioFormat.CHANNEL_IN_STEREO)
                        .build();

                audioRecord = new AudioRecord.Builder()
                        .setAudioFormat(audioFormat)
                        .setAudioPlaybackCaptureConfig(konfigurasyon)
                        .build();

                audioRecord.startRecording();
        new Thread(() -> {
            while(recordAudio) {

                try {
                    
                    final ByteBuffer buffer = ByteBuffer.allocateDirect(AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT));

                    try {
                       os = new FileOutputStream(context.getExternalFilesDir(Environment.DIRECTORY_MUSIC) + "/tempAudio.pcm");
                    } catch (Exception e) {
                       e.printStackTrace();
                    }

                                      
                    Log.e("writeAudioDataToFile", "Recording.");

                    Log.e("writeAudioDataToFile", String.valueOf(AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT)));

                    audioRecord.read(buffer.array(), 0, AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT));                    
                  
                    os.write(buffer.array(), 0, AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT));
                    buffer.clear();
                } catch (Exception e) {

                    recordAudio = false;
                    audioRecord.stop();
                    audioRecord.release();
                    audioRecord = null;
                    Data = null;

                    try {
                        os.close();
                    } catch (Exception e1) {
                        e.printStackTrace();
                    }

                    os = null;

                    new File(context.getExternalFilesDir(Environment.DIRECTORY_MUSIC) + "/tempAudiıInProcess.pcm").delete();
                    new File(context.getExternalFilesDir(Environment.DIRECTORY_MUSIC) + "/tempAudio.pcm").delete();
                    new File(context.getExternalFilesDir(Environment.DIRECTORY_MUSIC) + "/tempAudio.mp3").delete();

                    showNotification(context);

                    e.printStackTrace();
                }

它应该在后台录制音频而不会出现故障。

java android audio-recording audiorecord android-audiorecord
1个回答
0
投票

我认为这与您使用硬编码缓冲区大小这一事实有关。 文件指出:

返回成功创建 AudioRecord 对象所需的最小缓冲区大小(以字节为单位)。 请注意,此大小并不能保证在负载下顺利录制,并且应根据 AudioRecord 实例轮询新数据的预期频率选择较高的值。有关有效配置值的更多信息,请参阅 AudioRecord(int, int, int, int, int)。

参考:getMinBufferSize

© www.soinside.com 2019 - 2024. All rights reserved.