2012-10-02 53 views
0

Im試圖做一些簡單的事情,錄製音頻和視頻,無需預覽。Android - 準備媒體錄製器失敗

這裏是我的代碼:

MediaRecorder mediaRecorder = new MediaRecorder(); 

// Configure the input sources 
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 

// Set the output format 
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 

// Specify the audio and video encoding   
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); 

    //Specify the outputfile 
    mediaRecorder.setOutputFile("/sdcard/Video/myoutputfile.mp4"); 

mediaRecorder.start(); 

In the AndroidManisfest.xml: 
<user-permission android:name="android.permission.RECORD_AUDIO" /> 
<user-permission android:name="android.permission.RECORD_VIDEO" /> 

這將停止應用程序時,它啓動。

任何人有任何想法?

+0

不要忘記<使用的許可機器人:名稱=「android.permission.WRITE_EXTERNAL_STORAGE」 /> – mario

+0

錯字:「用戶許可」應該是「使用的許可」。 :-) – darrenp

回答

0

嘗試在開始記錄前調用此函數;

mediaRecorder.prepare(); 

另外,記住你缺少一些必要的「特色」,你必須一套準備調用之前。 這裏似乎是你缺少的一些方法;

setPreviewDisplay(); 
setMaxDuration((int) #); 
setOnInfoListener(); 
setVideoSize(#, #); 
setVideoFrameRate(15); 
+0

我再次嘗試,並得到相同的錯誤,應用程序崩潰之前開始! –

+1

有沒有關於我在Galaxy Nexus中測試這個事實的信息? Android 4.1.1。我讀過這段代碼在android 2.3或更高版本中不起作用。 –

+0

你能編輯你的堆棧跟蹤嗎?我在Galaxy Nexus上使用了一些奇怪的設置,但我確實記得我的星系連線崩潰了與MediaRecorder類的很多! (大驚喜,因爲它應該是一個谷歌支持的手機使用谷歌api's?哈哈) 使用我的「特殊」設置從來沒有爲我工作,因爲顯然它只會在我的聯繫,然後其他手機會有問題: ( –