首页 > 解决方案 > 如何在android中播放vimeo视频。我尝试过使用 webview。有没有其他方法可以播放 vimeo 视频?

问题描述

我曾尝试使用 webview 播放 vimeo 视频。但它看起来并不好,它使屏幕可滚动。Vimeo 提出了另一种使用本机播放的播放方式,但它有特定的使用要求

原生播放的基本要求是:

  1. 用户必须登录。
  2. 用户必须是视频的所有者。
  3. 用户必须是 PRO 或更高版本(或应用程序必须具有“可以访问所有者的视频文件”功能)。
  4. 令牌必须具有 video_files 范围。用户必须是发出请求的 API 应用的所有者

我没有专业帐户。我如何使用本机播放来测试,如果一切顺利,我会去 vimeo PRO

这是我使用 WEBVIEW 播放的代码

 VimeoClient.getInstance().fetchNetworkContent(uri, new ModelCallback<Video>(Video.class) {
            @Override
            public void success(Video video) {
                String html = video.embed != null ? video.embed.html : null;
                if(html != null) {
                    WebView webView = (WebView) findViewById(R.id.webView);
                    WebSettings webSettings = webView.getSettings();
                    webSettings.setJavaScriptEnabled(true);
                    webView.loadData(html, "text/html", "utf-8");
                }
            }

            @Override
            public void failure(VimeoError error) {

                Log.d(TAG, "failure: ");
            }
        });
    }

有没有其他方法可以播放视频?

标签: androidvimeovimeo-apivimeo-android

解决方案


就我而言,我使用了 Exoplayer。Exoplayer 更具可定制性。您只需要从配置链接中提取 url 链接。您可以使用改造来提取视频网址。

BASE_URL = "https://player.vimeo.com/video/"

您将需要使用如下 get 方法:

@GET("{id}/{config}")
Call<JsonObject>getVideoLink(@Path("id") String id, @Path("config") String config);

您将从视频链接中获取 id。示例:“https://vimeo.com/123456789/”这里的 id 是:123456789。

 JsonObject jsonObject = response.body();
            JsonObject req = jsonObject.getAsJsonObject("request");
            JsonObject file = req.getAsJsonObject("files");
            JsonArray arr = file.getAsJsonArray("progressive");
            String url = arr.get(0).getAsJsonObject().get("url").getAsString();
           

现在你可以使用这个url玩了。不要忘记先启动 Exoplayer。


推荐阅读