titansgroup / k4l-video-trimmer

A library with UI and mechanisms to trim local videos on Android applications.
MIT License
784 stars 221 forks source link

MediaPlayer API is unable to play custom content provider uri from external usb drive. #22

Open Maqsood007 opened 7 years ago

Maqsood007 commented 7 years ago

You have done a very good job to write this code but now there is a problem that i have to fetch videos from external usb and trim them. but Android Native MediaPlayer API is not playing them. so kindly help me.

Regards

ferPrieto commented 7 years ago

If you are trying to access to an external path, you must make the next change in TimeLineView.java

private void getBitmap(final int viewWidth) {
        BackgroundExecutor.execute(new BackgroundExecutor.Task("", 0L, "") {
                                       @Override
                                       public void execute() {
                                           try {
                                               LongSparseArray<Bitmap> thumbnailList = new LongSparseArray<>();

                                               MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
                                              //  mediaMetadataRetriever.setDataSource(getContext(), videoUri); //NOT VALID FOR STREAMING VIDEOS
                                               mediaMetadataRetriever.setDataSource(videoUri.toString(), new HashMap<String, String>());

                                               // Retrieve media data
                                               long videoLengthInMs = Integer.parseInt(mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)) * 1000;

                                               // Set thumbnail properties (Thumbs are squares)
                                               final int thumbWidth = heightView;
                                               final int thumbHeight = heightView;

                                               int numThumbs = (int) Math.ceil(((float) viewWidth) / thumbWidth);

                                               final long interval = videoLengthInMs / numThumbs;

                                               for (int i = 0; i < numThumbs; ++i) {
                                                   Bitmap bitmap = mediaMetadataRetriever.getFrameAtTime(i * interval, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
                                                   // TODO: bitmap might be null here, hence throwing NullPointerException. You were right
                                                   try {
                                                       bitmap = Bitmap.createScaledBitmap(bitmap, thumbWidth, thumbHeight, false);
                                                   } catch (Exception e) {
                                                       e.printStackTrace();
                                                   }
                                                   thumbnailList.put(i, bitmap);
                                               }

                                               mediaMetadataRetriever.release();
                                               returnBitmaps(thumbnailList);
                                           } catch (final Throwable e) {
                                               Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
                                           }
                                       }
                                   }
        );
    }