我在启动时面临OnClick
问题按钮视频录制将在android中启动。当应用程序启动时,它将开始录制视频,但我希望视频录制将在启动按钮单击后启动。所以请帮助我。我使用了SurfaceView
并在清单中使用了权限。
public class CameraActivity extends Activity implements SurfaceHolder.Callback {
private MediaRecorder recorder;
private SurfaceHolder surfaceHolder;
private CamcorderProfile camcorderProfile;
private Camera camera;
boolean recording = false;
boolean usecamera = true;
boolean previewRunning = false;
SurfaceView surfaceView;
Button btnStart, btnStop;
File root;
File file;
Boolean isSDPresent;
SimpleDateFormat simpleDateFormat;
String timeStamp;
private final String VIDEO_PATH_NAME = "/sdcard/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
recording = false;
initComs();
actionListener();
}
private void initComs() {
simpleDateFormat = new SimpleDateFormat("ddMMyyyyhhmmss");
timeStamp = simpleDateFormat.format(new Date());
camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
btnStop = (Button) findViewById(R.id.buttonExit);
btnStart = (Button) findViewById(R.id.buttonAnswer);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
isSDPresent = android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
private void actionListener() {
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (recording) {
recorder.stop();
if (usecamera) {
try {
camera.reconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
recording = false;
prepareRecorder();
Toast.makeText(getApplicationContext(), "Clicked on Stopped", Toast.LENGTH_LONG).show();
finish();
}
}
});
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
recording = true;
recorder.start();
}
});
}
private void prepareRecorder() {
recorder = new MediaRecorder();
recorder.setPreviewDisplay(surfaceHolder.getSurface());
if (usecamera) {
camera.unlock();
recorder.setCamera(camera);
}
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setProfile(camcorderProfile);
if (camcorderProfile.fileFormat == MediaRecorder.OutputFormat.MPEG_4) {
recorder.setOutputFile(VIDEO_PATH_NAME + "XYZAppVideo" + ""
+ new SimpleDateFormat("ddMMyyyyHHmmss").format(new Date())
+ ".mp4");
} else if (camcorderProfile.fileFormat == MediaRecorder.OutputFormat.MPEG_4) {
recorder.setOutputFile(VIDEO_PATH_NAME + "XYZAppVideo" + ""
+ new SimpleDateFormat("ddMMyyyyHHmmss").format(new Date())
+ ".mp4");
} else {
recorder.setOutputFile(VIDEO_PATH_NAME + "XYZAppVideo" + ""
+ new SimpleDateFormat("ddMMyyyyHHmmss").format(new Date())
+ ".mp4");
}
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}
public void surfaceCreated(SurfaceHolder holder) {
System.out.println("onsurfacecreated");
if (usecamera) {
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
previewRunning = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
System.out.println("onsurface changed");
if (!recording && usecamera) {
if (previewRunning) {
camera.stopPreview();
}
try {
Camera.Parameters p = camera.getParameters();
p.setPreviewSize(camcorderProfile.videoFrameWidth,
camcorderProfile.videoFrameHeight);
p.setPreviewFrameRate(camcorderProfile.videoFrameRate);
camera.setParameters(p);
camera.setPreviewDisplay(holder);
camera.startPreview();
previewRunning = true;
} catch (IOException e) {
e.printStackTrace();
}
prepareRecorder();
if (!recording) {
recording = true;
recorder.start();
}
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
if (recording) {
recorder.stop();
recording = false;
}
recorder.release();
if (usecamera) {
previewRunning = false;
camera.release();
}
finish();
}
}
答案 0 :(得分:0)
此代码可以正常工作:
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();
}
}