gpt4 book ai didi

android - 在 Android 应用程序中录制媒体时崩溃

转载 作者:太空狗 更新时间:2023-10-29 16:06:37 24 4
gpt4 key购买 nike

我刚刚开始使用开发人员教程。我修改了下面的录音代码,但我收到一个错误,我不确定。我对 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() failed09-26 20:05:37.620: E/MediaRecorder(1152): start called in an invalid state: 409-26 20:05:37.620: D/AndroidRuntime(1152): Shutting down VM09-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: main09-26 20:05:37.630: E/AndroidRuntime(1152): java.lang.IllegalStateException09-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)

最佳答案

在您的情况下,准备失败。并且由于您正在捕获 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>

关于android - 在 Android 应用程序中录制媒体时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12608698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com