2013-04-05 120 views
0

我想在android中捕捉視頻,但我無法捕捉它。雖然我的代碼正在創建3gp文件,但我無法播放它。我的編碼有問題嗎?如何在android中捕捉視頻?

MediaRecorder videoRecorder; 
sFilePath = sSDPath + "/" + System.currentTimeMillis() + ".3gp"; 
String sSDPath = Environment.getExternalStorageDirectory().getPath(); 
String sFilePath=""; 

if (videoRecorder == null) { 
    videoRecorder = new MediaRecorder(); 
} 

videoRecorder.setVideoSource(
MediaRecorder.VideoSource.CAMERA); 
videoRecorder.setOutputFormat(
MediaRecorder.OutputFormat.THREE_GPP); 
videoRecorder.setVideoSize(640, 480); 
videoRecorder.setVideoFrameRate(30); 
videoRecorder.setVideoEncoder(
MediaRecorder.VideoEncoder.H264); 
videoRecorder.setOutputFile(sFilePath); 
try { 
videoRecorder.prepare(); 
} catch (IllegalStateException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
videoRecorder.start(); 
+0

[錄製影片只需](http://developer.android.com/training/camera/videobasics.html) – 2013-04-05 10:58:08

+0

你玩它在Android媒體播放器? – 2013-04-05 11:00:54

+0

3gp應該可以工作。你用mp4試過了嗎? – 2013-04-05 11:03:51

回答

0

你可以找到名爲SpyDroid的開源項目,或者查詢sipdroid。它的好例子。

0

你使用自定義錄音機..?如果沒有的話下面寫兩行代碼捕獲視頻..

Intent record_videoIntent=new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE); 
    startActivityForResult(record_videoIntent, 1); 

然後在活動覆蓋onActivityResult和得到的結果一樣......

烏里VIDEOURI = data.getData();

-1

創建RecorderPreview類延伸SurfaceView器具SurfaceHolder.Callback接口。

class RecorderPreview extends SurfaceView implements SurfaceHolder.Callback 
{ 
    //Create objects for MediaRecorder and SurfaceHolder. 
    MediaRecorder recorder; 
    SurfaceHolder holder; 

    //Create constructor of Preview Class. In this, get an object of 
    //surfaceHolder class by calling getHolder() method. After that add 
    //callback to the surfaceHolder. The callback will inform when surface is 
    //created/changed/destroyed. Also set surface not to have its own buffers. 

    public Preview(Contect context,MediaRecorder temprecorder) { 
super(context); 
     recorder=temprecorder; 
holder=getHolder(); 
holder.addCallback(this); 
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 

    // Implement the methods of SurfaceHolder.Callback interface 

    // SurfaceCreated : This method gets called when surface is created. 
    // In this, initialize all parameters of MediaRecorder object as explained 
    // above. 

    public void surfaceCreated(SurfaceHolder holder){ 
    try{ 
     recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); 
     recorder.setOutputFile("/sdcard/recordvideooutput.3gpp"); 
     recorder.setPreviewDisplay(mHolder.getSurface()); 
    recorder.prepare(); 
    } catch (Exception e) { 
     String message = e.getMessage() 
    } 
    } 

要允許錄製視頻,請將權限添加到應用程序的清單文件中。

 <uses-permission android:name="android.permission.RECORD_VIDEO"/> 


public void surfaceDestroyed(SurfaceHolder holder) 
{ 
if(recorder!=null) 
{ 
recorder.release(); 
recorder = null; 
} 
} 

//surfaceChanged : This method is called after the surface is created. 
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) 
    { 
} 
} 

實現視頻錄製應用程序的onCreate方法。

//Create objects of MediaRecorder and Preview class 
private MediaRecorder recorder; 
private Preview preview; 
    // In this method, create an object of MediaRecorder class. Create an object of 
    // RecorderPreview class(Customized View). Add RecorderPreview class object 
    // as content of UI. 

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    recorder = new MediaRecorder(); 
    preview = new RecorderPreview(this,recorder); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    setContentView(preview); 
} 

//Create option menu which can be used for starting and stopping video recording. 
// Also implement the onOptionsItemSelected to handle the events for option menu. 

public boolean onCreateOptionsMenu(Menu menu) 
{ 
    menu.add(0, 0, 0, "StartRecording"); 
    menu.add(0, 1, 0, "StopRecording"); 
    return super.onCreateOptionsMenu(menu); 
} 

public boolean onOptionsItemSelected(MenuItem item) 
{ 
    switch (item.getItemId()) 
    { 
     case 0: 
      recorder.start(); 
      break; 
     case 1: 
     recorder.stop(); 
     break; 
    } 
    return super.onOptionsItemSelected(item); 
} 

你可以在這裏找到原代碼:

http://www.codeproject.com/Articles/107270/Recording-and-Playing-Video-on-Android

+0

不適用於Motorola Zoom 4.0 – 2013-08-06 10:34:32