AudioRecord 如何保存文件(PCM.WAV)?

发布于 2024-10-16 21:51:07 字数 1239 浏览 3 评论 0原文

static final int frequency = 8000;
static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;  
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;


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

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() 
                     +"/reverseme.pcm");

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

short[] buffer = new short[recBufSize];  
audioRecord.startRecording();

             while (isRecording) {  

                 int bufferReadResult = audioRecord.read(buffer, 0,  
                         recBufSize);

                 for(int i = 0; i < bufferReadResult;i++) {
                     dos.writeShort(buffer[i]);
                 }
             }  
             audioRecord.stop();
             dos.flush();
             dos.close();

但是,打开保存文件(reverseme.pcm),无法播放。 帮帮我,谢谢。

static final int frequency = 8000;
static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;  
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;


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

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() 
                     +"/reverseme.pcm");

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

short[] buffer = new short[recBufSize];  
audioRecord.startRecording();

             while (isRecording) {  

                 int bufferReadResult = audioRecord.read(buffer, 0,  
                         recBufSize);

                 for(int i = 0; i < bufferReadResult;i++) {
                     dos.writeShort(buffer[i]);
                 }
             }  
             audioRecord.stop();
             dos.flush();
             dos.close();

but, open the save file(reverseme.pcm), can not play.
Help me, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2024-10-23 21:51:07

你犯了一个大错误:当你设置AudioRecord.getMinBufferSize的参数时,你选择channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO,但是你可以查找应该设置channelConfig的字典描述音频通道的配置。请参阅CHANNEL_IN_MONOCHANNEL_IN_STEREO

You are making a big mistake : when you set AudioRecord.getMinBufferSize's parameters,you choose channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO,but you can look up dictionary that is should set channelConfig describes the configuration of the audio channels. See CHANNEL_IN_MONO and CHANNEL_IN_STEREO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文