0

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)
4

4 回答 4

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();
  }  

}**
于 2012-07-05T05:36:51.220 回答
1

您的问题出在 com.example.newaa.Recipient 类中。
看到这个行号。在 com.example.newaa.Recipient$2.onTextChanged(Recipient.java:76)
希望你会发现问题或发布你的 Recipient.java 的代码

于 2012-07-05T04:58:27.280 回答
0

从我不久前对自己的回答中可以看出,AndroidEXTRA_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
        }
   }
}
于 2012-07-05T05:45:34.880 回答
0

你不应该这样调用输出文件。而是使用Environment.getExternalStorageDirectory()

于 2012-07-05T04:45:29.377 回答