0

Application is getting crashed when I stop recording. and I am getting an error like this

E/AndroidRuntime: FATAL EXCEPTION: main in voice recorder

. I have checked these links but did not find any solution.

This Activity already has an action bar supplied by the window decor

How to Record Voice in android?

my activity:

public class MainActivity extends AppCompatActivity {
    MediaRecorder recorder;
    Button btnstart;
    Button btnstop;
    ImageView micrecord,imgrecord;
    TextView txttime;
    boolean isrecording=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        micrecord=(ImageView)findViewById(R.id.micrecord);
        imgrecord=(ImageView) findViewById(R.id.imgrecord);
        txttime=(TextView)findViewById(R.id.txttime);
        imgrecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(!isrecording){
                    isrecording=true;
                    imgrecord.setImageResource(R.drawable.stop);
                    micrecord.setImageResource(R.drawable.mic);
                    MediaRecorder recorder;
                    recorder=new MediaRecorder();
                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                    recorder.setOutputFile(G.address+"/"+System.currentTimeMillis()+".amr");
                    try {
                        recorder.prepare();
                        recorder.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else {

                    isrecording=false;
                    imgrecord.setImageResource(R.drawable.stop);
                    micrecord.setImageResource(R.drawable.micc);
                    recorder.release();
                }




            }
        });





    }
}

manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.video69">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.STORAGE"/>

G class:

public class G extends Application {
   public static Context context;
   public  static String address;


    @Override
    public void onCreate() {
        super.onCreate();
        context=getApplicationContext();
        address= Environment.getExternalStorageDirectory().getAbsolutePath()+"/clicksite";
        new File(address).mkdirs();
    }
}

style

  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>

        </style>
gautam
  • 522
  • 3
  • 14
reza motahari
  • 21
  • 1
  • 7

1 Answers1

0

just remove the commented line inside if loop.

MediaRecorder recorder;

you have created MediaRecorder reference globally as well as locally.so inside else loop you are calling

recorder.release();

on the null reference so you are getting this error.

 if(!isrecording){
                    isrecording=true;
                    imgrecord.setImageResource(R.drawable.stop);
                    micrecord.setImageResource(R.drawable.mic);
                    // MediaRecorder recorder;
                    recorder=new MediaRecorder();
                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                    recorder.setOutputFile(G.address+"/"+System.currentTimeMillis()+".amr");
                    try {
                        recorder.prepare();
                        recorder.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else {

                    isrecording=false;
                    imgrecord.setImageResource(R.drawable.stop);
                    micrecord.setImageResource(R.drawable.micc);
                    recorder.release();
                }
gautam
  • 522
  • 3
  • 14