我开发了一个可记录用户语音的android应用。对我来说这是一个学习应用程序。但是,问题在于,录音按钮只能工作一次,而当我第二次点击录音时,录音按钮根本无法停止。共有四个按钮“记录-停止”记录。停止播放。
确切的问题是,当按下第二次记录时,停止按钮保持灰色。
我第三次按记录直接导致应用程序崩溃。
package com.example.soundrecorder;
import android.Manifest;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
Button btnRecord, btnStopRecord, btnPlay, btnStop;
String pathsave="";
MediaRecorder mediaRecorder;
MediaPlayer mediaplayer;
final int REQUEST_PERMISSION_CODE=1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Request Runtime Permission
if(!CheckPermissionFromDevice())
requestPermissions();
//init view
btnPlay = (Button)findViewById(R.id.btnPlay);
btnRecord = (Button)findViewById(R.id.btnStartRecord);
btnStop = (Button)findViewById(R.id.btnStop);
btnStopRecord = (Button)findViewById(R.id.btnStopRecord);
btnRecord.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
if(CheckPermissionFromDevice())
{
pathsave = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"
+ UUID.randomUUID().toString()+"_audio_record.3gp";
setupMediaRecorder();
try{
mediaRecorder.prepare();
mediaRecorder.start();
}catch (IOException e){
e.printStackTrace();
}
btnPlay.setEnabled(false);
btnStop.setEnabled(false);
Toast.makeText(MainActivity.this, "Recording...", Toast.LENGTH_SHORT).show();
}
else
{
requestPermissions();
}
}
});
btnStopRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaRecorder.stop();
btnStopRecord.setEnabled(false);
btnPlay.setEnabled(true);
btnRecord.setEnabled(true);
btnStop.setEnabled(false);
}
});
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStop.setEnabled(true);
btnStopRecord.setEnabled(false);
btnRecord.setEnabled(false);
mediaplayer = new MediaPlayer();
try{
mediaplayer.setDataSource(pathsave);
mediaplayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaplayer.start();
Toast.makeText(MainActivity.this, "Playing...", Toast.LENGTH_SHORT).show();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStopRecord.setEnabled(false);
btnRecord.setEnabled(true);
btnStop.setEnabled(false);
btnPlay.setEnabled(true);
if(mediaplayer !=null)
{
mediaplayer.stop();
mediaplayer.release();
setupMediaRecorder();
}
}
});
}
private void setupMediaRecorder() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(pathsave);
}
private void requestPermissions()
{
ActivityCompat.requestPermissions(this,new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO,
},REQUEST_PERMISSION_CODE );
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode)
{
case REQUEST_PERMISSION_CODE:
{
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
Toast.makeText(this,"Permission Granted",Toast.LENGTH_SHORT).show();
else
{
Toast.makeText(this,"Permission Denied",Toast.LENGTH_SHORT).show();
}
}
break;
}
}
private boolean CheckPermissionFromDevice()
{
int write_external_storage_result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int record_audio_result = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
return write_external_storage_result == PackageManager.PERMISSION_GRANTED && record_audio_result == PackageManager.PERMISSION_GRANTED;
}
}
答案 0 :(得分:0)
在btnStopRecord.setEnabled(true);
单击功能中似乎没有btnRecord
。如果您可以停止第一个录制,则可能是因为默认情况下已启用“停止录制”按钮,但是当您按下它时,它会自行禁用。您可能也应该初始化按钮的启用状态,但这应该可以解决您的问题:
public void onClick(View view) {
if(CheckPermissionFromDevice())
pathsave = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+UUID.randomUUID().toString()+"_audio_record.3gp";
setupMediaRecorder();
try{
mediaRecorder.prepare();
mediaRecorder.start();
btnStopRecord.setEnabled(true); //enable stop record
btnPlay.setEnabled(false); //shouldn't be able to play while recording too
btnRecord.setEnabled(false); //yours to see if should be able to restart record while recording, but probably not if it crashes
btnStop.setEnabled(false); //probably should be disabled too on record
//...rest of the function