7

I have VideoView instance. I need to know video source path from it.

Is it possible? Can anybody help me?

My code from WebChromeClient class is:

    @Override
public void onShowCustomView(final View view, final CustomViewCallback callback) {
    super.onShowCustomView(view, callback);

    if (view instanceof FrameLayout) {
        final FrameLayout frame = (FrameLayout) view;
        if (frame.getFocusedChild() instanceof VideoView) {
            // get video view

            video = (VideoView) frame.getFocusedChild();
        }
    }
}

How to get video source path fron video object ?

ihrupin
  • 6,932
  • 2
  • 31
  • 47

3 Answers3

22

VideoView doesn't have getters for video path/Uri. Your only chance is to use reflection. The Uri is stored in private Uri mUri. To access it you can use:

Uri mUri = null;
try {
    Field mUriField = VideoView.class.getDeclaredField("mUri");
    mUriField.setAccessible(true);
    mUri = (Uri)mUriField.get(video);
} catch(Exception e) {}

Just bear in mind that a private field might be subject to change in future Android releases.

Tomik
  • 23,857
  • 8
  • 121
  • 100
  • 5
    Tomik, thanks a lot!!! It is really interesting solution. it's a pity that's the VideoView don't have public getter for this field – ihrupin Sep 07 '11 at 08:22
9

You can override the setVideoUriMethod in the VideoView if you do not like using private methods like this:

public class MyVideoView extends VideoView 
{
    Uri uri;

    @Override
    public void setVideoURI (Uri uri)
    {
        super.setVideoURI(uri);
        this.uri = uri;
    }
}

Now you can access the uri of the videoview as needed. Hope that helps.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
DrewB
  • 91
  • 1
  • 1
  • In my opinion, this is the better answer. The answer by Tomik using Reflection works, but it might break in the future (changes to the Android SDK across different API levels). I've been bitten by this in the past. – dell116 Sep 15 '14 at 16:47
3

Another alternative would be to set the video Uri/path on the tag of the view and retrieve later.

When you play/start

videoView.setVideoPath(localPath);
videoView.setTag(localPath);

When you want to check what's playing

String pathOfCurrentVideoPlaying = (String)videoView.getTag();

Just remember to clear out the tag if using in a adapter.

scottyab
  • 23,621
  • 16
  • 94
  • 105