#android #prepared-statement #record #mediarecorder
Вопрос:
Я пытался создать приложение для записи, но когда я попытался перекодировать с помощью эмулятора, они перестали работать, когда я попытался записать свой голос, он разбился! И на самом деле, я использую этот источник https://www.geeksforgeeks.org/audio-recorder-in-android-with-example/https://github.com/ChaitanyaMunje/QR_Code_Scanner/tree/Audio_recorder
MainActivity.java
package com.gtappdevelopers.camviewlibrary;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
public class MainActivity extends AppCompatActivity {
//Intializing all variables..
private TextView startTV, stopTV, playTV, stopplayTV, statusTV;
//creating a variable for medi recorder object class.
private MediaRecorder mRecorder;
// creating a variable for mediaplayer class
private MediaPlayer mPlayer;
//string variable is created for storing a file name
private static String mFileName = null;
// constant for storing audio permission
public static final int REQUEST_AUDIO_PERMISSION_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize all variables with their layout items.
statusTV = findViewById(R.id.idTVstatus);
startTV = findViewById(R.id.btnRecord);
stopTV = findViewById(R.id.btnStop);
playTV = findViewById(R.id.btnPlay);
stopplayTV = findViewById(R.id.btnStopPlay);
stopTV.setBackgroundColor(getResources().getColor(R.color.gray));
playTV.setBackgroundColor(getResources().getColor(R.color.gray));
stopplayTV.setBackgroundColor(getResources().getColor(R.color.gray));
startTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// start recording method will start the recording of audio.
startRecording();
}
});
stopTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//pause Recording method will pause the recording of audio.
pauseRecording();
}
});
playTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// play audio method will play the audio which we have recorded
playAudio();
}
});
stopplayTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// pause play method will pause the play of audio
pausePlaying();
}
});
}
private void startRecording() {
// check permission method is used to check that the user has granted permission to record nd store the audio.
if (CheckPermissions()) {
//setbackgroundcolor method will change the background color of text view.
stopTV.setBackgroundColor(getResources().getColor(R.color.purple_200));
startTV.setBackgroundColor(getResources().getColor(R.color.gray));
playTV.setBackgroundColor(getResources().getColor(R.color.gray));
stopplayTV.setBackgroundColor(getResources().getColor(R.color.gray));
//we are here initializing our filename variable with the path of the recorded audio file.
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName = "/AudioRecording.3gp";
//below method is used to initialize the media recorder clss
mRecorder = new MediaRecorder();
//below method is used to set the audio source which we are using a mic.
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//below method is used to set the output format of the audio.
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//below method is used to set the audio encoder for our recorded audio.
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//below method is used to set the output file location for our recorded audio
mRecorder.setOutputFile(mFileName);
try {
//below mwthod will prepare our audio recorder class
mRecorder.prepare();
} catch (IOException e) {
Log.e("TAG", "prepare() failed");
}
// start method will start the audio recording.
mRecorder.start();
statusTV.setText("Recording Started");
} else {
//if audio recording permissions are not granted by user below method will ask for runtime permission for mic and storage.
RequestPermissions();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
// this method is called when user will grant the permission for audio recording.
switch (requestCode) {
case REQUEST_AUDIO_PERMISSION_CODE:
if (grantResults.length > 0) {
boolean permissionToRecord = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean permissionToStore = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (permissionToRecord amp;amp; permissionToStore) {
Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_LONG).show();
}
}
break;
}
}
public boolean CheckPermissions() {
//this method is used to check permission
int result = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), RECORD_AUDIO);
return result == PackageManager.PERMISSION_GRANTED amp;amp; result1 == PackageManager.PERMISSION_GRANTED;
}
private void RequestPermissions() {
// this method is used to request the permission for audio recording and storage.
ActivityCompat.requestPermissions(MainActivity.this, new String[]{RECORD_AUDIO, WRITE_EXTERNAL_STORAGE}, REQUEST_AUDIO_PERMISSION_CODE);
}
public void playAudio() {
stopTV.setBackgroundColor(getResources().getColor(R.color.gray));
startTV.setBackgroundColor(getResources().getColor(R.color.purple_200));
playTV.setBackgroundColor(getResources().getColor(R.color.gray));
stopplayTV.setBackgroundColor(getResources().getColor(R.color.purple_200));
//for playing our recorded audio we are using media player class.
mPlayer = new MediaPlayer();
try {
//below method is used to set the data source which will be our file name
mPlayer.setDataSource(mFileName);
//below method will prepare our media player
mPlayer.prepare();
//below method will start our media player.
mPlayer.start();
statusTV.setText("Recording Started Playing");
} catch (IOException e) {
Log.e("TAG", "prepare() failed");
}
}
public void pauseRecording() {
stopTV.setBackgroundColor(getResources().getColor(R.color.gray));
startTV.setBackgroundColor(getResources().getColor(R.color.purple_200));
playTV.setBackgroundColor(getResources().getColor(R.color.purple_200));
stopplayTV.setBackgroundColor(getResources().getColor(R.color.purple_200));
//below method will stop the audio recording.
mRecorder.stop();
//below method will release the media recorder class.
mRecorder.release();
mRecorder = null;
statusTV.setText("Recording Stopped");
}
public void pausePlaying() {
//this method will release the media player class and pause the playing of our recorded audio.
mPlayer.release();
mPlayer = null;
stopTV.setBackgroundColor(getResources().getColor(R.color.gray));
startTV.setBackgroundColor(getResources().getColor(R.color.purple_200));
playTV.setBackgroundColor(getResources().getColor(R.color.purple_200));
stopplayTV.setBackgroundColor(getResources().getColor(R.color.gray));
statusTV.setText("Recording Play Stopped");
}
}
и журнал ошибок также есть
2021-08-28 18:29:29.356 19444-19460/com.gtappdevelopers.camviewlibrary W/System: A resource failed to call close.
2021-08-28 18:29:30.321 19444-19444/com.gtappdevelopers.camviewlibrary E/TAG: prepare() failed
2021-08-28 18:29:30.321 19444-19444/com.gtappdevelopers.camviewlibrary E/MediaRecorder: start called in an invalid state: 4
2021-08-28 18:29:30.321 19444-19444/com.gtappdevelopers.camviewlibrary D/AndroidRuntime: Shutting down VM
2021-08-28 18:29:30.327 19444-19444/com.gtappdevelopers.camviewlibrary E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gtappdevelopers.camviewlibrary, PID: 19444
java.lang.IllegalStateException
at android.media.MediaRecorder.start(Native Method)
at com.gtappdevelopers.camviewlibrary.MainActivity.startRecording(MainActivity.java:110)
at com.gtappdevelopers.camviewlibrary.MainActivity.access$000(MainActivity.java:24)
at com.gtappdevelopers.camviewlibrary.MainActivity$1.onClick(MainActivity.java:55)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-08-28 18:29:30.351 19444-19444/com.gtappdevelopers.camviewlibrary I/Process: Sending signal. PID: 19444 SIG: 9
Could you help me, why did the Program crash?