androidstudio中的java调用mediarecorder程序崩溃

kxeu7u2r  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(382)

我试图创建一个类,设置和开始录音,但一旦我点击按钮的应用程序崩溃。我把问题隔离到了我为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();

    */
}

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

<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已启用:0 05-12 00:39:13.309 30086-30158/record66.record6 d/mali_winsys:new_window_surface返回0x3000,[1440x2560]-format: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\u glapi:dts不允许用于包:record66.record6 05-12 00:39:13.359 30086-30086/record66.record6d/viewrootimpl:msg\u resized\u 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 time:234401322 05-12 00:39:15.749 30086-30086/record66.record6 d/viewrootimpl:viewpostimeinputstage进程指针0 05-12 00:39:15.879 30086-30086/record66.record6 d/viewrootimpl:viewpostimeinputstage进程指针105-12 00:39:15.929 30086-30086/record66.record6 d/androidruntime:关闭vm 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信号:9

gopyfrb3

gopyfrb31#

请看看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;
    }

}
}

相关问题