Java 如何在 Android 设备上将 WAV 编码为 mp3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3641920/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:04:39  来源:igfitidea点击:

How to encode a WAV to a mp3 on a Android device

javaandroidencodingmp3wav

提问by bramp

I have simplified my question and offered a bounty:What options are there for compressing raw PCM audio data to a mp3 on a Android device.

我简化了我的问题并提供了一个奖励:在 Android 设备上有哪些选项可以将原始 PCM 音频数据压缩为 mp3。

My original post:I'm creating a synthesiser on my Android phone, and I've been generating PCM data to send to the speakers. Now I'm wondering if I can encode this PCM data as a mp3 to save to the sdcard. The MediaRecorderobject can encode audio coming from the microphone into various formats, but doesn't allow the encoding from programmatically generated audio data.

我的原始帖子:我正在我的 Android 手机上创建一个合成器,并且我一直在生成 PCM 数据以发送到扬声器。现在我想知道是否可以将此 PCM 数据编码为 mp3 以保存到 SD 卡。所述MediaRecorder对象可以编码音频从麦克风成各种格式的到来,但不允许从程序生成的音频数据的编码。

So my question is, is there a standard Android API for encoding audio? If not, what pure Java or NDK based solutions are there? And can you recommend any of them?

所以我的问题是,是否有用于编码音频的标准 Android API?如果没有,有哪些基于 Java 或 NDK 的纯解决方案?你能推荐其中任何一个吗?

Failing this I'll just have to save my generated audio as a WAV file, which I can easily do.

如果做不到这一点,我只需要将生成的音频保存为 WAV 文件,这很容易做到。

采纳答案by Paul Gregtheitroade

Pure Java
Look into Tritonus's clean room implementation of javasound which offers an MP3 encoderplugin here: http://www.tritonus.org/plugins.html
Secondly, I would suggest looking into jzoom's libraries JLayer or JLayerME: http://www.javazoom.net/javalayer/javalayer.html(this may only be decode, not sure)
If those doesn't suit your need you can look at this article from 2000 about adding MP3 capabilities to J2SE (with source): http://www.javaworld.com/javaworld/jw-11-2000/jw-1103-mp3.html

Native route
If you want "native" performance I would look at an FFmpeg or Lame port for Android. Lame: http://lame.sourceforge.net/

纯 Java
查看 Tritonus 的 javasound 洁净室实现,它在此处提供MP3 编码器插件:http: //www.tritonus.org/plugins.html
其次,我建议查看 jzoom 的库 JLayer 或 JLayerME:http://www。 javazoom.net/javalayer/javalayer.html(这可能只是解码,不确定)
如果这些不适合您的需要,您可以查看 2000 年关于向 J2SE 添加 MP3 功能的这篇文章(附来源):http:// www.javaworld.com/javaworld/jw-11-2000/jw-1103-mp3.html

本地路由
如果您想要“本地”性能,我会查看适用于 Android 的 FFmpeg 或 Lame 端口。跛脚:http: //lame.sourceforge.net/

回答by NagyI

As far as i know you can't do this using only the tools in the SDK. According to the official developer guide there isn't an MP3 encoder in the platform (Android Supported Media Formats), so you have to port an encoder on your own using the NDK, then write some wrapper code to receive the audio samples through JNI.

据我所知,您不能仅使用 SDK 中的工具来执行此操作。根据官方开发人员指南,平台中没有 MP3 编码器(Android 支持的媒体格式),因此您必须使用 NDK 自己移植编码器,然后编写一些包装器代码以通过 JNI 接收音频样本。

I'm currently working on porting some audio decoders from the Rockbox project for my own music player, and it can record audio into MP3, so maybe you should try to look into it's source and find the encoder library. Most of the decoders have ARM optimalizations which speeds up things noticable, so i guess some of the encoders have also this addition.

我目前正在为我自己的音乐播放器移植 Rockbox 项目中的一些音频解码器,它可以将音频录制到 MP3 中,所以也许您应该尝试查看它的来源并找到编码器库。大多数解码器都有 ARM 优化,可以加快显着的速度,所以我猜一些编码器也有这个附加功能。

回答by Mohit Chauhan

Mp3 encoder is not available in android.you have to compile libav with mp3 lame lib you can find code from http://libavandroid.wordpress.com

Mp3 编码器在 android.you 中不可用。你必须用 mp3 lame lib 编译 libav 你可以从http://libavandroid.wordpress.com找到代码