每当我单击按钮1时,应用程序崩溃(强制关闭)。我无法弄清楚原因。将目录更改为Environment.getExternalStorageDirectory()。getAbsolutePath()+“/ test.3gp”也无法正常工作。 代码是:
package com.example.newaa;
import java.io.IOException;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
public class Record extends Activity{
Button btn1,btn2;
MediaRecorder recorder;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.3gp");
//recorder.setOutputFile("file:///sdcard/sample.3gp");
// recorder.setOnErrorListener(errorListener);
//recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
});
}
}
logcat是
07-05 11:36:35.050: E/AndroidRuntime(220): Uncaught handler: thread main exiting due to
uncaught exception
07-05 11:36:35.070: E/AndroidRuntime(220): java.lang.RuntimeException: setAudioSource failed.
07-05 11:36:35.070: E/AndroidRuntime(220): at android.media.MediaRecorder.setAudioSource(Native Method)
07-05 11:36:35.070: E/AndroidRuntime(220): at com.example.newaa.Record$1.onClick(Record.java:56)
07-05 11:36:35.070: E/AndroidRuntime(220): at android.view.View.performClick(View.java:2364)
07-05 11:36:35.070: E/AndroidRuntime(220): at android.view.View.onTouchEvent(View.java:4179)
07-05 11:36:35.070: E/AndroidRuntime(220): at android.widget.TextView.onTouchEvent(TextView.java:6541)
07-05 11:36:35.070: E/AndroidRuntime(220): at android.view.View.dispatchTouchEvent(View.java:3709)
07-05 11:36:35.070: E/AndroidRuntime(220): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 11:36:35.070: E/AndroidRuntime(220): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 11:36:35.070: E/AndroidRuntime(220): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 11:36:35.070: E/AndroidRuntime(220): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 11:36:35.070: E/AndroidRuntime(220): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
07-05 11:36:35.070: E/AndroidRuntime(220): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
07-05 11:36:35.070: E/AndroidRuntime(220): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
07-05 11:36:35.070: E/AndroidRuntime(220): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-05 11:36:35.070: E/AndroidRuntime(220): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
07-05 11:36:35.070: E/AndroidRuntime(220): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 11:36:35.070: E/AndroidRuntime(220): at android.os.Looper.loop(Looper.java:123)
07-05 11:36:35.070: E/AndroidRuntime(220): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-05 11:36:35.070: E/AndroidRuntime(220): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 11:36:35.070: E/AndroidRuntime(220): at java.lang.reflect.Method.invoke(Method.java:521)
07-05 11:36:35.070: E/AndroidRuntime(220): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-05 11:36:35.070: E/AndroidRuntime(220): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-05 11:36:35.070: E/AndroidRuntime(220): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
你是否在模拟器上尝试,如果是的话,我建议你尝试一下Real设备。 可能是因为模拟器。因为我在模拟器上遇到这样的问题,但在电话上它工作正常
缺少权限>>清单中的audio permission可能是另一个原因。
答案 1 :(得分:0)
我明白了。我认为太容易了:P
我错过了清单文件中的权限