0

我想在 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;
            }
        }
    }
4

0 回答 0