Search code examples
audioandroid-mediaplayeraudio-streamingweb-mediarecorder

Recording audio on the web and sending the data to Android app


I want to capture audio in the Chrome web browser using MediaRecorder and send the encoded audio data to the Android app for playback in real-time.

I can't find any audio format that is supported on both sides. I was trying to use opus codec, but Chrome only supports webm container, and Android supports ogg.

What is the proper way of doing this? I don't want to use WebRTC. I could use some data converters/encoders, but all libraries I found are obsolete/abandoned/insecure.


Solution

  • The webm container format is sometimes stored in .mkv files.

    Android 5+ eats pretty much any webm the Chromium MediaRecorder class delivers. If you give MediaRecorder a MediaStream that came from getUserMedia(), and ask it for the right MIME type, you'll get Opus boxed inside webm.

    const mediaRec = new MediaRecorder(stream, {mimeType: 'audio/webm;codecs=opus'})
    

    If you put that into a file, use name.webm or name.mkv to name it. Android has been able to handle it for a long time now.

    The mkvtoolnix program helps you examine these webm files to see what's in them.

    If this didn't address your question, please make a comment.