Java 调用MediaRecorder会使AndroidStudio中的应用程序崩溃
我正在尝试创建一个设置并启动音频录制的类,但只要我单击按钮,应用程序就会崩溃。Iv将问题隔离到我设置MediRecorder参数的位置Java 调用MediaRecorder会使AndroidStudio中的应用程序崩溃,java,android,android-studio,sdk,Java,Android,Android Studio,Sdk,我正在尝试创建一个设置并启动音频录制的类,但只要我单击按钮,应用程序就会崩溃。Iv将问题隔离到我设置MediRecorder参数的位置 private void startRec() throws IOException { if (mrecorder!=null) mrecorder.release(); mrecorder= new MediaRecorder(); -> mrecorder.setAudioSource(MediaRecorder
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();
*/
}
当执行注释开头上方带有箭头的行时,它会崩溃。我还向清单添加了以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
非常感谢您的帮助
更新日志
[05-12 00:39:13.299 30086:30158 D/]
已启用ro.exynos.dss:0
05-12 00:39:13.309 30086-30158/record66.record6 D/mali_winsys:new_window_surface返回0x3000[1440x2560]-格式:1
05-12 00:39:13.319 30086-30086/record66.record6 W/DisplayListCanvas:DisplayListCanvas在未索引的RenderNode上启动(不带mOwningView)
05-12 00:39:13.319 30086-30158/record66.record6 D/libGLESv1:DTS_GLAPI:DTS不允许用于包:record66.record6
05-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)或=1
05-12 00:39:13.389 30086-30086/record66.record6 I/Timeline:Timeline:Activity\u idle id:android.os。BinderProxy@682466c时间:234401322
05-12 00:39:15.749 30086-30086/record66.record6 D/ViewRootImpl:ViewPostimeiInputStage进程指针0
05-12 00:39:15.879 30086-30086/record66.record6 D/ViewRootImpl:ViewPostimeiInputStage进程指针1
05-12 00:39:15.929 30086-30086/record66.record6 D/AndroidRuntime:关闭虚拟机
05-12 00:39:15.939 30086-30086/record66.record6 E/AndroidRuntime:致命异常:主
流程:record66.record6,PID:30086
java.lang.RuntimeException:setAudioSource失败。
在android.media.MediaRecorder.\u setAudioSource(本机方法)
位于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(本机方法)
在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说的,张贴出来,这样我们就可以帮助了。@Dhrupal我是新手,我不确定你指的是哪一个日志,但我直接在我的手机上运行程序,当它崩溃时,它会给我一个通用的“发生错误,此程序将关闭”好的,我想我明白了。编辑的原创帖子让我知道如果是这样谢谢你我会试试的。我想知道我的代码不起作用的原因是什么?是因为我从来没有实现过点击式侦听器吗?我添加了您编写的代码,但仍然遇到相同的问题。我用你的最新代码更新了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;
}
}
}