Search code examples
androidcheckboxandroid-mediarecorder

Facing some difficulty with recording


I have one CheckBox and one button in my application. The CheckBox name is Record meet and start is the name for button. But when I do check my Record meet am making the name of the button from start to stop.

When I check the button the recording will be started and when i stop the button then the recording will be stopped. When I stop the button am again just making the Record meet is enabled and setChecked(false). Because Again I want to do the recording in my application.

Before going back I want to get the recorded files and I want to store it in a file object.

The problem is when i stop the button then it will make enable the checkbox and setEnable(false). When I click again the checkbox then am getting unfortunately stopped. The error is pointing in myAudioRecorder.prepare().

enter image description here

Could someone help me to solve this problem.

Here is my code.

final CheckBox check=(CheckBox) rootView.findViewById(R.id.recordcheckbox);
    check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if (isChecked) {
                   shouldRecord=true;
                   toggle.setText("stop");
                   check.setEnabled(false);
                 try {
                     myAudioRecorder.prepare();
                     myAudioRecorder.start();
                  } catch (IllegalStateException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                  } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                  } 
                 Toast.makeText(getActivity(), "Recording started", Toast.LENGTH_SHORT).show();
                 String format = "yyyy-MM-dd HH:mm:ss";
                    SimpleDateFormat sdf =new SimpleDateFormat(format, Locale.US);
                    meet_start=sdf.format(Calendar.getInstance().getTime());
                    Log.i("start_time",meet_start);
                } else {

                }

        }
    });
    toggle = (Button) rootView.findViewById(R.id.toggleButton1);
    toggle.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(shouldRecord=false)
            {
            if(toggle.getText().equals("start"))
            {
                String format = "yyyy-MM-dd HH:mm:ss";
                SimpleDateFormat sdf1 =new SimpleDateFormat(format, Locale.US);
                start_time = sdf1.format(Calendar.getInstance().getTime());
                Log.i("start_time",actualstart);
                toggle.setText("stop");
            }
            }
            else if(shouldRecord=true)
            {
            if(toggle.getText().equals("stop")) 
            {
                shouldRecord = false;
                toggle.setText("start");
                check.setEnabled(true);
                check.setChecked(false);
                String format = "yyyy-MM-dd HH:mm:ss";
                SimpleDateFormat sdf =new SimpleDateFormat(format, Locale.US);
                actualend = sdf.format(Calendar.getInstance().getTime());
                Log.i("end_time",actualend);
                    shouldRecord = false;
                try{
                    myAudioRecorder.stop();
                    myAudioRecorder.release();
                    myAudioRecorder = null;
                    }
                    catch(Exception e)
                    {
                        System.out.print("error");
                    }
                Toast.makeText(getActivity(), "Recording stopped", Toast.LENGTH_SHORT).show();
                addRecordingToMediaLibrary();  
            }
            }

        }
    });

public void addRecordingToMediaLibrary() {
    // TODO Auto-generated method stub
    ContentValues values = new ContentValues();
    long current = System.currentTimeMillis();  
     values.put(MediaStore.Audio.Media.TITLE, "audio" + file.getName());  
        values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));  
        values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");  
        values.put(MediaStore.Audio.Media.DATA, file.getAbsolutePath()); 
        ContentResolver contentResolver = getActivity().getContentResolver();   
        Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;  
        Uri newUri = contentResolver.insert(base, values);   
}

Solution

  • You are setting myAudioRecorder to null in your onClick method. Try either commenting that out or getting a new instance of myAudioRecorder in onCheckedChanged.