2012-07-05 21 views
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) 
+0

發佈您的logcat輸出。 – Rajesh

+0

@Rajesh我現在已經做到了。 –

+0

何時獲得FC?在開始活動或按下按鈕時?引發FC的異常的堆棧跟蹤丟失。請張貼含有蹤跡完整logcat的輸出'com.example.newaa.Record' – Rajesh

回答

1

你的問題是com.example.newaa.Recipient類。
看到這一行號碼。在com.example.newaa.Recipient $ 2.onTextChanged(Recipient.java:76)
希望你會發現問題或發表您的Recipient.java

+0

不可以。當它調用Record.java時沒有任何錯誤,Recipient.java中的錯誤如何發生? –

+0

根據你的logcat: 引起:java.lang.IndexOutOfBoundsException:charAt:-1 <0 07-05 10:20:07.609:E/AndroidRuntime(841):at android.text.SpannableStringBuilder.charAt(SpannableStringBuilder。 Java的:110) 十月七日至5日:20:07.609:E/AndroidRuntime(841):在com.example.newaa.Recipient $ 2.onTextChanged(Recipient.java:76) – Waqas

+0

也許你已經粘貼舊的日誌。請確保您粘貼了最新的日誌。 – Waqas

0

你不應該調用輸出這樣的文件。而是使用Environment.getExternalStorageDirectory()

+0

你能告訴確切的格式來使用它嗎? –

+0

你可以像這樣使用它。 File file = new File(Environment.getExternalStorageDirectory() \t \t \t \t .getAbsolutePath()+「/temp.pcm」); – Waqas

1

使代碼的代碼重用像任何時間,任何地點。這裏是代碼來直接調用與上下文refrec媒體記錄器。並使用它的方法來處理它。

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

}** 
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 
     } 
    } 
}