Android:MediaRecorder.start()runetimException模糊错误

时间:2017-03-01 15:20:43

标签: java android microphone mediarecorder

我只是尝试创建一个Microphone类,它是一个外观,允许其他代码启动和停止录制。但是我总是得到以下错误:

Caused by: java.lang.RuntimeException: start failed.
                  at android.media.MediaRecorder.start(Native Method)
                  at kwedt.afasie_app.service.Microphone.Record(Microphone.java:41)
                  at kwedt.afasie_app.NamingActivity.recordAudio(NamingActivity.java:69)
                  at kwedt.afasie_app.NamingActivity.commence(NamingActivity.java:51)
                  at kwedt.afasie_app.AphasiaTestActivity.setSubtest(AphasiaTestActivity.java:143)
                  at kwedt.afasie_app.AphasiaTestActivity.menuSelect(AphasiaTestActivity.java:251)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:5637) 
                  at android.view.View$PerformClick.run(View.java:22429) 
                  at android.os.Handler.handleCallback(Handler.java:751) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6119) 

这是我的班级:

public class Microphone
{
private MediaRecorder recorder;

public Microphone()
{
    recorder = new MediaRecorder();
    init();
}

private void init()
{
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);;
}

public void Record(Context context, String path) throws DomainException
{
    File file = new File(context.getFilesDir(), path + Calendar.getInstance()
            .get(Calendar.SECOND)); //save recording under current time
    recorder.setOutputFile(file.getAbsolutePath());
    try
    {
        recorder.prepare();
    }
    catch (IOException e)
    {
        throw new DomainException(e,"Could not prepare microphone." + e.toString());
    }
    recorder.start();
}

public void Stop()
{
    recorder.stop();
    recorder.reset();
    recorder.release();
}
}

我几乎在整个万维网上搜索了一个答案但找不到它。 我还请求了录制音频的权限,并在我的模拟器中看到应用程序有权使用麦克风。

1 个答案:

答案 0 :(得分:2)

模拟器没有麦克风......您必须在真实设备上使用它。