我遇到一个问题(现在已经一周了),我遇到一个错误,我的应用崩溃了。 Logcat中显示的消息以蓝色突出显示recorder.start
。如果我尝试捕获引发的异常,则错误指向recorder.stop
。请帮忙。
private MediaRecorder recorder;
private MediaPlayer myPlayer;
private String OUTPUTFILE;
private TextView myCount;
private Button Play;
private Button Next;
private TextView myQuestion;
private int value;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// do something
try {
setupMediaRecorder();
} catch (IOException e) {
e.printStackTrace();
}
fiveSeconds();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recording);
OUTPUTFILE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/audiorecorder.3gp";
myCount = (TextView) findViewById(R.id.myCount);
Play = (Button) findViewById(R.id.Play);
Next = (Button) findViewById(R.id.Next);
myQuestion = (TextView) findViewById(R.id.myQuestion);
//recorder.start();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.RECORD_AUDIO)) {
Toast.makeText(this, "Please grand permission to record audio", Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, 1);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, 1);
}
}
else {
value = MainActivity.GenerateValue();
myQuestion.setText(MainActivity.mylist.get(value) + " ./?");
Play.setEnabled(false);
Next.setEnabled(false);
try {
setupMediaRecorder();
} catch (IOException e) {
e.printStackTrace();
}
fiveSeconds();
Play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Play.setTag("Play");
if(Play.getTag() == "Play"){
playBack();
Play.setBackground(getResources().getDrawable(R.drawable.pause));
Play.setTag("Pause");
}
else if (Play.getTag() == "Pause") {
stopPlayBack();
Play.setBackground(getResources().getDrawable(R.drawable.play));
Play.setTag("Play");
}
}
});
Next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent theIntent = new Intent(getApplicationContext(), Make_Notes.class);
startActivity(theIntent);
}
});
}
}
private void fiveSeconds() {
new CountDownTimer(5000, 1000) {
public void onTick(long milliseconds) {
// Log.i("Seconds Left", String.valueOf(milliseconds/1000));
myCount.setText(String.valueOf(milliseconds / 1000));
if (milliseconds / 1000 < 4) {
myCount.setTextColor(Color.RED);
}
}
public void onFinish() {
recorder.start();
sixtySeconds();
Toast.makeText(Recording.this, "Start!", Toast.LENGTH_SHORT).show();
}
}.start();
}
private void sixtySeconds() {
CountDownTimer start = new CountDownTimer(3000, 1000) {
public void onTick(long milliseconds) {
// Log.i("Seconds Left", String.valueOf(milliseconds/1000));
myCount.setText(String.valueOf(milliseconds / 1000));
if (milliseconds / 1000 < 4) {
myCount.setTextColor(Color.RED);
}
}
public void onFinish() {
recorder.stop();
Play.setEnabled(true);
Next.setEnabled(true);
}
}.start();
}
private void stopRecording(){
if(recorder != null){
recorder.stop();
}
else {
Toast.makeText(this, "There is no recorder to play", Toast.LENGTH_SHORT).show();
}
}
private void playBack(){
myPlayer = new MediaPlayer();
try {
myPlayer.setDataSource(OUTPUTFILE);
myPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
myPlayer.start();
}
private void stopPlayBack(){
myPlayer.stop();
Toast.makeText(this, "There is no media to stop", Toast.LENGTH_SHORT).show();
}
private void setupMediaRecorder() throws IOException {
recorder = new MediaRecorder();
//recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(OUTPUTFILE);
recorder.prepare();
}
}
这是manifest.xml
,表明我已经有权访问Record_Audio
和External_Storage
。
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Logcat错误:
Process: com.example.abdullah.be_fluent, PID: 7054
java.lang.IllegalStateException
at android.media.MediaRecorder._start(Native Method)
at android.media.MediaRecorder.start(MediaRecorder.java:1309)
at com.example.abdullah.be_fluent.Recording$3.onFinish(Recording.java:159)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)