42

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.

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 MediaRecorder object can encode audio coming from the microphone into various formats, but doesn't allow the encoding from programmatically generated audio data.

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?

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

Imdad
  • 683
  • 1
  • 9
  • 27
bramp
  • 9,581
  • 5
  • 40
  • 46
  • "So my question is, is there a standard Android API for encoding audio?" -- no. I'll re-tag your question with the `java` tag to try to help you get input... – CommonsWare Sep 04 '10 at 11:49
  • 6
    FYI: MP3 technology is patented, so including it within a commercial app will expose you to a potential lawsuit. Your best defense against this is to not be successful with your product. :) – MusiGenesis Feb 03 '11 at 13:17
  • 5
    Is there another compact, well supported (Android, Mac, Windows) format that is more open than mp3? Also, does mp3 actually go after anyone for using that format? – Victor Grazi Jul 20 '12 at 03:31
  • @VictorGrazi Ogg Vorbis audio (.ogg) is actually a better format than mp3 (lower file size and higher sound quality at the same time) but is less widely used. –  Jan 11 '15 at 01:21

3 Answers3

32

Pure Java
Look into Tritonus's clean room implementation of javasound which offers an MP3 encoder plugin 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/

Paul Gregoire
  • 9,715
  • 11
  • 67
  • 131
  • 1
    Hi, i am recording audio with AudioRecorder on android and resultant output format is WAV. The output has very large size for ex 1 minutes records take upto 2mb size. can i reduce size while convert to mp3 using tritonus jars? how to implement tritonus on android. please help me. – M.A.Murali Jun 27 '12 at 07:22
10

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.

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.

NagyI
  • 5,907
  • 8
  • 55
  • 83
1

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

Mohit
  • 634
  • 5
  • 15