我發現這個工作代碼將捕獲視頻並修改了一下。是的,代碼運行良好,但它只允許用戶只錄制視頻一次。當我點擊「開始錄製」時,當用戶點擊菜單中的「停止錄製」時,它將不再錄製視頻。在Android中只錄制視頻的視頻錄製只是一次
我想要發生的是當我點擊「停止錄製」時,它會再次準備相機,然後當我點擊「開始錄製」時它會錄製一個新的視頻。
如何解決這個問題?
這裏主要代碼的方式:
package com.tcs.video;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
public class VideoRecorder extends Activity{
private MediaRecorder recorder;
private Preview mPreview;
boolean flag=false;
boolean startedRecording=false;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
recorder = new MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mPreview = new Preview(VideoRecorder.this,recorder);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(mPreview);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
menu.clear();
menu.add(0, 0, 0, "Start Recording");
menu.add(1, 1, 0, "Stop Recording");
menu.setGroupVisible(0, false);
menu.setGroupVisible(1, false);
if(startedRecording==false){
menu.setGroupVisible(0, true);
menu.setGroupVisible(1, false);
}else if(startedRecording==true){
menu.setGroupVisible(1, true);
menu.setGroupVisible(0, false);
}
return true;
}
/*!
<p>
This method receives control when Item in menu option is selected. It contains implementations
to be performed on selection of menu item.
</p>*/
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case 0:
//start the recorder
recorder.start();
startedRecording=true;
break;
case 1:
//stop the recorder
recorder.stop();
recorder.release();
recorder = null;
startedRecording=false;
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
class Preview extends SurfaceView implements SurfaceHolder.Callback
{
//Create objects for MediaRecorder and SurfaceHolder.
SurfaceHolder mHolder;
MediaRecorder tempRecorder;
//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(Context context,MediaRecorder recorder) {
super(context);
tempRecorder=recorder;
mHolder=getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// TODO Auto-generated constructor stub
}
public Surface getSurface()
{
return mHolder.getSurface();
}
// Implement the methods of SurfaceHolder.Callback interface
// SurfaceCreated : This method gets called when surface is created.
// In this, initialize all parameters of MediaRecorder object.
//The output file will be stored in SD Card.
public void surfaceCreated(SurfaceHolder holder){
tempRecorder.setOutputFile("/sdcard/recordvideooutput.3gpp");
tempRecorder.setPreviewDisplay(mHolder.getSurface());
try{
tempRecorder.prepare();
} catch (Exception e) {
String message = e.getMessage();
tempRecorder.release();
tempRecorder = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
if(tempRecorder!=null)
{
tempRecorder.release();
tempRecorder = null;
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
}
}
}
請幫幫忙!
感謝
傑森
非常感謝...我會嘗試這一個在家裏。 :) – user1120260
你好Geobits,我試過你的建議,但仍然沒有工作。 :( – user1120260
你的更新代碼是什麼樣子的? – Geobits