0

When I call this Record.java class via Intent from another class and then click on the button1, i get a forced close exception. I want to know why this is happening. Also, the logcat says something like device disconnected "unable to create media player"

The code is:

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

}

Here is the logcat output

  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)
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
Aakash Anuj
  • 3,773
  • 7
  • 35
  • 47

4 Answers4

1

Your problem is in com.example.newaa.Recipient class.
see this line number. at com.example.newaa.Recipient$2.onTextChanged(Recipient.java:76)
Hope you will find problem or post the code of your Recipient.java

Waqas
  • 4,411
  • 6
  • 27
  • 31
  • No. How can the error be in Recipient.java when its calling the Record.java without any error? – Aakash Anuj Jul 05 '12 at 05:02
  • According to your logcat : 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) – Waqas Jul 05 '12 at 05:03
  • Maybe you have pasted the old log. Please make sure you have pasted the latest log. – Waqas Jul 05 '12 at 05:05
  • Meanwhile you should try replacing this "/sdcard/sample.3gp" by this one Environment.getExternalStorageDirectory() .getAbsolutePath() + "/sample.3gp" – Waqas Jul 05 '12 at 05:06
1

Make Code like Reuse any time and anyWhere. Here is code to directly call the mediarecorder with Context ref. and also use it's methods to handle it.

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

}**
Ashok Domadiya
  • 1,124
  • 13
  • 31
0

You should not be calling the output file like that. Instead use Environment.getExternalStorageDirectory()

Orlymee
  • 2,349
  • 1
  • 22
  • 24
  • you can use it like this. File file = new File(Environment.getExternalStorageDirectory() .getAbsolutePath() + "/temp.pcm"); – Waqas Jul 05 '12 at 05:00
0

as you can see from my answer to myself from not too long ago, Android has an issue with setting the EXTRA_OUTPUT flag in the video capture intent. try getting the default file location instead and copying the video from there like this:

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
        }
   }
}
Community
  • 1
  • 1
thepoosh
  • 12,497
  • 15
  • 73
  • 132