IllegalStateException异常[启动名为处于无效状态:1]重新启动的Andr​​oid MediaRecorder [英] IllegalStateException [start called in an invalid state: 1] on restarting Android MediaRecorder

查看:296
本文介绍了IllegalStateException异常[启动名为处于无效状态:1]重新启动的Andr​​oid MediaRecorder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现简单的逻辑来启动/停止 MediaRecorder Android的记录。

该周期


  1. 连接到localSocket /设置选项/ mRecorder prepare()。

  2. mRecorder.start();

  3. mRecorder.stop(); mRecorder.reset();

然后,2和3之间环

在第一个周期,1,2,3正常工作如预期,不过,我已经在第一站之后的第二个启动(重启)得到了一个错误。

com.example.app E / MediaRecorder:启动名为处于无效状态:1

什么是MediaRecorder 状态1 ?我怎么错过?
感谢您的输入。

 如果(cmd.equals(连接))
{
    尝试
    {
        sender.connect(新LocalSocketAddress(SOCKET_ADDRESS));
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
        mRecorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AAC);
        mRecorder.setOutputFile(sender.getFileDescriptor());
        mRecorder prepare()。
    }
    赶上(IOException异常E)
    {e.printStackTrace(); }
}
如果(cmd.equals(开始))
{
    尝试
    {
        mRecorder.start();
    }
    赶上(IllegalStateException异常E)
    {e.printStackTrace(); }
}
如果(cmd.equals(停止))
{
    尝试
    {
        mRecorder.stop();
        mRecorder.reset();
    }
    赶上(例外五)
    {e.printStackTrace(); }
}


解决方案

输出文件需要一个实际的文件,而不是一个插座。这是因为MediaRecorder通常需要能够寻求回该文件中,当记录结束更新报头,并且无法以插座寻求

I am trying to implement simple logic to start/stop recording with MediaRecorder of Android.

The cycle is

  1. connect to localSocket / set options / mRecorder.prepare();
  2. mRecorder.start();
  3. mRecorder.stop(); mRecorder.reset();

Then, loop between 2 and 3.

In the first cycle, 1,2,3 works fine as intended, however, I've got an error on the second start(restart) after the first stop.

com.example.app E/MediaRecorder﹕ start called in an invalid state: 1

What is the MediaRecorder state 1? What do I miss? Thanks for your input.

if (cmd.equals("connect"))
{
    try
    {
        sender.connect(new LocalSocketAddress(SOCKET_ADDRESS)); 
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mRecorder.setOutputFile(sender.getFileDescriptor()); 
        mRecorder.prepare(); 
    }
    catch (IOException e)
    {   e.printStackTrace();  } 
} 
if (cmd.equals("start"))
{
    try
    { 
        mRecorder.start(); 
    }
    catch (IllegalStateException e)
    { e.printStackTrace();  } 
} 
if (cmd.equals("stop"))
{ 
    try
    {
        mRecorder.stop();
        mRecorder.reset(); 
    }
    catch (Exception e)
    { e.printStackTrace(); }
}

解决方案

The output file needs to be an actual file, not a socket. This is because MediaRecorder usually needs to be able to seek back in the file to update the header when the recording ends, and you can't seek in a socket.

这篇关于IllegalStateException异常[启动名为处于无效状态:1]重新启动的Andr​​oid MediaRecorder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆