2011-07-08 117 views
3

我一直試圖錄制視頻失敗。我的代碼編譯,但當我嘗試運行時崩潰。有誰知道我可以在不改變屏幕上的視圖的情況下進行錄製?謝謝。在Android上錄製視頻而不在屏幕上顯示視頻

recorder = new MediaRecorder(); 
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); 
    recorder.setMaxDuration(15000); // 15 seconds 
    recorder.setMaxFileSize(5000000); // Approximately 5 megabytes 
    recorder.setOutputFile("/sdcard/xxxxx.mp4"); 
    recorder.start(); 
+1

可能會幫助,如果你提供的異常消息和堆棧跟蹤。 –

回答

2

你需要指定一個有效的表面:recorder.setPreviewDisplay().然後就可以調用recorder.prepare(),然後才recorder.start()

需要一個有效的表面退出惱人的,有一個關於這裏取出螺紋:

http://code.google.com/p/android/issues/detail?id=9570

這將例如允許後臺程序來錄製視頻。

0

源代碼&當屏幕關閉時,在Android手機上捕捉視頻記錄的步驟。請參閱文章Capturing a video record on Android mobile phone when screen is off

下面是一些示例記錄碼:

public class VideoCapture extends Activity implements OnClickListener, SurfaceHolder.Callback { 
    MediaRecorder recorder; 
    SurfaceHolder holder; 
    boolean recording = false; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

     recorder = new MediaRecorder(); 
     initRecorder(); 
     setContentView(R.layout.main); 

     SurfaceView cameraView = (SurfaceView) findViewById(R.id.CameraView); 
     holder = cameraView.getHolder(); 
     holder.addCallback(this); 
     holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

     cameraView.setClickable(true); 
     cameraView.setOnClickListener(this); 
    } 

    private void initRecorder() { 
     recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); 
     recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 

     CamcorderProfile cpHigh = CamcorderProfile 
       .get(CamcorderProfile.QUALITY_HIGH); 
     recorder.setProfile(cpHigh); 
     recorder.setOutputFile("/sdcard/videocapture_example.mp4"); 
     recorder.setMaxDuration(50000); // 50 seconds 
     recorder.setMaxFileSize(5000000); // Approximately 5 megabytes 
    } 

    private void prepareRecorder() { 
     recorder.setPreviewDisplay(holder.getSurface()); 

     try { 
      recorder.prepare(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
      finish(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      finish(); 
     } 
    } 

    public void onClick(View v) { 
     if (recording) { 
      recorder.stop(); 
      recording = false; 

      // Let's initRecorder so we can record again 
      initRecorder(); 
      prepareRecorder(); 
     } else { 
      recording = true; 
      recorder.start(); 
     } 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     prepareRecorder(); 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     if (recording) { 
      recorder.stop(); 
      recording = false; 
     } 
     recorder.release(); 
     finish(); 
    } 
} 


Also, do not forget to include these permissions in manifest: 
<uses-permission android:name="android.permission.RECORD_AUDIO" /> 
<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />