Android get Video source path from VideoView

倖福魔咒の 提交于 2019-12-05 06:39:52

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.

DrewB

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!