Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

record sound and save to mp3 in android

Discussion in 'Audio & Video' started by Afreshman, Nov 26, 2014.

  1. Afreshman

    Afreshman

    Joined:
    Apr 13, 2013
    Posts:
    2
    Hi all,
    I 'm sorry to trouble with you, I want to know, how to record a sound and save to mp3 in android. because wav file is too large. or There is another way to make sound file smaller?

    how should I do?

    thank you!
     
  2. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Hi Afreshman,

    There is unfortunately no straightforward answer.

    As mp3 is a licensed compression format, I suspect you just need compression, not necessarily mp3. ogg Vorbis is open source and good quality.

    Step by step:

    1) Grab the raw audio data with OnAudioFilterRead
    2) On a background thread, encode to ogg and write to disk

    If you are targeting a single platform, it's much easier: you'll just need to find an encoder for that platform and write a plugin for it. If your project is multi platform, you have much work ahead.

    As you mention Android only, research native audio encoding on Android. I'm sure you'll find a library to do the deed.
    More technical is handling threading issues: you shouldn't encode and write on either the audio or the main thread for performance reasons. A dedicated thread should handle queuing of incoming raw data from the audio thread, encoding, and writing to disk.

    It's not a trivial task!

    G-Audio uses a C# library for decoding ogg( way more overhead than native, but fully multi platform and not that bad performance wise ), but the library I've found only does encoding, not decoding.

    Feel free to ask more questions!

    Gregzo
     
  3. Afreshman

    Afreshman

    Joined:
    Apr 13, 2013
    Posts:
    2
    Hi gregzo,

    Thank you for such a detailed answer! But I don't anderstand why I can't encode clip.samples to OGG format and save it into sdcard?
    I had read this article you wrote, I try it and It is perfect worked on Android.
    http://forum.unity3d.com/threads/writing-audiolistener-getoutputdata-to-wav-problem.119295/
     
  4. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Hi Afreshman

    Writing uncompressed audio data is relatively easy. Encoding to ogg is definitely not, and you'll need a library to do it.
    The lazy way would be to write the wav file, and then convert it to ogg using whichever tool you find: performance won't be an issue since encoding won't need to be done in real time.

    Encoding on the fly would be more challenging, as you'll need to buffer audio data and encode it on a separate thread so as not to burden the audio thread.

    Cheers,

    Gregzo