可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.