Android:以mp3格式录制声音 [英] Android : Record sound in mp3 format

查看:141
本文介绍了Android:以mp3格式录制声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Android应用程序,它具有通过麦克风捕获声音并通过耳机播放声音的功能.为此,我使用了"AudioRecord"和"AudioTrack".以下是我正在使用的部分代码,(仅供理解)

I am building an android app, having feature of capturing sound through microphone and playing it through headphone. For this, I have used "AudioRecord" and "AudioTrack". Following is some part of code that I am using,(just for understanding)

mInBufferSize = AudioRecord.getMinBufferSize(mSampleRate,
            AudioFormat.CHANNEL_CONFIGURATION_MONO, mFormat);
mOutBufferSize = AudioTrack.getMinBufferSize(mSampleRate,
            AudioFormat.CHANNEL_CONFIGURATION_MONO, mFormat);
mAudioInput = new AudioRecord(MediaRecorder.AudioSource.MIC,
            mSampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, mFormat,
            mInBufferSize);
mAudioOutput = new AudioTrack(AudioManager.STREAM_MUSIC, mSampleRate,
            AudioFormat.CHANNEL_CONFIGURATION_MONO, mFormat,
            mOutBufferSize, AudioTrack.MODE_STREAM);

但是主要问题是我想以mp3格式录制传入的声音吗?请帮助我,我将非常感谢...

But the main problem is that I want to record incoming sound in mp3 format? Please help me in this, I will really appreciate...

预先感谢

推荐答案

有一种解决方法,可以使用MediaRecorder保存.mp3文件.方法如下:

There's a work around for saving .mp3 files using MediaRecorder. Here's how:

recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setOutputFile(Environment.getExternalStorageDirectory()
        .getAbsolutePath() + "/myrecording.mp3");
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.prepare();
recorder.start();

这里的重要部分是setOuputFormatsetAudioEncoder.显然,如果同时使用MediaRecorder.OutputFormat.MPEG_4MediaRecorder.AudioEncoder.AAC,MediaRecorder会录制可播放的mp3.希望这对某人有帮助.

The important part here is the setOuputFormat and the setAudioEncoder. Apparently MediaRecorder records playable mp3 if you're using MediaRecorder.OutputFormat.MPEG_4 and MediaRecorder.AudioEncoder.AAC together. Hope this helps somebody.

当然,如果您愿意使用AudioRecorder类,那么我认为下面链接的源代码Chirag应该可以正常工作- https://github.com/yhirano/Mp3VoiceRecorderSampleForAndroid (尽管您可能需要将其中一些内容从日语翻译为英语)

Of course, if you'd rather use the AudioRecorder class I think the source code Chirag linked below should work just fine - https://github.com/yhirano/Mp3VoiceRecorderSampleForAndroid (although you might need to translate some of it from Japanese to English)

编辑:正如Bruno以及其他一些评论中指出的那样,这不会不对文件进行MP3编码.编码仍然是AAC.但是,如果您尝试播放通过上述代码使用.mp3扩展名保存的声音,由于大多数媒体播放器都足够聪明,可以检测到真正的编码,因此它仍然可以正常播放.

As Bruno, and a few others pointed out in the comments, this does not encode the file in MP3. The encoding is still AAC. However, if you try to play a sound, saved using a .mp3 extension via the code above, it will still play without issues because most media players are smart enough to detect the real encoding.

这篇关于Android:以mp3格式录制声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆