gpt4 book ai didi

android - java.lang.RuntimeException : setAudioSource failed 错误

转载 作者:可可西里 更新时间:2023-11-01 18:50:48 57 4
gpt4 key购买 nike

我是安卓开发的新手。我只是想用 6.0.1 Marshmallow 设备测试 android studio(2.1.1) 来录制音频。

public class MainActivity extends AppCompatActivity {

Button start, stop;
public MediaRecorder recorder = null;
public String fileextn = ".mp4";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

start = (Button) findViewById(R.id.start_button);
stop = (Button) findViewById(R.id.stop_button);

start.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {

switch (v.getId()) {
case R.id.start_button:
startRecord();
case R.id.stop_button:
stoprecord();
}
}

}
);

}

public void startRecord() {
recorder = new MediaRecorder();
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setOutputFile(getFilePath());

try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void stoprecord() {

if (recorder != null) {
recorder.stop();
recorder.reset();

recorder.release();
recorder = null;
}

}


private String getFilePath() {

String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, "MediaRecorderSample");

if (!file.exists())
file.mkdirs();

return (file.getAbsolutePath() + "/" + fileextn);
}


}

顺便说一下,我已经在 manifext.xml 文件中包含了使用权限。

这是我在 Android Monitor 中得到的:

  05-18 11:08:36.576 10414-10414/com.example.gk.audiocapture E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gk.audiocapture, PID: 10414
java.lang.RuntimeException: stop failed.
at android.media.MediaRecorder.stop(Native Method)
at com.example.gk.audiocapture.MainActivity.stoprecord(MainActivity.java:65)
at com.example.gk.audiocapture.MainActivity$1.onClick(MainActivity.java:35)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

我尝试了一些答案,但没有找到任何答案。

我的 AndroidManifest.xml:

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gk.audiocapture">
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

最佳答案

我遇到了完全相同的问题,并通过请求录制音频的权限设法解决了这个问题:

if (ActivityCompat.checkSelfPermission(activity(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(activity(), new String[]{Manifest.permission.RECORD_AUDIO}, BuildDev.RECORD_AUDIO);

} else {

startRecording();

}

在哪里

BuildDev.RECORD_AUDIO 是 public static final int RECORD_AUDIO = 0;

关于android - java.lang.RuntimeException : setAudioSource failed 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290752/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com