Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8628579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:34:57+00:00 2026-06-12T08:34:57+00:00

Can someone please share with me a RELIABLE way to record audio across all

  • 0

Can someone please share with me a RELIABLE way to record audio across all devices using MediaRecorder? I’m simply trying to record a low-bitrate AMR format audio file, which according to google is standard across all devices. That’s a bunch of crap.

In my experience, there are many off-brand devices, tablets, etc. that will fail horribly when you use the default AudioEncoder.AMR_NB. My workaround is currently to use reflection to poll what encoders are in the superclass, then looping through each one with an errorlistener to see which one doesn’t fail. Not only is this not graceful, but it doesn’t catch all devices. I have also tried setting to default on the AudioEncoder and OutputFormat options (constant 0) and this fails horribly on some devices as well.

Here is what i’m using if the default AMR encoder doesn’t work:

Class encoderClass = MediaRecorder.AudioEncoder.class;
Field[] encoders = encoderClass.getFields();

Then i loop through each encoder, setting an error listener. If it ends successfully i set it as the default encoder as a setting.

for (int i = j; i < encoders.length; i++) {

try {
    int enc = encoders[i].getInt(null);
    recorder.reset();
    recorder.setAudioSource(AudioSource.MIC);
    recorder.setOutputFormat(OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(enc);  //testing the encoder const here
    recorder.setOutputFile(amrPath);
    recorder.setMaxDuration(3000);
    recorder.setOnInfoListener(new OnInfoListener() {

I continue the loop if the listener catches an error:

if (arg1 == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) {

This technique works for most devices. What about the rest?
I still have devices that fall through the cracks and frankly i’d like
something RELIABLE for nearly all devices????

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:34:58+00:00Added an answer on June 12, 2026 at 8:34 am

    Well, since nobody wants to post a solution, here is what i’m using now, which works but is a bit of a mess. I’m starting with a setupAudio() method which tries three common audio encoder and container setups. This will work for most devices. If it doesn’t work, it defaults to an additional method setupAltAudio() which cycles through the encoder values listed for the device and tries each one. I’m sure someone will chime in and say “why not use OnErrorListener()”? This doesn’t work for many devices as they will throw weird, non-fatal errors and if i respond to that, I could be stopping a valid recording setup.

    Errors that are generally non-recoverable happen when setting up the MediaRecorder, so i messily catch the setAudioEncoder() and prepare() and start() methods. If it throws an exception here, I don’t have a valid audio recording setup. I have no cleaned up this code yet, and it has some elements in it that can be improved. Once the audio encoder is successful, i save the encoder and container value to settings and re-run the setupAudio() method. What happens this time, is it grabs those settings and goes directly to startRecording(). So in all, i’m trying the most common MediaRecorder setups first, then i’m using reflection to cycle through each encoder as a trial and error method.
    EDIT:
    The setupAltAudio is missing one detail. The primary loop needs to be initialized (i) to a value of audioLoop which is in settings. This keeps track of which encoder it last tested.

    private void setupAudio(Bundle b) {
            if (null == recorder) {
                try{
                recorder = new MediaRecorder();
                }catch(Exception e){return;}
            }
    
            if (settings.getInt("audioEncoder", -1) > -1) {
                if(null==b){
                    seconds = 60;
                }else{
                seconds = b.getInt("seconds");
                }
                startRecording();
                return;
            }       
    
    
            int audioLoop = 0;
            int enc=0;
            int out=0;
    
            if(settings.getInt("audioLoop", 0)>0){
                audioLoop = settings.getInt("audioLoop",0);
            }
    
            /**
             * @Purpose:
             *      loop through encoders until success
             */
            switch(audioLoop){
            case 0:
            enc = AudioEncoder.AMR_NB;
            out = OutputFormat.THREE_GPP;
            break;
            case 1:
            enc = AudioEncoder.AMR_NB;
            out = OutputFormat.DEFAULT;
            break;
            case 2:
            enc = AudioEncoder.DEFAULT;
            out = OutputFormat.DEFAULT;
            break;
            case 3:
                setupAltAudio(seconds);
                return;
            }
    
            String amrPath = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/data/temp";
            if(!new File(amrPath).exists()){
                new File(amrPath).mkdirs();
            }
            amrPath += "/test.3gp";
            try{    
            recorder.reset();
            recorder.setAudioSource(AudioSource.MIC);
            recorder.setOutputFormat(out);
            recorder.setAudioEncoder(enc);
            recorder.setOutputFile(amrPath);
            recorder.setMaxDuration(5000);
            recorder.prepare();
            recorder.start();
            SharedPreferences.Editor editor = settings.edit();
            editor.putInt("audioEncoder", enc);
            editor.putInt("audioContainer", out);
            editor.commit();
            setupAudio(b);
            return;
            }catch(Exception e){
                e.printStackTrace();
                int count = settings.getInt("audioLoop", 0);
                count++;
                SharedPreferences.Editor editor = settings.edit();
                editor.putInt("audioLoop", count);
                editor.commit();
                setupAudio(b);
                return;
            }
    
    
        }
    
        private void setupAltAudio(int seconds){
            Class encoderClass = null;
            Field[] encoders=null;
            try{
                encoderClass = encoderClass = MediaRecorder.AudioEncoder.class;
                encoders = encoderClass.getFields();            
            }catch(Exception e){}
    
            File tempDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/data/tmp");
            if(!tempDir.exists()){
                tempDir.mkdirs();
            }
    
            int enc = 0;
            int container = 0;
    
            for(int i = 0; i < encoders.length; i++){
    
                try{
                    enc = encoders[i].getInt(null);
                }catch(Exception e){
                    continue;
                }
                recorder.reset();
                recorder.setAudioSource(AudioSource.MIC);
                try{
                recorder.setOutputFormat(OutputFormat.THREE_GPP);
                container = OutputFormat.THREE_GPP;
                }catch(Exception e){
                    recorder.setOutputFormat(OutputFormat.DEFAULT);
                    container = OutputFormat.DEFAULT;
                }
                recorder.setAudioEncoder(enc);
                recorder.setOutputFile(amrPath);
                recorder.setMaxDuration(seconds*1000);
                recorder.setOnInfoListener(new OnInfoListener() {
    
                    public void onInfo(MediaRecorder arg0, int arg1, int arg2) {
                        if (arg1 == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
                            try{
                                recorder.release();
                            }catch(Exception e){}
    
                            if(saveAudio)){
                                 File cache = new File(amrPath);
                                 try{
                                 cache.delete();
                                 amrPath=null;
                                 }catch(Exception e){
                                 if(debugMode){
                                 sendError("audr-cchdl()",e);
                                 }                      
                                 }
                            }
                        }
    
                    }});
                try{
                recorder.prepare();
                recorder.start();
                SharedPreferences.Editor editor = settings.edit();
                editor.putInt("audioEncoder", enc);
                editor.putInt("audioContainer", container);
                editor.commit();
                }catch(Exception e){
                    recorder.release();
                    continue;
                }
    
            }
        }
        private void startRecording() {
            if (!storageAvailable()) {
                stopMe();
                return;
            }
    
    
            try {
                int audioEncoder = settings.getInt("audioEncoder", 1);
                int audioContainer = settings.getInt("audioContainer",1);
                String stamp = String.valueOf(System.currentTimeMillis());
                String filePath = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/data/temp/";
                File fileDir = new File(filePath);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
    
                amrPath = filePath + stamp + ".3gp";
                recorder = new MediaRecorder();
                recorder.reset();
                recorder.setAudioSource(AudioSource.MIC);
                recorder.setOutputFormat(audioContainer);
                recorder.setAudioEncoder(audioEncoder);
                recorder.setOutputFile(amrPath);
                recorder.setMaxDuration(seconds * 1000);
    
                recorder.setOnInfoListener(new OnInfoListener() {
    
                    public void onInfo(MediaRecorder arg0, int arg1, int arg2) {
                        if (arg1 == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
    
                            try {
                                recorder.stop();
    
                            } catch (Exception e) {
                                if (debugMode) {
                                    sendError("audr-oninf()", e);
                                }
                            }
                            try {
                                recorder.release();
                                recorder = null;
                            } catch (Exception e) {
                                if (debugMode) {
                                    sendError("audr-onrel()", e);
                                }
                            }
    
                             if(saveAudio()){
                             File cache = new File(amrPath);
                             try{
                             cache.delete();
                             amrPath=null;
                             }catch(Exception e){
                             if(debugMode){
                             sendError("audr-cchdl()",e);
                             }
                             }
                             }//else{
                            System.out.println("AudioService:Network:SendRecording:Fail");
                            // }
                            stopMe();
                        }
                        if (arg1 == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) { // TODO:
                                                                                    // this
                                                                                    // may
                                                                                    // cause
                                                                                    // more
                                                                                    // problems
                            try {
    
                                recorder.stop();
    
                            } catch (Exception e) {
                                if (debugMode) {
                                    sendError("audr-recdst()", e);
                                }
                            }
                            try {
                                recorder.release();
                                recorder = null;
                                if(new File(amrPath).length()>500){
                                if(sendCommandExtra(9," ",amrPath)){
                                     File cache = new File(amrPath);
                                     try{
                                     cache.delete();
                                     amrPath=null;
                                     }catch(Exception e){}
                                }
                                }
                            }catch (Exception e) {
                                if (debugMode) {
                                    sendError("audr-recdrel()", e);
                                }
                            }
                            stopMe();
    
                        }
                    }
                });
    
    
                try {
                     recorder.prepare();
                     recorder.start();
                } catch (Exception e) {
                    if (debugMode) {
                        sendError("audr-prpst()", e);
                    }
                    recorder.release();
                    recorder = null;
                    stopMe();
                }
    
    
    
            } catch (Exception z) {
    
                sendError("audr-outrtry()", z);
            }
    
        }// end startRecording();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please share a extremely simple version of JQuery Ajax with pagination??
Can someone please share a library/code to access a USB 3G dongle in C#..?
I am trying to implement URL rewriting in my PHP application. Can someone share
Can someone please share a program in java which does the following. if given
Before i start if someone know a better way to do this please Share
Can someone please tell me what this means: 07-04 09:54:38.048: I/DetailActivity(15496): Title that is
Can someone please explain why ?___SID=U is appearing in some Magento URLs on my
Can someone please explain how Read/Show works.. I cannot find any tutorials on it.
Can someone please provide an example of how to store, and read xml data
Can someone please explain to me how this responsive approach works? This was done

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.