简体   繁体   中英

sound recording problem in android

i am creating a voice recorder app for tablet.i tried the below code for recording sound.but it crashes when i click on stop buttonplz help me...

public class soundrecord extends Activity {
    private Button start;
    private Button stop;
    private TextView txt;
     MediaRecorder recorder;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start=(Button)findViewById(R.id.start);
        stop=(Button)findViewById(R.id.stop);
        txt=(TextView)findViewById(R.id.txtstatus);
        recorder = new MediaRecorder();  
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);  
        recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);  
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);  
        recorder.setOutputFile("/myfile/temp");  



        start.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                 try {
                     txt.setText("recording");
                     recorder.prepare();
                     recorder.start(); 
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


            }
        })  ;       


        stop.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                txt.setText("stop recording");
                recorder.stop();
                /*recorder.reset();    
                recorder.release();*/

            }
        })  ;


    }
}

this is exception shown in the logcat

05-12 15:49:01.013: ERROR/AndroidRuntime(677): java.lang.IllegalStateException

The documentation for stop says:

IllegalStateException if it is called before start()

Now I assume you aren't calling it before start. What I'm wondering if your onCreate() is happening properly. If for some reason onCreate() is called a second time (eg the screen rotates), then a new MediaRecorder will be created, and you will be calling stop() on a recorder that hasn't had start called.

Also, are you sure start() is working properly? It is possible the IllegalStateException is coming from start(). Can you give us the full stack trace to say where the IllegalStateException is coming from?


Carrying on with the exception you have given. This is probably because the audiometer directory doesn't exist. If you create this with File.mkdir() it should work. Alternatively, try running with a filename at the top level as given in the example at: http://developer.android.com/guide/topics/media/index.html

recorder.setOutputFile("/audiorecordtest.3gp");

please go through the following link. This is a sample code I found in developer.android.com may be it will be helpful for you http://developer.android.com/guide/topics/media/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM