2

我刚刚开始,我正在使用开发人员教程。我已将录制代码修改如下,但出现错误,我不确定。我没有深入的 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:电子/录音(1152):准备()失败
09-26 20:05:37.620:E/MediaRecorder(1152):在无效状态下开始调用:4
09-26 20:05:37.620: D/AndroidRuntime(1152): 关闭 VM
09-26 20:05:37.620: W/dalvikvm(1152): threadid=1: 线程以未捕获的异常退出 (group=0x41c18300)
09-26 20:05:37.630: E/AndroidRuntime(1152): 致命异常: 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): 在 cs4084.panic.button.Record.startRecording(Record.java:55)
09-26 20:05:37.630: E/AndroidRuntime(1152): 在 cs4084.panic.button.Record.onRecord(Record.java:34)
09-26 20:05:37.630: E/AndroidRuntime(1152): 在 cs4084.panic.button.Record.access$0(Record.java:31)
09-26 20:05:37.630: E/AndroidRuntime(1152): 在 cs4084.panic.button.Record$RecordButton$1.onClick(Record.java:71)
09-26 20:05:37.630: E/AndroidRuntime(1152): 在 android.view.View.performClick(View.java:4211)
09-26 20:05:37.630: E/AndroidRuntime(1152): 在 android.view.View$PerformClick.run(View.java:17267)
09-26 20:05:37.630: E/AndroidRuntime(1152): 在 android.os.Handler.handleCallback(Handler.java:615)
09-26 20:05:37.630: E/AndroidRuntime(1152): 在 android.os.Handler.dispatchMessage(Handler.java:92)
09-26 20:05:37.630: E/AndroidRuntime(1152): 在 android.os.Looper.loop(Looper.java:137)
09-26 20:05:37.630: E/AndroidRuntime(1152): 在 android.app.ActivityThread.main(ActivityThread.java:4898)
09-26 20:05:37.630: E/AndroidRuntime(1152): 在 java.lang.reflect.Method.invokeNative(Native Method)
09-26 20:05:37.630: E/AndroidRuntime(1152): 在 java.lang.reflect.Method.invoke(Method.java:511)
09-26 20:05:37.630: E/AndroidRuntime(1152): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
09-26 20:05:37.630: E/AndroidRuntime(1152): 在 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)
4

2 回答 2

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>
于 2012-09-26T19:37:32.973 回答
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)
...

start在此处阅读方法文档的“抛出”部分:http: //developer.android.com/reference/android/media/MediaRecorder.html#start ()

于 2012-09-26T19:12:07.907 回答