2011-10-10 63 views
2

我工作的一個自定義的錄像課,我有一些問題讓相機預覽活動第一次出現時顯示。我打電話surfaceCreated回調在這個函數:錄像 - 無法啓動相機預覽

private void initRecorder(Surface surface) throws IOException { 
// It is very important to unlock the camera before doing setCamera 
// or it will results in a black preview 
if(camera == null) { 
    camera = Camera.open(); 
    camera.unlock(); 
} 

if(recorder == null) 
    recorder = new MediaRecorder(); 

recorder.setPreviewDisplay(surface); 
recorder.setCamera(camera); 

camera.startPreview(); 

recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
recorder.setOutputFile("/sdcard/test.mp4"); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 
recorder.setVideoEncodingBitRate(15000000); 
recorder.setMaxDuration(10000); // length of video in MS 
recorder.setVideoSize(720, 480); 
recorder.setVideoFrameRate(30); 


try { 
    recorder.prepare(); 
} catch (IllegalStateException e) { 
    // This is thrown if the previous calls are not called with the 
    // proper order 
    e.printStackTrace(); 
} 
} 

當活動開始時,我的應用程序崩潰說:

java.lang.RuntimeException: startPreview failed

上面的錯誤,我注意到一個行說:

attempt to use a locked camera from a different process (old pid 4894, new pid 6405)

當我逐句通過代碼時,該錯誤發生在camera.startPreview()線。如果我從我的代碼中刪除該行,在我致電recorder.start()後,預覽顯示正常,在此之前,我只有一個黑屏與我的錄製按鈕。一旦我停止錄製,預覽繼續顯示正常(我在停止錄製後調用camera.startPreview())。

由於我在啓動預覽前只打了幾行,而且兩個調用發生在同一個函數中,我怎麼會有這個錯誤?

編輯:我測試了相同的代碼減去調用Droid X2和Droid 1上的startPreview(),它工作正常。它看起來像EVO 4G是問題。我會繼續調查。

+0

我有同樣的問題,因爲一個測試應用程序崩潰,同時具有凸輪鎖定:(你找出如何解鎖,因爲問題是最後一個活動? – Fildor

+0

可惜沒有。我最終沒有建立一個自定義記錄畢竟是說,做完。 – BigFwoosh

回答

1

安排這樣的代碼,並減少視頻編碼rate.it是非常高的爲您的視頻size.may是因爲在一些設備已經在內部修剪它不是在你的設備創建的問題。

private void initRecorder(Surface surface) throws IOException { 
// It is very important to unlock the camera before doing setCamera 
// or it will results in a black preview 
if(camera == null) { 
    camera = Camera.open(); 
    camera.unlock(); 
} 

if(recorder == null) 
    recorder = new MediaRecorder(); 

recorder.setCamera(camera); 

camera.startPreview(); 

recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 
recorder.setVideoEncodingBitRate(2048000); 
recorder.setMaxDuration(10000); // length of video in MS 
recorder.setVideoSize(720, 480); 
recorder.setVideoFrameRate(30); 
recorder.setOutputFile("/sdcard/test.mp4"); 
recorder.setPreviewDisplay(surface); 
try { 
    recorder.prepare(); 
} catch (IllegalStateException e) { 
    // This is thrown if the previous calls are not called with the 
    // proper order 
    e.printStackTrace(); 
} 
}