0

I want to create an app that is playing a live video . Have to capture the current frame while clicking the button. Thanks,

Priya
  • 91
  • 2
  • 8

2 Answers2

0

You can get frame using MediaMetadataRetriever

private fun getVideoFrame() {
    val retriever = MediaMetadataRetriever()
    retriever.setDataSource(VIDEO_PATH)
    val bitmap = retriever.getFrameAtTime(yourTime, MediaMetadataRetriever.OPTION_CLOSEST)
}
Akshay Raiyani
  • 1,086
  • 7
  • 19
  • Thanks for your time. When I click the button the app will be crashed and the error is the following... java.lang.IllegalArgumentException at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:76) at qc.yotm.mes.com.yo_qc.MainActivity$3.onClick(MainActivity.java:306) at android.view.View.performClick(View.java:6608) at android.view.View.performClickInternal(View.java:6585) at android.view.View.access$3100(View.java:785) – Priya Oct 09 '19 at 06:57
  • It means your the path is invalid. make sure you have the VIDEO_PATH present correctly – King of Masses Oct 09 '19 at 07:02
  • The video is playing – Priya Oct 09 '19 at 07:03
  • I assume your streaming the video from url / uri not from the local path. So in your case use this setDataSource (Context context, Uri uri) – King of Masses Oct 09 '19 at 07:03
  • Yes, it is an RTSP link. – Priya Oct 09 '19 at 07:05
  • I have tried that too... It shows the following error. E/AndroidRuntime: FATAL EXCEPTION: main Process: qc.yotm.mes.com.yo_qc, PID: 2128 java.lang.IllegalArgumentException at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:172) at qc.yotm.mes.com.yo_qc.MainActivity$3.onClick(MainActivity.java:307) at android.view.View.performClick(View.java:6608) at android.view.View.performClickInternal(View.java:6585) at android.view.View.access$3100(View.java:785) – Priya Oct 09 '19 at 07:09
  • It seams your url might be expecting some additional params as well (like headers) etc. So you need to check for that method in setDataSource in MediaMetadataRetriever documentation – King of Masses Oct 09 '19 at 07:11
  • My Code is MediaMetadataRetriever retriever = new MediaMetadataRetriever(); Uri uri = Uri.parse(liveVideoPath); retriever.setDataSource(getApplicationContext(),uri); Bitmap bitmap = retriever.getFrameAtTime(60000000, MediaMetadataRetriever.OPTION_CLOSEST); imageViewNew.setVisibility(View.VISIBLE); imageViewNew.setImageBitmap(bitmap); – Priya Oct 09 '19 at 07:11
  • check my old answer here https://stackoverflow.com/a/46483591/3983054 – King of Masses Oct 09 '19 at 07:13
  • did your liveVideoPath url is playing directly in your media player ? else your adding any extra parameters to play it ? – King of Masses Oct 09 '19 at 07:14
  • mediaPlayer.setDataSource(liveVideoPath); – Priya Oct 09 '19 at 07:16
  • still the same issue ? I have updated my answer with few more points – King of Masses Oct 09 '19 at 07:29
  • Yes, still the problem occurs. – Priya Oct 09 '19 at 07:33
0

In addition to @Akshay Raiyani Answer,

Maybe you are running into this bug. If so try:

  public static Bitmap retriveVideoFrameFromVideo(String videoPath)
                throws Throwable
        {
            Bitmap bitmap = null;
            MediaMetadataRetriever mediaMetadataRetriever = null;
            try
            {
                mediaMetadataRetriever = new MediaMetadataRetriever();
                if (Build.VERSION.SDK_INT >= 14)
                    mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
                    else
                        mediaMetadataRetriever.setDataSource(videoPath);
                bitmap = mediaMetadataRetriever.getFrameAtTime();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                throw new Throwable(
                        "Exception in retriveVideoFrameFromVideo(String videoPath)"
                                + e.getMessage());

            }
            finally
            {
                if (mediaMetadataRetriever != null)
                {
                    mediaMetadataRetriever.release();
                }
            }
            return bitmap;
        }
King of Masses
  • 17,833
  • 4
  • 58
  • 76
  • Thanks for your valuable time sharing with me... I have tried that it shows the following System.err: java.lang.RuntimeException: setDataSource failed: status = 0xFFFFFFEA Thanks W/System.err: at android.media.MediaMetadataRetriever._setDataSource(Native Method) 2019-10-09 12:58:40.860 10627-10627/qc.yotm.mes.com.yo_qc W/System.err: at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:102) W/System.err: at MainActivity$3.onClick(MainActivity.java:310) W/System.err: at android.view.View.performClick(View.java:6608) – Priya Oct 09 '19 at 07:30
  • 1
    I have added the run time permission – Priya Oct 09 '19 at 07:36
  • Yes, link is rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov – Priya Oct 09 '19 at 07:40
  • Check this https://issuetracker.google.com/issues/36952379#comment2 bug tracker and see the solutions – King of Masses Oct 09 '19 at 07:43
  • If you are getting the IllegalArgumentException means path is invalid – King of Masses Oct 09 '19 at 07:44
  • In addition, can you check with this sample url (http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4) and confirm the issue if still facing – King of Masses Oct 09 '19 at 07:47
  • Okay, but how the live video is playing – Priya Oct 09 '19 at 07:49
  • Yeah, May be we are missing something, that we need to check where exactly we did the mistake ! – King of Masses Oct 09 '19 at 07:50
  • Have you check with the url which I provided ? – King of Masses Oct 09 '19 at 07:55
  • After put this link my app is not opening . And it shows the app is not responding – Priya Oct 09 '19 at 07:56