开发者

Android get Video source path from VideoView

开发者 https://www.devze.com 2023-04-02 15:01 出处:网络
I have VideoView instance. I need to know vid开发者_运维技巧eo source path from it. Is it possible? Can anybody help me?

I have VideoView instance. I need to know vid开发者_运维技巧eo 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 ?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消