当我从另一个类通过Record.java
调用此Intent
课程,然后点击button1
时,我会收到强制关闭异常。我想知道为什么会这样。此外,logcat说设备断开连接“无法创建媒体播放器”
代码是:
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) {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/sample.3gp");
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) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
});
}
}
这是logcat输出
07-05 10:20:07.598: E/AndroidRuntime(841): Uncaught handler: thread main exiting due to uncaught exception
07-05 10:20:07.609: E/AndroidRuntime(841): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newaa/com.example.newaa.Recipient}: java.lang.IndexOutOfBoundsException: charAt: -1 < 0
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.os.Looper.loop(Looper.java:123)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-05 10:20:07.609: E/AndroidRuntime(841): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 10:20:07.609: E/AndroidRuntime(841): at java.lang.reflect.Method.invoke(Method.java:521)
07-05 10:20:07.609: E/AndroidRuntime(841): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-05 10:20:07.609: E/AndroidRuntime(841): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-05 10:20:07.609: E/AndroidRuntime(841): at dalvik.system.NativeStart.main(Native Method)
07-05 10:20:07.609: E/AndroidRuntime(841): Caused by: java.lang.IndexOutOfBoundsException: charAt: -1 < 0
07-05 10:20:07.609: E/AndroidRuntime(841): at android.text.SpannableStringBuilder.charAt(SpannableStringBuilder.java:110)
07-05 10:20:07.609: E/AndroidRuntime(841): at com.example.newaa.Recipient$2.onTextChanged(Recipient.java:76)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.widget.TextView.setText(TextView.java:2687)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.widget.TextView.setText(TextView.java:2552)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.widget.EditText.setText(EditText.java:71)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.widget.TextView.setText(TextView.java:2527)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.widget.TextView.onRestoreInstanceState(TextView.java:2427)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.view.View.dispatchRestoreInstanceState(View.java:5940)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1127)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1127)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.view.View.restoreHierarchyState(View.java:5919)
07-05 10:20:07.609: E/AndroidRuntime(841): at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1454)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.Activity.onRestoreInstanceState(Activity.java:835)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.Activity.performRestoreInstanceState(Activity.java:807)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)
07-05 10:20:07.609: E/AndroidRuntime(841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473)
答案 0 :(得分:1)
您的问题出在com.example.newaa.Recipient类中
看到这个行号。在com.example.newaa.Recipient $ 2.onTextChanged(Recipient.java:76)
希望您能找到问题或发布您的Recipient.java代码
答案 1 :(得分:1)
让代码像重用任何时间和任何地方。以下是使用Context ref直接调用mediarecorder的代码。并使用它的方法来处理它。
public class AudioRecorder {
final MediaRecorder recorder = new MediaRecorder();
final String path;
Context context;
public AudioRecorder(Context context,String path) {
this.context = context;
this.path = sanitizePath(path);
}
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
}
/**
* Starts a new recording.
*/
public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
Toast.makeText(context,"SD Card is not mounted",Toast.LENGTH_LONG).show();
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
System.out.println("Full path of audio"+path);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
public void Release() throws IOException{
recorder.release();
}
}**
答案 2 :(得分:0)
您不应该像这样调用输出文件。而是使用Environment.getExternalStorageDirectory()
答案 3 :(得分:0)
从不久前的my answer to myself可以看到,Android在视频捕获意图中设置EXTRA_OUTPUT
标志时出现问题。
尝试获取默认文件位置并从那里复制视频:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
try {
AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r");
FileInputStream fis = videoAsset.createInputStream();
File videoFile = new File(Environment.getExternalStorageDirectory(),"<VideoFileName>.mp4");
FileOutputStream fos = new FileOutputStream(videoFile);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
fis.close();
fos.close();
} catch (IOException e) {
// TODO: handle error
}
}
}