Android get Video source path from VideoView

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

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 ?

回答1:

VideoView doesn't have getters for video path/Uri. Your only change 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.



回答2:

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.



回答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.



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