我正在尝试为分贝计应用录制声音。我的AndroidManifest具有以下权限:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
当我的记录器在Noise.java
(记录发生的地方)
public void startRecorder() {
if (ActivityCompat.checkSelfPermission(Noise.this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Noise.this, new String[]{Manifest.permission.RECORD_AUDIO}, RECORD_AUDIO);
} else {
if (Recorder == null) {
Recorder = new MediaRecorder();
Recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
Recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
Recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
Recorder.setOutputFile("/dev/null");
try {
Recorder.prepare();
} catch (java.io.IOException ioe) {
android.util.Log.e(LOG_TAG, "IOException: " + android.util.Log.getStackTraceString(ioe));
} catch (java.lang.SecurityException e) {
android.util.Log.e(LOG_TAG, "SecurityException: " + android.util.Log.getStackTraceString(e));
}
try {
Recorder.start();
} catch (java.lang.SecurityException e) {
android.util.Log.e(LOG_TAG, "SecurityException: " + android.util.Log.getStackTraceString(e));
}
}
}
}
它完美适用于API&gt; 23,但在较老的那些我得到了
java.lang.RuntimeException: Unable to resume activity {com.mycompany.audiorecorder/com.mycompany.audiorecorder.Noise}: java.lang.RuntimeException: setAudioSource failed.
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3645)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.RuntimeException: setAudioSource failed.
at android.media.MediaRecorder.setAudioSource(Native Method)
at com.mycompany.audiorecorder.Noise.startRecorder(Noise.java:111)
at com.mycompany.audiorecorder.Noise.onResume(Noise.java:92)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1354)
at android.app.Activity.performResume(Activity.java:7079)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3620)
我选中了“设置&gt;应用&gt; MyApp&gt;权限”,并启用了麦克风权限。
我该怎么做才能解决问题?