在Android应用中录制媒体时崩溃

时间:2012-09-26 19:06:33

标签: android recording

我刚刚开始,我正在使用开发人员教程。我已经修改了下面的录制代码,但是我收到了一个错误,我不确定。我没有深入的Java级别,而且我只有大约3周的时间进入对象和类。这个应用程序适用于我们没有规范的项目。

private static final String LOG_TAG = "Recording";

//start recording stuff

private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;

private void onRecord(boolean start)
{
    if (start)
        startRecording();
    else
        stopRecording();

}

private void startRecording()
{
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile("/sdCard0/panic_record/" + System.currentTimeMillis()+ ".3gp");
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }

    mRecorder.start();
}

private void stopRecording()
{
    mRecorder.stop();
    mRecorder.release();
    mRecorder = null;
}

class RecordButton extends Button {
    boolean mStartRecording = true;

    OnClickListener clicker = new OnClickListener() {
        public void onClick(View v) {
            onRecord(mStartRecording);
            if (mStartRecording) {
                setText("Stop recording");
            } else {
                setText("Start recording");
            }
            mStartRecording = !mStartRecording;
        }
    };

    public RecordButton(Context ctx) {
        super(ctx);
        setText("Start recording");
        setOnClickListener(clicker);
    }


}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    LinearLayout ll = new LinearLayout(this);
    mRecordButton = new RecordButton(this);
    ll.addView(mRecordButton,
        new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            0));

    setContentView(ll);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_record, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

LogCat输出

09-26 20:05:37.620: E/Recording(1152): prepare() failed
09-26 20:05:37.620: E/MediaRecorder(1152): start called in an invalid state: 4
09-26 20:05:37.620: D/AndroidRuntime(1152): Shutting down VM
09-26 20:05:37.620: W/dalvikvm(1152): threadid=1: thread exiting with uncaught exception (group=0x41c18300)
09-26 20:05:37.630: E/AndroidRuntime(1152): FATAL EXCEPTION: main
09-26 20:05:37.630: E/AndroidRuntime(1152): java.lang.IllegalStateException
09-26 20:05:37.630: E/AndroidRuntime(1152):     at android.media.MediaRecorder.start(Native Method)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at cs4084.panic.button.Record.startRecording(Record.java:55)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at cs4084.panic.button.Record.onRecord(Record.java:34)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at cs4084.panic.button.Record.access$0(Record.java:31)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at cs4084.panic.button.Record$RecordButton$1.onClick(Record.java:71)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at android.view.View.performClick(View.java:4211)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at android.view.View$PerformClick.run(View.java:17267)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at android.os.Handler.handleCallback(Handler.java:615)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at android.os.Looper.loop(Looper.java:137)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at android.app.ActivityThread.main(ActivityThread.java:4898)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at java.lang.reflect.Method.invoke(Method.java:511)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
09-26 20:05:37.630: E/AndroidRuntime(1152):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:5)

在您的情况下,准备失败。因为您正在捕获IO异常,我想这可能是由于文件夹/ sdCard0 / panic_record /不可用于写访问。

通常sdcard文件夹是/ sdcard而不是/ sdCard0(注意C的大小写)。更好的方法是

String path = Environment.getExternalStorageDirectory().getAbsolutePath()
 + "/panic_record/" +  System.currentTimeMillis()+ ".3gp";

mRecorder.setOutputFile(path);

别忘了添加WRITE_EXTERNAL_STORAGE权限

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

答案 1 :(得分:2)

这是你例外的相关部分:

09-26 20:05:37.630: E/AndroidRuntime(1152): FATAL EXCEPTION: main 
09-26 20:05:37.630: E/AndroidRuntime(1152): java.lang.IllegalStateException 
09-26 20:05:37.630: E/AndroidRuntime(1152): at android.media.MediaRecorder.start(Native Method) 
09-26 20:05:37.630: E/AndroidRuntime(1152): at cs4084.panic.button.Record.startRecording(Record.java:55)
...

阅读&#34;投掷&#34;此处start方法文档的部分:http://developer.android.com/reference/android/media/MediaRecorder.html#start()