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 6225663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:52:58+00:00 2026-05-24T08:52:58+00:00

I have strange problem with my MediaRecorder. It records voice, make a file with

  • 0

I have strange problem with my MediaRecorder. It records voice, make a file with recording. But when I want to stop recording using stop() method, it throws IllegalStateException. Generally I have used setMaxDuration() method, so usually I have ending recording using OnInfoListener and it works properly. But I want also to stop MediaRecorder in the OnTouchListener of the ImageView. My code is here:

private static String OUTPUT_FILE;

private void prepareRecording() throws Exception {      
    OUTPUT_FILE = "/sdcard/temp.3gpp";      
    File outFile = new File(OUTPUT_FILE);

    if (outFile.exists())
        outFile.delete();       

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(OUTPUT_FILE);
    recorder.setMaxDuration(10000);
    try {
        recorder.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }       
    recorder.start();       
}

private void startRecording() {  
     ImageView image = (ImageView) this.findViewById(R.id.image);

     image.post(new Runnable() { 
     @Override
     public void run() {
        try {
                prepareRecording();
            } catch (Exception e) {
                e.printStackTrace();                    
            }   

        image.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if (recorder != null)
                    recorder.stop();
                return true;
            }
        });

        recorder.setOnInfoListener(new OnInfoListener() {

            @Override
            public void onInfo(MediaRecorder mr, int what, int extra) {                     
                if (recorder != null && what == 800){
                    recorder.stop();                            
                }                       
            }
        });     

    }});
    }

When I have touched the ImageView I have FATAL EXCEPTION java.lang.IllegalStateException at the line with code recorder.stop(). I have tested my code and I have realized that MediaRecorder seems to be not started. Therefore I have IllegalStateException at recorder.stop(). But when recording stops, I can find on my sdcard recording file. Whats wrong with my code?

I have also AnimationDrawable connected to the my image. It works properly.

  • 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-05-24T08:52:59+00:00Added an answer on May 24, 2026 at 8:52 am

    I have resolved my problem. It is necessary to start new thread in onTouch method to avoid eceptions.

    image.setOnTouchListener(new OnTouchListener() {
    
        public boolean onTouch(View v, MotionEvent event) {
            Thread thread = new Thread(){
            public void run(){                              
                    if (recorder != null)                                   
                        recorder.stop();
            }
        };
        thread.start();                             
        return false;
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have strange problem when using at() method of std::string. I'd like to calculate
I have strange problem with receiving data from socket. On client im using air
I have a strange problem. I do this query in a node.js server using
I have a strange problem. When I want to create a new Spring MVC
I have a strange problem in Visual Studio 2010 (C#) using the Report Viewer
I have a strange problem when using Entity Framework code first. When I return
I have a strange problem: I have a CSV file that I read correctly
I have a strange problem with select on chrome. I want to add a
I have a strange problem here, I want to create an emtpy trigger: SET
I have a strange problem using a MapView in Android. It works fine until

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.