gpt4 book ai didi

java - 在 Android Studio 中调用 MediaRecorder 会使应用程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:18 25 4
gpt4 key购买 nike

我正在尝试创建一个类来设置和开始录音,但是当我点击按钮时应用程序崩溃了。我将问题隔离到我为 MediRecorder 设置参数的地方。

private void startRec() throws IOException {
if (mrecorder!=null)
mrecorder.release();

mrecorder= new MediaRecorder();

-> mrecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
/*
mrecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrecorder.setOutputFile(MFILE);

mrecorder.prepare();
mrecorder.start();

*/
}

当执行注释开头上方带有箭头的行时,它会崩溃。我还向 list 添加了以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

非常感谢任何帮助。

更新的 LOGCAT

[ 05-12 00:39:13.299 30086:30158 D/] ro.exynos.dss 已启用:005-12 00:39:13.309 30086-30158/record66.record6 D/mali_winsys:new_window_surface 返回 0x3000,[1440x2560]-格式:105-12 00:39:13.319 30086-30086/record66.record6 W/DisplayListCanvas:DisplayListCanvas 在未绑定(bind)的 RenderNode 上启动(没有 mOwningView)05-12 00:39:13.319 30086-30158/record66.record6 D/libGLESv1: DTS_GLAPI: DTS 不允许用于包:record66.record605-12 00:39:13.359 30086-30086/record66.record6 D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=105-12 00:39:13.389 30086-30086/record66.record6 I/时间线:时间线:Activity_idle id:android.os.BinderProxy@682466c 时间:23440132205-12 00:39:15.749 30086-30086/record66.record6 D/ViewRootImpl: ViewPostImeInputStage processPointer 005-12 00:39:15.879 30086-30086/record66.record6 D/ViewRootImpl: ViewPostImeInputStage processPointer 105-12 00:39:15.929 30086-30086/record66.record6 D/AndroidRuntime:关闭 VM05-12 00:39:15.939 30086-30086/record66.record6 E/AndroidRuntime:致命异常:主要 进程:record66.record6,PID:30086 java.lang.RuntimeException: setAudioSource 失败。 在 android.media.MediaRecorder._setAudioSource( native 方法) 在 android.media.MediaRecorder.setAudioSource(MediaRecorder.java:488) 在 record66.record6.MainActivity.startRec(MainActivity.java:58) 在 record66.record6.MainActivity.onClick(MainActivity.java:94) 在 android.view.View.performClick(View.java:5697) 在 android.widget.TextView.performClick(TextView.java:10815) 在 android.view.View$PerformClick.run(View.java:22526) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:158) 在 android.app.ActivityThread.main(ActivityThread.java:7229) 在 java.lang.reflect.Method.invoke( native 方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)05-12 00:39:17.909 30086-30086/record66.record6 I/进程:发送信号。 PID:30086 SIG:9

最佳答案

请看什么logcat是。

让我们知道您遇到了什么错误。所以可以提供帮助。


package com.example.dhrupalpatel.test;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import java.io.File;
import java.io.IOException;

public class MainActivity extends Activity implements View.OnClickListener{
MediaRecorder mrecorder;
boolean mStartRecording=false;

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

start =(Button)findViewById(R.id.start);
stop =(Button)findViewById(R.id.stop);
start.setOnClickListener(this);
stop.setOnClickListener(this);

}

private void startRec() throws IOException {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
File sdCardDirectory= Environment
.getExternalStorageDirectory();
if(mExternalStorageAvailable && !sdCardDirectory.exists())
{
sdCardDirectory.mkdir();
}
File f= new File(sdCardDirectory.getPath()+"/"+System.currentTimeMillis()+".mp3");
if( mrecorder == null ) {
mrecorder = new MediaRecorder();
mrecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mrecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

mrecorder.setOutputFile(f.getPath());
mrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
}
if(!mStartRecording) {

try {
mrecorder.prepare();
mrecorder.start();
mStartRecording = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}

private void stopRec() throws IOException {

if(mStartRecording) {
mStartRecording = false;
mrecorder.stop();
mrecorder.reset();
mrecorder.release();
mrecorder = null;
}
}

@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.start:
try {
startRec();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.stop:
try {
stopRec();
} catch (IOException e) {
e.printStackTrace();
}
break;
}

}
}

关于java - 在 Android Studio 中调用 MediaRecorder 会使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37129254/

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