Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.88 KB | None | 0 0
  1. I/ExternalStorage﹕ Scanned /mnt/sdcard/Pictures/Enterprise/VID_20150327_153645.mp4:
  2. I/ExternalStorage﹕ -> uri=content://media/external/video/media/21992
  3. I/﹕ FILE_PATH /mnt/sdcard/Pictures/Enterprise/VID_20150327_153645.mp4
  4. E/MediaPlayer﹕ error (1, -2147483648)
  5. E/MediaPlayer﹕ Error (1,-2147483648)
  6.  
  7. private void showVideoOnUI() {
  8. Log.i("", "FILE_PATH " + FILE_PATH);
  9.  
  10. // THIS LINE SHOW THE VIDEO TOTALLY CAN BE PLAYED
  11. // mVvVideo.setVideoPath("/mnt/sdcard/Pictures/Enterprise/VID_20150327_143003.mp4");
  12.  
  13. // ------- ERROR HAPPEN IN THIS LINE ---------
  14. // FILE PATH = /mnt/sdcard/Pictures/Enterprise/VID_20150327_143555.mp4 also,
  15. // BUT THE VIDEO TOTALLY CAN NOT BE PLAYED
  16. mVvVideo.setVideoPath(FILE_PATH);
  17. // mVvVideo.setVideoURI(Uri.parse(FILE_PATH));
  18.  
  19. // set play video view dialog details photo
  20. MediaController mMc = new MediaController(getActivity());
  21. mMc.setAnchorView(mVvVideo);
  22. mMc.setMediaPlayer(mVvVideo);
  23.  
  24. mVvVideo.requestFocus();
  25. mVvVideo.setBackgroundColor(Color.WHITE);
  26. mVvVideo.setMediaController(mMc);
  27. mVvVideo.setZOrderOnTop(true);
  28. mVvVideo.setOnTouchListener(new View.OnTouchListener() {
  29.  
  30. @Override
  31. public boolean onTouch(View v, MotionEvent event) {
  32. if (mVvVideo.isPlaying()) {
  33. mVvVideo.pause();
  34.  
  35. // Show full-screen button again
  36. mVvVideo.setVisibility(View.VISIBLE);
  37. } else {
  38. mVvVideo.start();
  39. }
  40.  
  41. return false;
  42. }
  43. });
  44.  
  45. if (!mVvVideo.isPlaying())
  46. mVvVideo.start();
  47. }
  48.  
  49. /**
  50. * Create a File for saving an image or video
  51. */
  52. private File getOutputMediaFile(int type) {
  53. // To be safe, you should check that the SDCard is mounted
  54. // using Environment.getExternalStorageState() before doing this.
  55. File mediaStorageDir = new File(
  56. Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), define.Camera.ENTERPRISE);
  57.  
  58. // This location works best if you want the created images to be shared
  59. // between applications and persist after your app has been uninstalled.
  60.  
  61. // Create the storage directory if it does not exist
  62. if (!mediaStorageDir.exists()) {
  63. if (!mediaStorageDir.mkdirs()) {
  64. Log.i("", "failed to create directory");
  65. return null;
  66. }
  67. }
  68.  
  69. // Create a media file name
  70. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  71. File mediaFile = null;
  72. if (type == MediaType.PHOTO) {
  73. mediaFile = new File(
  74. mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + Extension.JPG);
  75. } else if (type == MediaType.VIDEO) {
  76. mediaFile = new File(
  77. mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + Extension.MP4);
  78. }
  79.  
  80. String FILE_PATH = mediaFile.getAbsolutePath();
  81. // todo Should refresh Sd card in here
  82. File mFile = new File(FILE_PATH);
  83. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  84. Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  85. File f = new File(mFile.getAbsolutePath(), mFile.getName());
  86. Uri contentUri = Uri.fromFile(f);
  87.  
  88. Log.i("", "contentUri - " + contentUri.toString());
  89.  
  90. mediaScanIntent.setData(contentUri);
  91. getActivity().sendBroadcast(mediaScanIntent);
  92. } else {
  93. getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/" + mFile.getParent())));
  94. }
  95.  
  96. // Tell the media scanner about the new file so that it is
  97. // immediately available to the user.
  98. MediaScannerConnection.scanFile(getActivity(),
  99. new String[]{FILE_PATH}, null,
  100. new MediaScannerConnection.OnScanCompletedListener() {
  101. public void onScanCompleted(String path, Uri uri) {
  102. // ExternalStorage﹕ Scanned /mnt/sdcard/Pictures/Enterprise/VID_20150327_151212.mp4:
  103. Log.i("ExternalStorage", "Scanned " + path + ":");
  104. // ExternalStorage﹕ -> uri=content://media/external/video/media/21973
  105. Log.i("ExternalStorage", "-> uri=" + uri);
  106. }
  107. });
  108. return mediaFile;
  109. }
  110.  
  111. <uses-feature android:name="android.hardware.camera" />
  112. <uses-feature android:name="android.hardware.camera.autofocus" />
  113.  
  114. <uses-permission android:name="android.permission.CAMERA" />
  115. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  116. <uses-permission android:name="android.permission.RECORD_AUDIO" />
  117. <uses-permission android:name="android.permission.WAKE_LOCK" />
  118. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  119.  
  120. <application
  121. android:allowBackup="true"
  122. android:icon="@mipmap/ic_launcher"
  123. android:label="@string/app_name"
  124. android:theme="@style/AppTheme">
  125.  
  126. <activity
  127. android:name=".Enterprise"
  128. android:label="@string/app_name">
  129. <intent-filter>
  130. <action android:name="android.intent.action.MAIN" />
  131. <category android:name="android.intent.category.LAUNCHER" />
  132. </intent-filter>
  133. </activity>
  134.  
  135. <activity
  136. android:name="ui.activity.CustomCamera"
  137. android:screenOrientation="portrait"
  138. android:exported="true">
  139.  
  140. <intent-filter>
  141. <action android:name="android.intent.action.MEDIA_MOUNTED" />
  142. <data android:scheme="file"/>
  143. </intent-filter>
  144.  
  145. </activity>
  146. <activity android:name="ui.activity.CustomGallery" />
  147.  
  148. </application>
  149.  
  150. /**
  151. * Record video mode
  152. */
  153. // Begin Record video
  154.  
  155. // Configure MediaRecorder
  156. if (IS_RECORDING_VIDEO) {
  157. Log.i("", "Stop recording video");
  158.  
  159. try {
  160. // stop recording and release camera
  161. // stop the recording
  162. CustomCamera.mMediaRecorder.stop();
  163.  
  164. // release the MediaRecorder object
  165. CustomCamera.releaseMediaRecorder();
  166. // take camera access back from MediaRecorder
  167. CustomCamera.mCamera.lock();
  168.  
  169. // inform the user that recording has stopped
  170. IS_RECORDING_VIDEO = false;
  171.  
  172. // Stop the preview before transfer to Review page
  173. CustomCamera.mCamera.stopPreview();
  174.  
  175. // Should release Camera for the next time can be used
  176. CustomCamera.releaseCamera();
  177.  
  178. // Transfer to Review page to see Recording video at there
  179. // Send the file path also
  180. ((FragmentActivity) getActivity())
  181. .getSupportFragmentManager().beginTransaction()
  182. .addToBackStack(null)
  183. .replace(
  184. R.id.fl_custom_camera,
  185. CameraReviewFragment.newInstance(
  186. getOutputMediaFile(MediaType.VIDEO).getAbsolutePath()))
  187. .commitAllowingStateLoss();
  188. } catch (Exception e) {
  189. e.printStackTrace();
  190. }
  191. } else {
  192. // initialize video camera
  193. if (prepareVideoRecorder(define.Camera.CAMERA_BACK)) {
  194. Log.i("", "Start recording video");
  195.  
  196. // Camera is available and unlocked, MediaRecorder is prepared,
  197. // now you can start recording
  198. CustomCamera.mMediaRecorder.start();
  199.  
  200. // inform the user that recording has started
  201. IS_RECORDING_VIDEO = true;
  202.  
  203. // Begin set Recording time in here
  204. //start the countDown
  205. mCdt.start();
  206. } else {
  207. // prepare didn't work, release the camera
  208. CustomCamera.releaseMediaRecorder();
  209. }
  210. }
  211.  
  212. private boolean prepareVideoRecorder(int mode){
  213. // Should release before use new Preview for Recording Video mode
  214. CustomCamera.releaseCamera();
  215.  
  216. // Initialize camera
  217. CustomCamera.mCamera = CustomCamera.getCameraInstance(mode);
  218.  
  219. // Set orientation display
  220. CustomCamera.setCameraDisplayOrientation(getActivity(), mode);
  221.  
  222. // Should release before use new Preview for Recording Video mode
  223. CustomCamera.releaseMediaRecorder();
  224.  
  225. CustomCamera.mMediaRecorder = new MediaRecorder();
  226.  
  227. // Step 1: Unlock and set camera to MediaRecorder
  228. CustomCamera.mCamera.unlock();
  229. CustomCamera.mMediaRecorder.setCamera(CustomCamera.mCamera);
  230.  
  231. // Step 2: Set sources
  232. CustomCamera.mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
  233. CustomCamera.mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
  234.  
  235. // todo Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
  236. CustomCamera.mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
  237.  
  238. // Step 4: Set output file
  239. CustomCamera.mMediaRecorder.setOutputFile(getOutputMediaFile(MediaType.VIDEO).toString());
  240.  
  241. // Step 5: Set the preview output
  242. CustomCamera.mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());
  243.  
  244. // Step 6: Prepare configured MediaRecorder
  245. try {
  246. CustomCamera.mMediaRecorder.prepare();
  247. } catch (IllegalStateException e) {
  248. e.printStackTrace();
  249. CustomCamera.releaseMediaRecorder();
  250. return false;
  251. } catch (IOException e) {
  252. e.printStackTrace();
  253. CustomCamera.releaseMediaRecorder();
  254. return false;
  255. }
  256. return true;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement