在alertDialog上按下保存按钮后Android应用程序强制关闭

时间:2016-11-27 19:25:33

标签: android audio audio-recording

当用户按下开始按钮时,弹出窗口要求输入名称,按此名称音频将存储在SD卡中。

现在我的问题是当用户在警告对话框上按下保存按钮时发生错误,说Meteor.methods

记录活动代码。

java.lang.IllegalStateException and android app force closing

我的logcat

第138行中的

= recorder.start();

import android.app.Activity;
import android.content.DialogInterface;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class Record extends Activity {
String audioName= "";

private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";

private MediaRecorder recorder = null;

Chronometer myChronometer;
Handler seekHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record);


    myChronometer = (Chronometer) findViewById(R.id.chronometer);
    Button recordingButton = (Button) findViewById(R.id.btnStart);


    recordingButton.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(Record.this,
                    "Start Recording With LongClick", Toast.LENGTH_SHORT)
                    .show();
            enableButtons(true);
            startRecording();
            return true;
        }
    });
    setButtonHandlers();
    enableButtons(false);

}

private void setButtonHandlers() {
    ((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
    ((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);

}

private void enableButton(int id, boolean isEnable) {
    ((Button) findViewById(id)).setEnabled(isEnable);
}

private void enableButtons(boolean isRecording) {
    enableButton(R.id.btnStart, !isRecording);
    enableButton(R.id.btnStop, isRecording);
}

private String getFilename() {
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath, AUDIO_RECORDER_FOLDER);

    if (!file.exists()) {
        file.mkdirs();
    }
    return (file.getAbsolutePath() + "/" + audioName +  ".wav");
}

private void startRecording() {
    displayAlertDialog();


}

private void stopRecording() {
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release();
        recorder = null;
        myChronometer.stop();
    }
}



private void displayAlertDialog() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            Record.this);

    // Setting Dialog Title
    alertDialog.setTitle("Would you Like to save your Recording");

    // Setting Dialog Message
    alertDialog.setMessage("Enter Audio Name");

    final EditText editTextAudioName = new EditText(Record.this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    editTextAudioName.setLayoutParams(lp);
    alertDialog.setView(editTextAudioName);


    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("Save",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to execute after dialog
                    audioName = editTextAudioName.getText().toString().trim();
                    recorder = new MediaRecorder();
                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                    recorder.setAudioEncoder(MediaRecorder.OutputFormat.DEFAULT);
                    recorder.setOutputFile(getFilename());
                    recorder.setOnErrorListener(errorListener);
                    recorder.setOnInfoListener(infoListener);


                    try {
                        recorder.prepare();
                        recorder.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    myChronometer.setBase(SystemClock.elapsedRealtime());
                    myChronometer.start();
                }
            }).setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to execute after dialog
                    dialog.cancel();
                }
            });

    // Setting Negative "NO" Button
    alertDialog.show();
}


    private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    @Override
    public void onError(MediaRecorder mr, int what, int extra) {
        Toast.makeText(Record.this, "Error: " + what + ", " + extra,
                Toast.LENGTH_SHORT).show();
    }
};

private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {
        Toast.makeText(Record.this,
                "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
                .show();
    }
};

private View.OnClickListener btnClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnStart: {
                Toast.makeText(Record.this, "Start Recording",
                        Toast.LENGTH_SHORT).show();
                enableButtons(true);
                startRecording();
                break;
            }
            case R.id.btnStop: {
                Toast.makeText(Record.this, "Stop Recording",
                        Toast.LENGTH_SHORT).show();
                enableButtons(false);
                stopRecording();
                // displayFormatDialog();
                break;
            }


        }
    }
};
}

谢谢

1 个答案:

答案 0 :(得分:0)

你是否在你的mannifest中添加了许可?

<uses-permission   android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.RECORD_AUDIO" / >