У меня есть приложение, которое использует аудио-рекордер. Но он постоянно выдает ошибку, когда я нажимаю на кнопку «начать запись». Я указал разрешение в манифесте и зарегистрировал их в полосе активности. Для теста я использую galaxy s10 api 29. Есть идеи?
at android.app.ActivityThread.main(ActivityThread.java:7777)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:6425)
at android.view.View.performClick(View.java:7870)
at android.widget.TextView.performClick(TextView.java:14966)
at android.view.View.performClickInternal(View.java:7839)
at android.view.View.access$3600(View.java:886)
at android.view.View$PerformClick.run(View.java:29315)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7777)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
Caused by: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
at android.media.AudioRecord.startRecording(AudioRecord.java:1097)
at ru.mirea.belov.del1.MainActivity.recordStart(MainActivity.java:66)
MainActivity
public class MainActivity extends Activity {
final String TAG = "myLogs";
private static final int REQUEST_CODE_PERMISSION_AUDIO = 100;
private String PERMISSIONS = Manifest.permission.RECORD_AUDIO;
private boolean isWork;
int myBufferSize = 8192;
AudioRecord audioRecord;
boolean isReading = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isWork = hasPermissions(this, PERMISSIONS);
if (!isWork) {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA},
REQUEST_CODE_PERMISSION_AUDIO) ;
}
createAudioRecorder();
Log.d(TAG, "init state = " + audioRecord.getState());
}
void createAudioRecorder() {
int sampleRate = 8000;
int channelConfig = AudioFormat.CHANNEL_IN_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int minInternalBufferSize = AudioRecord.getMinBufferSize(sampleRate,
channelConfig, audioFormat);
int internalBufferSize = minInternalBufferSize * 4;
Log.d(TAG, "minInternalBufferSize = " + minInternalBufferSize
+ ", internalBufferSize = " + internalBufferSize
+ ", myBufferSize = " + myBufferSize);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
sampleRate, channelConfig, audioFormat, internalBufferSize);
}
public void recordStart(View v) {
Log.d(TAG, "record start");
audioRecord.startRecording();
int recordingState = audioRecord.getRecordingState();
Log.d(TAG, "recordingState = " + recordingState);
}
public void recordStop(View v) {
Log.d(TAG, "record stop");
audioRecord.stop();
}
public void readStart(View v) {
Log.d(TAG, "read start");
isReading = true;
new Thread(new Runnable() {
@Override
public void run() {
if (audioRecord == null)
return;
byte[] myBuffer = new byte[myBufferSize];
int readCount = 0;
int totalCount = 0;
while (isReading) {
readCount = audioRecord.read(myBuffer, 0, myBufferSize);
totalCount += readCount;
Log.d(TAG, "readCount = " + readCount + ", totalCount = "
+ totalCount);
}
}
}).start();
}
public void readStop(View v) {
Log.d(TAG, "read stop");
isReading = false;
}
@Override
protected void onDestroy() {
super.onDestroy();
isReading = false;
if (audioRecord != null) {
audioRecord.release();
}
}
public static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) !=
PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull
int[] grantResults) {
if (requestCode == REQUEST_CODE_PERMISSION_AUDIO) {
isWork = grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED;
}
}
}
В манифесте есть разрешение
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
Что не так? Помоги мне, пожалуйста.