Android从VideoView获取视频源路径

发布于 2024-12-03 08:06:25 字数 567 浏览 0 评论 0原文

我有 VideoView 实例。我需要知道它的视频源路径。

是否可以?有人可以帮助我吗?

我来自 WebChromeClient 类的代码是:

    @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();
        }
    }
}

如何从 video 对象获取视频源路径?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

油饼 2024-12-10 08:06:25

VideoView 没有视频路径/Uri 的 getter。你唯一的机会就是使用反射。 Uri 存储在private Uri mUri 中。要访问它,您可以使用:

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

请记住,私有字段可能会在未来的 Android 版本中发生变化。

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.

冷血 2024-12-10 08:06:25

如果您不喜欢使用这样的私有方法,您可以覆盖 VideoView 中的 setVideoUriMethod

public class MyVideoView extends VideoView 
{
    Uri uri;

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

现在您可以根据需要访问 videoview 的 uri。希望有帮助。

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.

冰雪之触 2024-12-10 08:06:25

另一种选择是在视图标签上设置视频 Uri/路径并稍后检索。

当您播放/开始时

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

当您想检查正在播放的内容时

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

如果在适配器中使用,请记住清除标签。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文