2017-07-20 128 views
0

我一直在試圖用com.android.hardware.Camera包拍攝視頻。Android - 拍攝視頻時出錯-22

看起來MediaRecorder無法啓動。 我想通過Intent Service拍攝視頻(無需預覽屏幕上的輸出)。 這是我的意圖主要功能。

private  MediaRecorder mMediaRecorder; 
private  Camera   cam = null; 
public static boolean  isRecording = false; 
public static boolean  isRunning = false; 

public void onCreate() { 
    cam = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); 
    try { 
     cam.unlock(); 
     mMediaRecorder.setCamera(cam); 
     cam.lock(); 
    } catch (Exception e){ 
     e.fillInStackTrace(); 
    } 
    isRunning = true; 
    // Step 2: Set sources 
    mMediaRecorder = new MediaRecorder(); 
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); 
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); 
    // Step 4: Set output file 
    String fileName = "/sdcard/" + RandomNameGenerator.setSecureFilename(".video"); 
    mMediaRecorder.setOutputFile(fileName); 
    // Step 6: Prepare configured MediaRecorder 
    try { 
     mMediaRecorder.prepare(); 
    } catch (IllegalStateException e) { 
     Log.d("DEBUG", "IllegalStateException preparing MediaRecorder: " + e.getMessage()); 
    } catch (IOException e) { 
     Log.d("DEBUG", "IOException preparing MediaRecorder: " + e.getMessage()); 
    } 
    isRecording = true; 
    Log.d("SERVICE VIDEO", "IS RECORDING"); 
    try { 
     mMediaRecorder.start(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public void onDestroy(){ 
    isRecording = false; 
    if (mMediaRecorder != null) { 
     mMediaRecorder.reset(); // clear recorder configuration 
     mMediaRecorder.release(); // release the recorder object 
     mMediaRecorder = null; 
     isRunning = false; 
    } 
} 

而這裏的日誌,當我開始的意圖:

E/MediaRecorder: start failed: -22 
W/System.err: java.lang.RuntimeException: start failed. 
W/System.err:  at android.media.MediaRecorder.start(Native Method) 
W/System.err:  at com.example.nathan.def_calc.VideoRecorder.onCreate(VideoRecorder.java:61) 
W/System.err:  at android.app.ActivityThread.handleCreateService(ActivityThread.java:3192) 
W/System.err:  at android.app.ActivityThread.-wrap5(ActivityThread.java) 
W/System.err:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1568) 
W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:102) 
W/System.err:  at android.os.Looper.loop(Looper.java:154) 
W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:6121) 
W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 
W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 

所有必需的權限清單中的設定。 我是否需要在某處聲明某種緩衝區?我沒有在API中找到關於我們如何創建視頻緩衝區的任何文檔。 有沒有人有解決方案?

回答

0

好的,我解決了這個問題,顯而易見的問題是我沒有任何視頻緩衝區。我發現我需要一個Surface對應於Camera對象的緩衝區。添加在onCreate()

sft = new SurfaceTexture(0); 
sf = new Surface(sft); 
mMediaRecorder.setPreviewDisplay(sf); 

sf.release(); 
sft.release(); 

onDestroy()功能所作的伎倆。