2012-09-13 27 views
1

我正在製作一個android應用程序來錄製聲音。我一直在使用MediaRecorder類。MediaRecorder實例未發佈

在任何設備上首次完成記錄時,錄製成功完成。但下次不行。

如果我嘗試在另一臺機器上的代碼,錄製成功完成。這是因爲MediaRecorder實例沒有被應用程序釋放,即使我正在調用方法release()

這是我的代碼 -

public class NewClass extends Activity { 
private static String sFileName = null; 
private static String sFileNameMSD = null; 
private static String sPlayFile = null; 
public MediaRecorder mRecorder = null; 
private String mPn_id; 
private String mDescription; 
private String mTask_flag; 
private String mPatient_name; 
private String mPatient_id; 
private String mIs_upload = "N"; 
private Context mContext; 
Button btnStart; 
Button btnStop; 
private int mReuqestcode; 
NewClass myActivity; 
SeekBar seekBar; 
int totalTime; 
private final Handler handler = new Handler(); 
private final String FILE_EXTENTION_AMR = ".amr"; 
private final String FILE_EXTENTION_MSD = ".msd"; 
protected boolean recodeFlag = false;; 
private static final String TAG = GridAllActivity.class.getName(); 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle);requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.transcriptionrecord_activity); 
    mContext = this; 
    myActivity = this; 
    Main.setTitle(myActivity, mContext); 

    Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(
      mContext)); 
    TextView txtTitle = (TextView) findViewById(R.id.txt_Title1); 
    txtTitle.setText("Transcription"); 
    TextView txtOption = (TextView) findViewById(R.id.txtOptionName); 
    TextView txtPatientName = (TextView) findViewById(R.id.txtPatientName); 
    setProperty(); 
    txtOption.setText("" + mDescription); 
    txtPatientName.setText("" + mPatient_name); 
} 
private void onRecord(boolean start) throws SettingNotFoundException { 
    if (start) { 
     startRecording(); 
    } else { 
     stopRecording(); 
     } 
} 

private void startRecording() throws SettingNotFoundException { 
    mRecorder = new MediaRecorder(); 

    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); 
    mRecorder.setOutputFile(sFileName); 
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    try { 

     mRecorder.prepare(); 

     mRecorder.start(); 
    } catch (IOException e) { 

     recodeFlag=true; 
     Log.e("1", "prepare() failed"); 
     e.printStackTrace(); 
    } 

} 

private void stopRecording() throws SettingNotFoundException { 

    mRecorder.stop(); 
    mRecorder.release(); 
    Log.d("1", "mRecorder released"); 
    mRecorder = null; 
    System.gc(); 
} 

public void setRecordPath() throws IOException { 

    sFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); 

    sFileName += Global.creteFolderPath("IMSEMR" + mTask_flag + "_" 
      + mPn_id + FILE_EXTENTION_AMR); 
    sFileNameMSD = "IMSEMR" + mTask_flag + "_" + mPn_id 
      + FILE_EXTENTION_MSD; 
    String state = android.os.Environment.getExternalStorageState(); 
    if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) { 
     Global.alertbox("", "SD Card is not mounted.", mContext); 
     throw new IOException("SD Card is not mounted. It is " + state 
       + "."); 
    } 

    File directory = new File(sFileName).getParentFile(); 
    if (!directory.exists() && !directory.mkdirs()) { 
     directory.mkdirs(); 
     throw new IOException("Path to file could not be created."); 
    } 

} 

public void setRecodingLayout() { 

    btnStop.setEnabled(false); 
    try { 
     btnStart.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       boolean mStartRecording = true; 
       try { 
        btnStop.setEnabled(true); 
        btnStart.setEnabled(false); 
        onRecord(mStartRecording); 

       } catch (SettingNotFoundException e) { 
        e.printStackTrace(); 
       } 

      } 
     });   
        btnStop.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       try { 
       boolean mStartRecording = false; 
       try { 
        onRecord(mStartRecording); 
       } catch (SettingNotFoundException e) { 
        e.printStackTrace(); 
       } 

      } catch (SQLiteException e) { 
       // TODO: handle exception 
      } catch (Exception e) { 
       // TODO: handle exception 
      } 
         } 
     }); 

    } catch (NullPointerException e) { 

    } 
} 

public void setProperty() { 
    Bundle bundel = getIntent().getExtras(); 
    mReuqestcode = (Integer) bundel.get("REQUEST_CODE"); 
    // property for Record 
    if (mReuqestcode == 1) { 
     mPn_id = (String) bundel.get("PN_ID"); 
     mDescription = (String) bundel.get("DESCRIPTION"); 
     mTask_flag = (String) bundel.get("TASK_FLAG"); 
     mPatient_id = (String) bundel.get("PATIENT_ID"); 
     mPatient_name = (String) bundel.get("PATIENT_NAME"); 
     btnStart = (Button) findViewById(R.id.btnStart); 
     btnStop = (Button) findViewById(R.id.btnStop); 
     btnStop.setEnabled(false); 
     setRecodingLayout(); 
     try { 
      setRecordPath(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    } 
    } 
+0

請出示你的代碼。 –

回答

0

你需要你想記錄每次創建介質記錄的新實例(確保釋放前一個)。

更新 - 對不起,只需要在對象上調用release即可創建新實例。否則,你可以叫reset, 閱讀:http://developer.android.com/reference/android/media/MediaRecorder.html

例: 可以說,你有2個按鈕的活動,記錄按鈕和播放按鈕

public class MyRecordDemo extends Activity { 
    private static final String PATH_NAME = "/sdcard/myfile.3gp"; //probably shouldn't hardcode this 
    private Button mRecordButton, mStopButton; 
    private MediaRecorder mRecorder = null; 
    private boolean mIsRecording = false; 
    public void onCreate(Bundle state) { 
     super.onCreate(state); 
     setContentView(R.layout.recorder_demo); 
     mRecordButton = (Button) findViewById(R.id.record_btn); 
     mStopButton = (Button) findViewById(R.id.stop_btn); 
     mStopButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      if (mIsRecording && mRecorder != null) { 
       mRecorder.stop(); 
       mRecorder.release(); 
      } 
      } 
     }); 
     mRecordButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      if (mIsRecording && mRecorder != null) { 
       mRecorder.stop(); 
       mRecorder.release(); 
      } 
      mRecorder = new MediaRecorder(); 
      mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
      mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
      mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
      mRecorder.setOutputFile(PATH_NAME); 
      mRecorder.prepare(); 
      mRecorder.start() 
     } 

     }); 
    } 

} 
+0

這是我的代碼 mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); mRecorder.setOutputFile(sFileName); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mRecorder.prepare(); mRecorder.start(); 我每次創建MEDIARECORDER的新實例進行錄製。 不知道是什麼問題。每次在任何設備上進行首次錄製後,我都會收到準備()失敗錯誤。 – user17128

+0

編輯你的問題,並把你的代碼在那裏..不只是上面的代碼,但整個活動,如果可能的話。 –