第一次为Android编写应用程序时,只有两个按钮都可以,所以我决定尝试包含MediaRecorder。 当我按“开始”时,我收到消息“不幸的是,按钮教程已停止”。用“OK”。 有人可能会建议出现什么问题以及为什么我需要做些什么来纠正它? 这是我的“MainActivity.java”
package example.com;
import java.io.IOException;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity
extends Activity
implements OnClickListener
{
private static final String TAG = null;
private String fileName;
private MediaRecorder myRecorder = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating Button variable
Button Btn_Start = (Button) this.findViewById(R.id.btStart);
Button Btn_Stop = (Button) this.findViewById(R.id.btStop);
//Adding Listener to button
Btn_Start.setOnClickListener(this);
Btn_Stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btStart:
Log.i("onClick", "btStart BEGIN");
try {
startRecording();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("onClick", "btStart END");
break;
case R.id.btStop:
Log.i("onClick", "btStop BEGIN");
stopRecording();
Log.i("onClick", "btStop END");
break;
}
}
private void stopRecording() {
myRecorder.stop();
}
private void startRecording() throws IllegalStateException, IOException {
Log.d(TAG,"void startRecording");
myRecorder = new MediaRecorder();
Log.d(TAG,"new MediaRecorder");
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
Log.d(TAG,"setAudioSource");
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
Log.d(TAG,"setOutputFormat");
//myRecorder.setOutputFile(mFileName);
fileName = Environment.getExternalStorageDirectory() + "/audiotest.3gpp";
myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
myRecorder.setOutputFile(fileName);
myRecorder.prepare();
myRecorder.start();
}
}
这是“LogCat”
01-27 17:40:34.154: I/onClick(6116): btStart BEGIN
01-27 17:40:34.154: D/(6116): void startRecording
01-27 17:40:34.164: D/(6116): new MediaRecorder
01-27 17:40:34.164: D/AndroidRuntime(6116): Shutting down VM
01-27 17:40:34.164: W/dalvikvm(6116): threadid=1: thread exiting with uncaught exception (group=0x417ebba8)
01-27 17:40:34.164: E/AndroidRuntime(6116): FATAL EXCEPTION: main
01-27 17:40:34.164: E/AndroidRuntime(6116): Process: example.com, PID: 6116
01-27 17:40:34.164: E/AndroidRuntime(6116): java.lang.RuntimeException: setAudioSource failed.
01-27 17:40:34.164: E/AndroidRuntime(6116): at android.media.MediaRecorder.setAudioSource(Native Method)
01-27 17:40:34.164: E/AndroidRuntime(6116): at example.com.MainActivity.startRecording(MainActivity.java:68)
01-27 17:40:34.164: E/AndroidRuntime(6116): at example.com.MainActivity.onClick(MainActivity.java:43)
01-27 17:40:34.164: E/AndroidRuntime(6116): at android.view.View.performClick(View.java:4438)
01-27 17:40:34.164: E/AndroidRuntime(6116): at android.view.View$PerformClick.run(View.java:18422)
01-27 17:40:34.164: E/AndroidRuntime(6116): at android.os.Handler.handleCallback(Handler.java:733)
01-27 17:40:34.164: E/AndroidRuntime(6116): at android.os.Handler.dispatchMessage(Handler.java:95)
01-27 17:40:34.164: E/AndroidRuntime(6116): at android.os.Looper.loop(Looper.java:136)
01-27 17:40:34.164: E/AndroidRuntime(6116): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-27 17:40:34.164: E/AndroidRuntime(6116): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 17:40:34.164: E/AndroidRuntime(6116): at java.lang.reflect.Method.invoke(Method.java:515)
01-27 17:40:34.164: E/AndroidRuntime(6116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-27 17:40:34.164: E/AndroidRuntime(6116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-27 17:40:34.164: E/AndroidRuntime(6116): at dalvik.system.NativeStart.main(Native Method)
这是我的AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="example.com.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
有关我正在尝试在Google Nexus 7上运行此信息的信息
非常感谢
答案 0 :(得分:0)
我的猜测是你缺少所需的权限,请尝试
<uses-permission android:name="android.permission.RECORD_AUDIO" />
您还需要:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
将此添加到清单:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />