Example usage for android.media MediaMetadataRetriever MediaMetadataRetriever

List of usage examples for android.media MediaMetadataRetriever MediaMetadataRetriever

Introduction

In this page you can find the example usage for android.media MediaMetadataRetriever MediaMetadataRetriever.

Prototype

public MediaMetadataRetriever() 

Source Link

Usage

From source file:com.univpm.s1055802.faceplusplustester.Detect.AcquireVideo.java

/**
 * Estrae un frame per ogni secondo del video e li salva sul dispositivo
 * @param uri uri della cartella dove salvare i frames
 *///ww w .ja v  a 2  s. co  m
private void captureFrames(Uri uri) {
    String filePath = FileUtils.getPath(getApplicationContext(), uri);
    File video = new File(filePath);
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(filePath);
        double duration = Double
                .parseDouble(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
        Log.v("duration", String.valueOf(duration));

        int timeSkip = 1000000;
        for (int i = 0; i < duration * 1000; i = i + timeSkip) {
            File img = null;
            try {
                img = new File(framesDir, "Frame_" + String.valueOf(i / timeSkip) + ".jpg");
                OutputStream fOut = null;
                fOut = new FileOutputStream(img);
                Bitmap imgBmp = retriever.getFrameAtTime(i, MediaMetadataRetriever.OPTION_NEXT_SYNC);
                imgBmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                fOut.flush();
                fOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    } catch (RuntimeException ex) {
        ex.printStackTrace();
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
        }
    }

}

From source file:com.appdevper.mediaplayer.ui.PlaybackControlsFragment.java

private Bitmap downloadBitmap(String mediaId) {
    String url = MusicProvider.getInstance().getMusic(mediaId)
            .getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);
    final MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
    metaRetriever.setDataSource(url, new HashMap<String, String>());
    try {//from   w  w w  . j  av a 2  s.  c  om
        final byte[] art = metaRetriever.getEmbeddedPicture();
        return BitmapFactory.decodeByteArray(art, 0, art.length);
    } catch (Exception e) {
        Log.d(TAG, "Couldn't create album art: " + e.getMessage());
        return BitmapFactory.decodeResource(getResources(), R.drawable.ic_default_art);
    }
}

From source file:com.nostra13.universalimageloader.core.decode.ContentImageDecoder.java

private static MediaMetadataRetriever getMediaMetadataRetriever(String filePath) {
    if (TextUtils.isEmpty(filePath))
        return null;
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(filePath);/*from   ww w  . java 2s .com*/
    return retriever;
}

From source file:com.android.camera.manager.ThumbnailViewManager.java

private String getMimeType(String filePath) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    String mime = "image/jpeg";
    if (filePath != null) {
        try {/*  w w w  .j  a  v  a  2s. co  m*/
            retriever.setDataSource(filePath);
            mime = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
        } catch (IllegalStateException e) {
            return mime;
        } catch (IllegalArgumentException e) {
            return mime;
        } catch (RuntimeException e) {
            return mime;
        }
    }
    Log.d(TAG, "[getMimeType] mime = " + mime);
    return mime;
}

From source file:com.nostra13.universalimageloader.core.decode.ContentImageDecoder.java

private static MediaMetadataRetriever getMediaMetadataRetriever(Context context, Uri uri) {
    if (context == null || uri == null)
        return null;
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(context, uri);
    return retriever;
}

From source file:com.android.camera.v2.uimanager.ThumbnailManager.java

private String getMimeType(String filePath) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    String mime = "image/jpeg";
    if (filePath != null) {
        try {/*w w w .j  a v a2  s .com*/
            retriever.setDataSource(filePath);
            mime = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
        } catch (IllegalStateException e) {
            return mime;
        } catch (IllegalArgumentException e) {
            return mime;
        } catch (RuntimeException e) {
            return mime;
        }
    }
    LogHelper.i(TAG, "[getMimeType] mime = " + mime);
    return mime;
}

From source file:com.psu.capstonew17.pdxaslapp.CreateCardActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    switch (requestCode) {
    //We be getting a video! yay!
    case GET_VIDEO:
        if (resultCode == RESULT_OK) {
            videoUri = intent.getData();

            if (videoUri != null) {
                //Verify length of video is greater than two seconds
                MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                retriever.setDataSource(this, videoUri);
                int endTime = Integer
                        .parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
                if (endTime < MIN_VIDEO_LENGTH || endTime > MAX_VIDEO_LENGTH) {
                    Toast.makeText(this, R.string.bad_video_length, Toast.LENGTH_SHORT).show();
                } else {
                    //if they had previously imported a video but changed their minds and imported
                    //a different video then we need to stop payback of the old one and hide some
                    //views while the new one imports.
                    stopVideo();/* ww  w .  j  a va2  s .co  m*/

                    //lets get the import options the user wants
                    VideoManager.ImportOptions imo = new VideoManager.ImportOptions();
                    imo.startTime = 0;
                    imo.endTime = endTime;
                    imo.quality = 20;
                    imo.cropRegion = null;

                    //block with a spin wheel while the video is imported
                    progressDialog.show();

                    //now we can import the new video.
                    video = null;
                    VideoManager vm = ExternalVideoManager.getInstance(this);
                    vm.importVideo(this, videoUri, imo, new VideoManager.VideoImportListener() {
                        //this is behaving weirdly with a horizontal progress bar, so for now I'm
                        //ignoring it in favor of a spin wheel
                        @Override
                        public void onProgressUpdate(int current, int max) {
                        }

                        //this is called when the import has completed
                        //we get the video, display it and the submit button,
                        //and then hide the progress dialog.
                        @Override
                        public void onComplete(Video vid) {
                            video = vid;
                            startVideo();
                            progressDialog.dismiss();
                            Toast.makeText(CreateCardActivity.this, R.string.video_delete_safe,
                                    Toast.LENGTH_LONG).show();
                        }

                        //if importing fails.
                        @Override
                        public void onFailed(Throwable err) {
                            Toast.makeText(CreateCardActivity.this, R.string.import_video_error,
                                    Toast.LENGTH_SHORT).show();
                            progressDialog.dismiss();
                        }
                    });
                }
            }
        }
        break;
    }
}

From source file:com.online.fullsail.SaveWebMedia.java

private Bitmap getVideoFrame(String uri) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {//from ww w.j  av  a  2s  . co  m
        if (Integer.parseInt(Build.VERSION.SDK) > Build.VERSION_CODES.GINGERBREAD) {
            retriever.setDataSource(uri);
            return retriever.getFrameAtTime();
        } else {
            retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
            retriever.setDataSource(uri);
            return retriever.captureFrame();
            // These items were handled by the custom android media class
            // Despite being marked errors, they compile without an issue
        }
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    } catch (RuntimeException ex) {
        ex.printStackTrace();
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
        }
    }
    return null;
}

From source file:life.knowledge4.videotrimmer.K4LVideoTrimmer.java

public void onSaveClicked() {
    if (mOnTrimVideoListener != null && !mOnTrimVideoListener.shouldTrim()) {
        onTrimFinished();//from ww  w. jav  a 2  s  .  c o m
        return;
    }

    if (mStartPosition <= 0 && mEndPosition >= mDuration) {
        if (mOnTrimVideoListener != null)
            mOnTrimVideoListener.getResult(mSrc);
    } else {
        mPlayView.setVisibility(View.VISIBLE);
        mVideoView.pause();

        MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(getContext(), mSrc);
        long METADATA_KEY_DURATION = Long.parseLong(
                mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));

        final File file = new File(mSrc.getPath());

        if (mTimeVideo < MIN_TIME_FRAME) {

            if ((METADATA_KEY_DURATION - mEndPosition) > (MIN_TIME_FRAME - mTimeVideo)) {
                mEndPosition += (MIN_TIME_FRAME - mTimeVideo);
            } else if (mStartPosition > (MIN_TIME_FRAME - mTimeVideo)) {
                mStartPosition -= (MIN_TIME_FRAME - mTimeVideo);
            }
        }

        Log.d(TAG, "onSaveClicked: mStartPosition: " + stringForTime(mStartPosition) + " #### mEndPosition: "
                + stringForTime(mEndPosition));

        final File root = new File(Environment.getExternalStorageDirectory() + File.separator
                + "k4lVideoTrimmer" + File.separator);
        root.mkdirs();
        final String fname = "t_" + mSrc.getPath().substring(mSrc.getPath().lastIndexOf("/") + 1);
        final File sdImageMainDirectory = new File(root, fname);
        final Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);

        final String outPutPath = getRealPathFromUri(outputFileUri);

        FFmpeg ffmpeg = FFmpeg.getInstance(getContext());
        String[] command = { "-y", "-i", file.getPath(), "-ss", stringForTime(mStartPosition), "-to",
                stringForTime(mEndPosition), "-c", "copy", outPutPath };//{"-y", "-ss", "00:00:03", "-i", file.getPath(), "-t", "00:00:08", "-async", "1", outPutPath};  //-i movie.mp4 -ss 00:00:03 -t 00:00:08 -async 1 cut.mp4
        try {
            ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
                @Override
                public void onSuccess(String message) {
                    super.onSuccess(message);
                    Log.d(TAG, "onSuccess: " + message);
                }

                @Override
                public void onProgress(String message) {
                    super.onProgress(message);
                    Log.d(TAG, "onProgress: " + message);
                }

                @Override
                public void onFailure(String message) {
                    super.onFailure(message);
                    loading.setVisibility(GONE);
                    Log.d(TAG, "onFailure: " + message);
                }

                @Override
                public void onStart() {
                    super.onStart();
                    Log.d(TAG, "onStart: ");
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    loading.setVisibility(GONE);
                    mOnTrimVideoListener.getResult(outputFileUri);
                    Log.d(TAG, "onFinish: ");
                }
            });
        } catch (FFmpegCommandAlreadyRunningException e) {

        }

        //notify that video trimming started
        if (mOnTrimVideoListener != null)
            mOnTrimVideoListener.onTrimStarted();
        //<<<<<<< HEAD
        //=======
        //
        //            BackgroundExecutor.execute(
        //                    new BackgroundExecutor.Task("", 0L, "") {
        //                        @Override
        //                        public void execute() {
        //                            try {
        //                                TrimVideoUtils.startTrim(file, new File(getDestinationPath(), UUID.randomUUID().toString()), mStartPosition, mEndPosition, mOnTrimVideoListener);
        //                            } catch (final Throwable e) {
        //                                Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
        //                            }
        //                        }
        //                    }
        //            );
        //>>>>>>> e9b3dcbb38ccfa62307fc2fb020bcb4d35933afa
    }
}

From source file:at.ac.tuwien.detlef.fragments.PlayerFragment.java

private void setNotPlayingSeekBarAndTime(Episode ep) {
    if (service == null) {
        return;//from   w  w w.  j a  v  a 2  s. c o  m
    }
    if (trackingTouch) {
        return;
    }
    try {
        if (service.episodeFileOK(ep)) {
            MediaMetadataRetriever metaData = new MediaMetadataRetriever();
            metaData.setDataSource(ep.getFilePath());
            String durationString = metaData.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            int duration = Integer.parseInt(durationString);
            int playingPosition = ep.getPlayPosition();
            int pos = 0;
            if (((playingPosition) > 0) && (playingPosition < duration)) {
                pos = playingPosition;
            }
            String minutesSecondsRemaining = getRemainingTime(duration, pos);
            String minutesSecondsAlreadyPlayed = getAlreadyPlayed(pos);
            remainingTime.setText("-" + minutesSecondsRemaining);
            alreadyPlayed.setText(minutesSecondsAlreadyPlayed);
            seekBar.setMax(duration);
            seekBar.setProgress(pos);
            seekBar.setSecondaryProgress(0);
        } else if ((service.getNextEpisode() == ep) && service.hasRunningEpisode()) {
            seekBar.setMax(service.getDuration());
            seekBar.setProgress(service.getCurrentPosition());
            alreadyPlayed.setText(getAlreadyPlayed(service.getCurrentPosition()));
            remainingTime.setText("-" + getRemainingTime(service.getDuration(), service.getCurrentPosition()));
        }
    } catch (Exception ex) {
        Log.d(getClass().getName(), "Error while retrieving duration of episode: " + ex.getMessage());
    }
}