我想在Android中播放视频文件(使用VideoView和MediaController),同时让相机捕捉图像。无需相机显示或预览。但是,如果我调用“openCamera”(实例化一个新的VideoCapture),应用程序将失败并通知:视频无法显示。如果我删除调用以实例化摄像机,则视频播放顺畅。
public class Myvideoplayer extends Activity {
public FrameLayout imageFrame;
public RelativeLayout imageFrameLayout;
private ProgressDialog progressDialog;@
Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
imageFrame = new FrameLayout(this);
imageFrameLayout = new RelativeLayout(this);
imageFrameLayout.setGravity(Gravity.CENTER);
VideoView videoPlayer = new VideoView(this);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoPlayer);
videoPlayer.setMediaController(mediaController);
RelativeLayout.LayoutParams rel_btnBack =
new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
File root = Environment.getExternalStorageDirectory();
String myURI = root + "/DCIM/Camera/20121122_170657.mp4";
Uri video = Uri.parse(myURI);
videoPlayer.setVideoURI(video);
videoPlayer.requestFocus();
videoPlayer.start();
imageFrameLayout.addView(videoPlayer);
imageFrame.addView(imageFrameLayout);
setContentView(imageFrame);
//set up camera
progressDialog = ProgressDialog.show(this,
"Please wait....", "Openning camera");
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2 * 1000);
progressDialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Calculating().execute(1000, 1000, 1000);
}
public void InfoMessageBox(String str, String str2) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setTitle(str);
dlgAlert.setMessage(str2);
dlgAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {}
});
dlgAlert.create().show();
}
private class Calculating extends AsyncTask < Integer, Integer, Integer > {
protected Integer doInBackground(Integer...intInput) {
return 0;
}
protected void onProgressUpdate(Integer...progress) {}
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
cameraWrapper mCameraController = new cameraWrapper();
if (!mCameraController.openCamera()) {
InfoMessageBox("", "OOps, camera not ready!");
return;
}
}
}
}
这是相机开场
public boolean openCamera() {
synchronized (this) {
releaseCamera();
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID+1);
if (!mCamera.isOpened()) {
mCamera.release();
mCamera = null;
return false;
}
}
return true;
}
public void releaseCamera() {
synchronized (this) {
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
}