开发者

sound recording problem in android

开发者 https://www.devze.com 2023-03-05 14:56 出处:网络
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...

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 B开发者_JAVA百科utton 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");
0

精彩评论

暂无评论...
验证码 换一张
取 消