停止录音机后应用程序崩溃

时间:2019-09-13 09:24:53

标签: android

停止记录时,应用程序崩溃了。我收到这样的错误

  

E / AndroidRuntime:致命例外:录音机中主要

。 我检查了这些链接,但没有找到任何解决方案。

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

How to Record Voice in android?

我的活动:

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 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类:

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 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>

1 个答案:

答案 0 :(得分:0)

只需删除if循环内的注释行即可。

MediaRecorder记录器;

您已在全局和本地创建了MediaRecorder引用。因此在else循环内您正在调用

recorder.release();

使用空引用,因此您会收到此错误。

 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();
                }