Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I/ExternalStorage﹕ Scanned /mnt/sdcard/Pictures/Enterprise/VID_20150327_153645.mp4:
- I/ExternalStorage﹕ -> uri=content://media/external/video/media/21992
- I/﹕ FILE_PATH /mnt/sdcard/Pictures/Enterprise/VID_20150327_153645.mp4
- E/MediaPlayer﹕ error (1, -2147483648)
- E/MediaPlayer﹕ Error (1,-2147483648)
- private void showVideoOnUI() {
- Log.i("", "FILE_PATH " + FILE_PATH);
- // THIS LINE SHOW THE VIDEO TOTALLY CAN BE PLAYED
- // mVvVideo.setVideoPath("/mnt/sdcard/Pictures/Enterprise/VID_20150327_143003.mp4");
- // ------- ERROR HAPPEN IN THIS LINE ---------
- // FILE PATH = /mnt/sdcard/Pictures/Enterprise/VID_20150327_143555.mp4 also,
- // BUT THE VIDEO TOTALLY CAN NOT BE PLAYED
- mVvVideo.setVideoPath(FILE_PATH);
- // mVvVideo.setVideoURI(Uri.parse(FILE_PATH));
- // set play video view dialog details photo
- MediaController mMc = new MediaController(getActivity());
- mMc.setAnchorView(mVvVideo);
- mMc.setMediaPlayer(mVvVideo);
- mVvVideo.requestFocus();
- mVvVideo.setBackgroundColor(Color.WHITE);
- mVvVideo.setMediaController(mMc);
- mVvVideo.setZOrderOnTop(true);
- mVvVideo.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (mVvVideo.isPlaying()) {
- mVvVideo.pause();
- // Show full-screen button again
- mVvVideo.setVisibility(View.VISIBLE);
- } else {
- mVvVideo.start();
- }
- return false;
- }
- });
- if (!mVvVideo.isPlaying())
- mVvVideo.start();
- }
- /**
- * Create a File for saving an image or video
- */
- private File getOutputMediaFile(int type) {
- // To be safe, you should check that the SDCard is mounted
- // using Environment.getExternalStorageState() before doing this.
- File mediaStorageDir = new File(
- Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), define.Camera.ENTERPRISE);
- // This location works best if you want the created images to be shared
- // between applications and persist after your app has been uninstalled.
- // Create the storage directory if it does not exist
- if (!mediaStorageDir.exists()) {
- if (!mediaStorageDir.mkdirs()) {
- Log.i("", "failed to create directory");
- return null;
- }
- }
- // Create a media file name
- String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
- File mediaFile = null;
- if (type == MediaType.PHOTO) {
- mediaFile = new File(
- mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + Extension.JPG);
- } else if (type == MediaType.VIDEO) {
- mediaFile = new File(
- mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + Extension.MP4);
- }
- String FILE_PATH = mediaFile.getAbsolutePath();
- // todo Should refresh Sd card in here
- File mFile = new File(FILE_PATH);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
- File f = new File(mFile.getAbsolutePath(), mFile.getName());
- Uri contentUri = Uri.fromFile(f);
- Log.i("", "contentUri - " + contentUri.toString());
- mediaScanIntent.setData(contentUri);
- getActivity().sendBroadcast(mediaScanIntent);
- } else {
- getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/" + mFile.getParent())));
- }
- // Tell the media scanner about the new file so that it is
- // immediately available to the user.
- MediaScannerConnection.scanFile(getActivity(),
- new String[]{FILE_PATH}, null,
- new MediaScannerConnection.OnScanCompletedListener() {
- public void onScanCompleted(String path, Uri uri) {
- // ExternalStorage﹕ Scanned /mnt/sdcard/Pictures/Enterprise/VID_20150327_151212.mp4:
- Log.i("ExternalStorage", "Scanned " + path + ":");
- // ExternalStorage﹕ -> uri=content://media/external/video/media/21973
- Log.i("ExternalStorage", "-> uri=" + uri);
- }
- });
- return mediaFile;
- }
- <uses-feature android:name="android.hardware.camera" />
- <uses-feature android:name="android.hardware.camera.autofocus" />
- <uses-permission android:name="android.permission.CAMERA" />
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.RECORD_AUDIO" />
- <uses-permission android:name="android.permission.WAKE_LOCK" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme">
- <activity
- android:name=".Enterprise"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name="ui.activity.CustomCamera"
- android:screenOrientation="portrait"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.MEDIA_MOUNTED" />
- <data android:scheme="file"/>
- </intent-filter>
- </activity>
- <activity android:name="ui.activity.CustomGallery" />
- </application>
- /**
- * Record video mode
- */
- // Begin Record video
- // Configure MediaRecorder
- if (IS_RECORDING_VIDEO) {
- Log.i("", "Stop recording video");
- try {
- // stop recording and release camera
- // stop the recording
- CustomCamera.mMediaRecorder.stop();
- // release the MediaRecorder object
- CustomCamera.releaseMediaRecorder();
- // take camera access back from MediaRecorder
- CustomCamera.mCamera.lock();
- // inform the user that recording has stopped
- IS_RECORDING_VIDEO = false;
- // Stop the preview before transfer to Review page
- CustomCamera.mCamera.stopPreview();
- // Should release Camera for the next time can be used
- CustomCamera.releaseCamera();
- // Transfer to Review page to see Recording video at there
- // Send the file path also
- ((FragmentActivity) getActivity())
- .getSupportFragmentManager().beginTransaction()
- .addToBackStack(null)
- .replace(
- R.id.fl_custom_camera,
- CameraReviewFragment.newInstance(
- getOutputMediaFile(MediaType.VIDEO).getAbsolutePath()))
- .commitAllowingStateLoss();
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- // initialize video camera
- if (prepareVideoRecorder(define.Camera.CAMERA_BACK)) {
- Log.i("", "Start recording video");
- // Camera is available and unlocked, MediaRecorder is prepared,
- // now you can start recording
- CustomCamera.mMediaRecorder.start();
- // inform the user that recording has started
- IS_RECORDING_VIDEO = true;
- // Begin set Recording time in here
- //start the countDown
- mCdt.start();
- } else {
- // prepare didn't work, release the camera
- CustomCamera.releaseMediaRecorder();
- }
- }
- private boolean prepareVideoRecorder(int mode){
- // Should release before use new Preview for Recording Video mode
- CustomCamera.releaseCamera();
- // Initialize camera
- CustomCamera.mCamera = CustomCamera.getCameraInstance(mode);
- // Set orientation display
- CustomCamera.setCameraDisplayOrientation(getActivity(), mode);
- // Should release before use new Preview for Recording Video mode
- CustomCamera.releaseMediaRecorder();
- CustomCamera.mMediaRecorder = new MediaRecorder();
- // Step 1: Unlock and set camera to MediaRecorder
- CustomCamera.mCamera.unlock();
- CustomCamera.mMediaRecorder.setCamera(CustomCamera.mCamera);
- // Step 2: Set sources
- CustomCamera.mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
- CustomCamera.mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
- // todo Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
- CustomCamera.mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
- // Step 4: Set output file
- CustomCamera.mMediaRecorder.setOutputFile(getOutputMediaFile(MediaType.VIDEO).toString());
- // Step 5: Set the preview output
- CustomCamera.mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());
- // Step 6: Prepare configured MediaRecorder
- try {
- CustomCamera.mMediaRecorder.prepare();
- } catch (IllegalStateException e) {
- e.printStackTrace();
- CustomCamera.releaseMediaRecorder();
- return false;
- } catch (IOException e) {
- e.printStackTrace();
- CustomCamera.releaseMediaRecorder();
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement