Android : 以 mp3 格式录制声音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11985518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android : Record sound in mp3 format
提问by user609239
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)
我正在构建一个 android 应用程序,具有通过麦克风捕获声音并通过耳机播放的功能。为此,我使用了“AudioRecord”和“AudioTrack”。以下是我正在使用的部分代码,(仅供理解)
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);
But the main problem is that I want to record incoming sound in mp3 format? Please help me in this, I will really appreciate...
但主要问题是我想以 mp3 格式录制传入的声音?请在这方面帮助我,我将非常感激......
Thanks in Advance
提前致谢
回答by Advait S
There's a work around for saving .mp3 files using MediaRecorder. Here's how:
有一种使用 MediaRecorder 保存 .mp3 文件的变通方法。就是这样:
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();
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.
这里的重要部分是setOuputFormat
和setAudioEncoder
。如果您一起使用MediaRecorder.OutputFormat.MPEG_4
和MediaRecorder.AudioEncoder.AAC
一起使用,显然 MediaRecorder 会记录可播放的 mp3 。希望这可以帮助某人。
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)
当然,如果您更愿意使用 AudioRecorder 类,我认为下面链接的源代码 Chirag 应该可以正常工作 - https://github.com/yhirano/Mp3VoiceRecorderSampleForAndroid(尽管您可能需要将其中一些从日语翻译成英语)
Edit:As Bruno, and a few others pointed out in the comments, this does not encodethe 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.
编辑:正如布鲁诺和其他一些人在评论中指出的那样,这不会将文件编码为 MP3。编码仍然是AAC。但是,如果您尝试播放通过上述代码使用 .mp3 扩展名保存的声音,它仍然可以正常播放,因为大多数媒体播放器都足够智能,可以检测到真正的编码。
回答by Chirag
回答by Akhil Soman
Set your outputformat
of the recorder as MediaRecorder.OutputFormat.DEFAULT
. I have used this and the resulting audio file is an mp3.
将您outputformat
的录音机设置为MediaRecorder.OutputFormat.DEFAULT
. 我已经使用了这个,生成的音频文件是一个 mp3。
回答by Sushant
This worked for me
这对我有用
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/record.mp3");
mediaRecorder.prepare();
mediaRecorder.start();
回答by Eliran Azulay
I know this is an old question. but if it will help someone I created a repository with an android java full recording app demo, the output file is an mp3 file. keep in mind, I set the target sdk to 21 if to eliminate the need for requesting user permission. this is only for the demo purposes. you can clone and use it.
我知道这是一个老问题。但是如果它可以帮助我创建一个带有 android java 完整录音应用程序演示的存储库,则输出文件是一个 mp3 文件。请记住,如果不需要请求用户权限,我将目标 sdk 设置为 21。这仅用于演示目的。你可以克隆并使用它。
Most of the code I used is from this repo: this
我使用的大部分代码来自这个 repo:this
My demo repo: mp3 recording app
我的演示仓库: mp3 录音应用
回答by Kaushali de silva
This is work for me
这对我来说是工作
outputFile = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/myrecording.mp3";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);`
myAudioRecorder.prepare(); myAudioRecorder.start();`
myAudioRecorder.prepare(); myAudioRecorder.start();`