In the code below, my goal is to record the Vorbis OVG file from the microphone. However, whenever I set the output format to MediaRecorder.OutputFormat.WEBM and the encoding to MediaRecorder.AudioEncoder.VORBIS , it crashes with an error:
06-10 22:40:45.465 23050-23050/com.example.x3.apitest E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.x3.apitest, PID: 23050 java.lang.IllegalStateException at android.media.MediaRecorder.native_start(Native Method) at android.media.MediaRecorder.start(MediaRecorder.java:815) at com.example.x3.apitest.MainActivity$StartRecording.onClick(MainActivity.java:57) at android.view.View.performClick(View.java:5210) at android.view.View$PerformClick.run(View.java:21328) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5551) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
If I try to catch .start, this is a -38 error, which means that the microphone is used per - stack overflow.site/questions/295312 / ... - however this cannot be true, because when I just change to MediaRecorder.OutputFormat.MPEG_4 and MediaRecorder.AudioEncoder.AAC , and then run the application in which it runs. If I put it back in webm / vorbis, it will work again.
My code is:
package com.example.x3.apitest; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.media.MediaRecorder; import android.os.Environment; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button1).setOnClickListener(new StartRecording()); findViewById(R.id.button2).setOnClickListener(new StopRecording()); findViewById(R.id.button3).setOnClickListener(new RequestPerms()); } void setText(String str) { TextView tv = (TextView) findViewById(R.id.button1); tv.setText(str); } MediaRecorder recorder = new MediaRecorder(); private class StartRecording implements View.OnClickListener { public void onClick(View arg0) { String rec_path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath() + "/blah.ogg"; Log.d("TAG", rec_path); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
My XML:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.x3.apitest.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button1" android:text="Start Recording" android:layout_width="match_parent" android:layout_height="200dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button2" android:text="Stop Recording" android:layout_width="180dp" android:layout_height="50dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" /> <Button android:id="@+id/button3" android:text="Prompt Permissions" android:layout_width="180dp" android:layout_height="50dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" /> </android.support.constraint.ConstraintLayout>
source share