录音机录音后无法播放
Audio recorder unable to play after recording
我已经创建了音频播放器。录音机具有开始录制按钮和停止录制按钮以及播放录制和停止按钮。我注意到录制后,单击时不会播放录制的文件,但会显示 Toast,表示执行了操作。我无法在 logcat 中检测到任何错误。感谢期待您的帮助。
我的程序代码如下:
//Requesting Run-time permission
if (!checkPermissionFromDevice())
requestPermission();
//Init View
btnPlay = (Button)findViewById(R.id.btnPlay);
btnStop = (Button) findViewById(R.id.btnStop);
btnStartRecorder = (Button) findViewById(R.id.startRecord);
btnStopRecorder = (Button) findViewById(R.id.stopRecord);
//implementing the actions
btnStartRecorder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkPermissionFromDevice())
{
pathSave = Environment.getExternalStorageDirectory()
.getAbsolutePath()+"/"
+ UUID.randomUUID().toString()+"_audio_record.amr";
setupMediaRecorder();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
btnPlay.setEnabled(false);
btnStop.setEnabled(false);
Toast.makeText(RecorderActivity.this, "Recording...", Toast.LENGTH_SHORT).show();
}
else {
requestPermission();
}
}
});
btnStopRecorder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaRecorder.stop();
btnStopRecorder.setEnabled(false);
btnPlay.setEnabled(true);
btnStartRecorder.setEnabled(true);
btnStop.setEnabled(false);
Toast.makeText(RecorderActivity.this, "Stop Record...", Toast.LENGTH_SHORT).show();
}
});
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStop.setEnabled(true);
btnStopRecorder.setEnabled(false);
btnStartRecorder.setEnabled(false);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(pathSave);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
Toast.makeText(RecorderActivity.this, "Playing...", Toast.LENGTH_SHORT).show();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStopRecorder.setEnabled(false);
btnStartRecorder.setEnabled(true);
btnStop.setEnabled(false);
btnPlay.setEnabled(true);
if (mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
setupMediaRecorder();
Toast.makeText(RecorderActivity.this, "Stop Playing...", Toast.LENGTH_SHORT).show();
}
}
});
}
private void setupMediaRecorder() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(pathSave);
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
}, REQUEST_PERMISSION_CODE);
}
//Press Ctr+O
@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;
}
}
error encountered when i replaced the with the answer you gave me
这里是你问题的解答,你可以看看
public class RecordAudioActivity extends AppCompatActivity {
Button buttonStart, buttonStop, buttonPlayLastRecordAudio,
buttonStopPlayingRecording, setAudioasMusic ;
File AudioSavePathInDevice;
MediaRecorder mediaRecorder ;
String audioUri;
public static final int RequestPermissionCode = 1;
MediaPlayer mediaPlayer ;
SharedPreferences sharedPref;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPref = getSharedPreferences("musicUri", Context.MODE_PRIVATE);
setContentView(R.layout.activity_record_audio);
buttonStart = (Button) findViewById(R.id.button);
buttonStop = (Button) findViewById(R.id.button2);
buttonPlayLastRecordAudio = (Button) findViewById(R.id.button3);
buttonStopPlayingRecording = (Button)findViewById(R.id.button4);
setAudioasMusic = (Button)findViewById(R.id.button5);
buttonStop.setEnabled(false);
buttonStop.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonPlayLastRecordAudio.setEnabled(false);
buttonPlayLastRecordAudio.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonStopPlayingRecording.setEnabled(false);
buttonStopPlayingRecording.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
setAudioasMusic.setEnabled(false);
setAudioasMusic.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonStart.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View view) {
if(checkPermission()) {
AudioSavePathInDevice =
new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/AudioRecording.3gp");
if (Build.VERSION.SDK_INT < 24) {
audioUri = String.valueOf(Uri.fromFile(AudioSavePathInDevice));
} else {
audioUri = String.valueOf(Uri.parse(AudioSavePathInDevice.getPath())); // My work-around for new SDKs, doesn't work in Android 10.
}
MediaRecorderReady();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
buttonStart.setEnabled(false);
buttonStart.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonStop.setEnabled(true);
buttonStop.setTextColor(getResources().getColor(R.color.ef_white));
Toast.makeText(RecordAudioActivity.this, "Recording started",
Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
});
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaRecorder.stop();
buttonStop.setEnabled(false);
buttonStop.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonPlayLastRecordAudio.setEnabled(true);
buttonPlayLastRecordAudio.setTextColor(getResources().getColor(R.color.ef_white));
buttonStart.setEnabled(true);
buttonStart.setTextColor(getResources().getColor(R.color.ef_white));
setAudioasMusic.setEnabled(true);
setAudioasMusic.setTextColor(getResources().getColor(R.color.ef_white));
buttonStopPlayingRecording.setEnabled(false);
buttonStopPlayingRecording.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
Toast.makeText(RecordAudioActivity.this, "Recording Completed",
Toast.LENGTH_LONG).show();
}
});
buttonPlayLastRecordAudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) throws IllegalArgumentException,
SecurityException, IllegalStateException {
buttonStop.setEnabled(false);
buttonStop.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonStart.setEnabled(false);
buttonStart.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonStopPlayingRecording.setEnabled(true);
buttonStopPlayingRecording.setTextColor(getResources().getColor(R.color.ef_white));
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(String.valueOf(AudioSavePathInDevice));
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
Toast.makeText(RecordAudioActivity.this, "Recording Playing",
Toast.LENGTH_LONG).show();
}
});
buttonStopPlayingRecording.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View view) {
buttonStop.setEnabled(false);
buttonStop.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonStart.setEnabled(true);
buttonStart.setTextColor(getResources().getColor(R.color.ef_white));
buttonStopPlayingRecording.setEnabled(false);
buttonStopPlayingRecording.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonPlayLastRecordAudio.setEnabled(true);
buttonPlayLastRecordAudio.setTextColor(getResources().getColor(R.color.ef_white));
setAudioasMusic.setEnabled(true);
setAudioasMusic.setTextColor(getResources().getColor(R.color.ef_white));
if(mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
MediaRecorderReady();
}
}
});
private void requestNewInterstitial() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case RequestPermissionCode:
if (grantResults.length> 0) {
boolean StoragePermission = grantResults[0] ==
PackageManager.PERMISSION_GRANTED;
boolean RecordPermission = grantResults[1] ==
PackageManager.PERMISSION_GRANTED;
if (StoragePermission && RecordPermission) {
Toast.makeText(RecordAudioActivity.this, "Permission Granted",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(RecordAudioActivity.this,"Permission " +
"Denied",Toast.LENGTH_LONG).show();
}
}
break;
}
}
public boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(),
WRITE_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(getApplicationContext(),
RECORD_AUDIO);
return result == PackageManager.PERMISSION_GRANTED &&
result1 == PackageManager.PERMISSION_GRANTED;
}
private void requestPermission() {
ActivityCompat.requestPermissions(RecordAudioActivity.this, new
String[]{WRITE_EXTERNAL_STORAGE, RECORD_AUDIO}, RequestPermissionCode);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void MediaRecorderReady(){
mediaRecorder=new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(AudioSavePathInDevice);
}
}
如果您使用 android 10 或更高
,请不要忘记在您的清单中添加
android:grantUriPermissions="true"
android:requestLegacyExternalStorage="true"
经过密切监控和发现,我得到了解决方案。
只需设置 btnStopRecorder enabled 即可解决问题。
现在,对于可能喜欢此代码的任何人
只需用下面的代码替换 btnStartRecorder 按钮。
我现在很开心!
btnStartRecorder.setOnClickListener(new View.OnClickListener() {
@Override*emphasized text*
public void onClick(View view) {
if (checkPermissionFromDevice())
{
pathSave = Environment.getExternalStorageDirectory()
.getAbsolutePath()+"/"
+ UUID.randomUUID().toString()+"_audio_record.amr";
setupMediaRecorder();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
btnPlay.setEnabled(false);
btnStop.setEnabled(false);
btnStopRecorder.setEnabled(true);
Toast.makeText(RecorderActivity.this, "Recording...", Toast.LENGTH_SHORT).show();
}
else {
requestPermission();
}
}
});
我已经创建了音频播放器。录音机具有开始录制按钮和停止录制按钮以及播放录制和停止按钮。我注意到录制后,单击时不会播放录制的文件,但会显示 Toast,表示执行了操作。我无法在 logcat 中检测到任何错误。感谢期待您的帮助。
我的程序代码如下:
//Requesting Run-time permission
if (!checkPermissionFromDevice())
requestPermission();
//Init View
btnPlay = (Button)findViewById(R.id.btnPlay);
btnStop = (Button) findViewById(R.id.btnStop);
btnStartRecorder = (Button) findViewById(R.id.startRecord);
btnStopRecorder = (Button) findViewById(R.id.stopRecord);
//implementing the actions
btnStartRecorder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkPermissionFromDevice())
{
pathSave = Environment.getExternalStorageDirectory()
.getAbsolutePath()+"/"
+ UUID.randomUUID().toString()+"_audio_record.amr";
setupMediaRecorder();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
btnPlay.setEnabled(false);
btnStop.setEnabled(false);
Toast.makeText(RecorderActivity.this, "Recording...", Toast.LENGTH_SHORT).show();
}
else {
requestPermission();
}
}
});
btnStopRecorder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaRecorder.stop();
btnStopRecorder.setEnabled(false);
btnPlay.setEnabled(true);
btnStartRecorder.setEnabled(true);
btnStop.setEnabled(false);
Toast.makeText(RecorderActivity.this, "Stop Record...", Toast.LENGTH_SHORT).show();
}
});
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStop.setEnabled(true);
btnStopRecorder.setEnabled(false);
btnStartRecorder.setEnabled(false);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(pathSave);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
Toast.makeText(RecorderActivity.this, "Playing...", Toast.LENGTH_SHORT).show();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStopRecorder.setEnabled(false);
btnStartRecorder.setEnabled(true);
btnStop.setEnabled(false);
btnPlay.setEnabled(true);
if (mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
setupMediaRecorder();
Toast.makeText(RecorderActivity.this, "Stop Playing...", Toast.LENGTH_SHORT).show();
}
}
});
}
private void setupMediaRecorder() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(pathSave);
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
}, REQUEST_PERMISSION_CODE);
}
//Press Ctr+O
@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;
}
}
error encountered when i replaced the with the answer you gave me
这里是你问题的解答,你可以看看
public class RecordAudioActivity extends AppCompatActivity {
Button buttonStart, buttonStop, buttonPlayLastRecordAudio,
buttonStopPlayingRecording, setAudioasMusic ;
File AudioSavePathInDevice;
MediaRecorder mediaRecorder ;
String audioUri;
public static final int RequestPermissionCode = 1;
MediaPlayer mediaPlayer ;
SharedPreferences sharedPref;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPref = getSharedPreferences("musicUri", Context.MODE_PRIVATE);
setContentView(R.layout.activity_record_audio);
buttonStart = (Button) findViewById(R.id.button);
buttonStop = (Button) findViewById(R.id.button2);
buttonPlayLastRecordAudio = (Button) findViewById(R.id.button3);
buttonStopPlayingRecording = (Button)findViewById(R.id.button4);
setAudioasMusic = (Button)findViewById(R.id.button5);
buttonStop.setEnabled(false);
buttonStop.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonPlayLastRecordAudio.setEnabled(false);
buttonPlayLastRecordAudio.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonStopPlayingRecording.setEnabled(false);
buttonStopPlayingRecording.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
setAudioasMusic.setEnabled(false);
setAudioasMusic.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonStart.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View view) {
if(checkPermission()) {
AudioSavePathInDevice =
new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/AudioRecording.3gp");
if (Build.VERSION.SDK_INT < 24) {
audioUri = String.valueOf(Uri.fromFile(AudioSavePathInDevice));
} else {
audioUri = String.valueOf(Uri.parse(AudioSavePathInDevice.getPath())); // My work-around for new SDKs, doesn't work in Android 10.
}
MediaRecorderReady();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
buttonStart.setEnabled(false);
buttonStart.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonStop.setEnabled(true);
buttonStop.setTextColor(getResources().getColor(R.color.ef_white));
Toast.makeText(RecordAudioActivity.this, "Recording started",
Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
});
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaRecorder.stop();
buttonStop.setEnabled(false);
buttonStop.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonPlayLastRecordAudio.setEnabled(true);
buttonPlayLastRecordAudio.setTextColor(getResources().getColor(R.color.ef_white));
buttonStart.setEnabled(true);
buttonStart.setTextColor(getResources().getColor(R.color.ef_white));
setAudioasMusic.setEnabled(true);
setAudioasMusic.setTextColor(getResources().getColor(R.color.ef_white));
buttonStopPlayingRecording.setEnabled(false);
buttonStopPlayingRecording.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
Toast.makeText(RecordAudioActivity.this, "Recording Completed",
Toast.LENGTH_LONG).show();
}
});
buttonPlayLastRecordAudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) throws IllegalArgumentException,
SecurityException, IllegalStateException {
buttonStop.setEnabled(false);
buttonStop.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonStart.setEnabled(false);
buttonStart.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonStopPlayingRecording.setEnabled(true);
buttonStopPlayingRecording.setTextColor(getResources().getColor(R.color.ef_white));
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(String.valueOf(AudioSavePathInDevice));
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
Toast.makeText(RecordAudioActivity.this, "Recording Playing",
Toast.LENGTH_LONG).show();
}
});
buttonStopPlayingRecording.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View view) {
buttonStop.setEnabled(false);
buttonStop.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonStart.setEnabled(true);
buttonStart.setTextColor(getResources().getColor(R.color.ef_white));
buttonStopPlayingRecording.setEnabled(false);
buttonStopPlayingRecording.setTextColor(getResources().getColor(R.color.ef_black_alpha_50));
buttonPlayLastRecordAudio.setEnabled(true);
buttonPlayLastRecordAudio.setTextColor(getResources().getColor(R.color.ef_white));
setAudioasMusic.setEnabled(true);
setAudioasMusic.setTextColor(getResources().getColor(R.color.ef_white));
if(mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
MediaRecorderReady();
}
}
});
private void requestNewInterstitial() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case RequestPermissionCode:
if (grantResults.length> 0) {
boolean StoragePermission = grantResults[0] ==
PackageManager.PERMISSION_GRANTED;
boolean RecordPermission = grantResults[1] ==
PackageManager.PERMISSION_GRANTED;
if (StoragePermission && RecordPermission) {
Toast.makeText(RecordAudioActivity.this, "Permission Granted",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(RecordAudioActivity.this,"Permission " +
"Denied",Toast.LENGTH_LONG).show();
}
}
break;
}
}
public boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(),
WRITE_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(getApplicationContext(),
RECORD_AUDIO);
return result == PackageManager.PERMISSION_GRANTED &&
result1 == PackageManager.PERMISSION_GRANTED;
}
private void requestPermission() {
ActivityCompat.requestPermissions(RecordAudioActivity.this, new
String[]{WRITE_EXTERNAL_STORAGE, RECORD_AUDIO}, RequestPermissionCode);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void MediaRecorderReady(){
mediaRecorder=new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(AudioSavePathInDevice);
}
}
如果您使用 android 10 或更高
,请不要忘记在您的清单中添加android:grantUriPermissions="true"
android:requestLegacyExternalStorage="true"
经过密切监控和发现,我得到了解决方案。 只需设置 btnStopRecorder enabled 即可解决问题。
现在,对于可能喜欢此代码的任何人 只需用下面的代码替换 btnStartRecorder 按钮。 我现在很开心!
btnStartRecorder.setOnClickListener(new View.OnClickListener() {
@Override*emphasized text*
public void onClick(View view) {
if (checkPermissionFromDevice())
{
pathSave = Environment.getExternalStorageDirectory()
.getAbsolutePath()+"/"
+ UUID.randomUUID().toString()+"_audio_record.amr";
setupMediaRecorder();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
btnPlay.setEnabled(false);
btnStop.setEnabled(false);
btnStopRecorder.setEnabled(true);
Toast.makeText(RecorderActivity.this, "Recording...", Toast.LENGTH_SHORT).show();
}
else {
requestPermission();
}
}
});