3

我想播放我的 Vimeo 视频,我已经按照https://github.com/vimeo/vimeo-networking-java中的步骤进行操作。

我使用此方法获取 Video 对象,然后将其加载到 WebView 中。

VimeoClient.getInstance().fetchNetworkContent(String uri, ModelCallback callback) 

但是,当我记录结果时,似乎表明它失败了。

这是我的 2 个 Java 文件。

MyVimeoApplication.java

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import com.vimeo.networking.Configuration;
import com.vimeo.networking.Vimeo;
import com.vimeo.networking.VimeoClient;

public class MyVimeoApplication extends Application
{
private static final String SCOPE = "private public interact";

private static final boolean IS_DEBUG_BUILD = false;
// Switch to true to see how access token auth works.
private static final boolean ACCESS_TOKEN_PROVIDED = false;
private static Context mContext;

@Override
public void onCreate()
{
    super.onCreate();
    mContext = this;

    Configuration.Builder configBuilder;
    // This check is just as for the example. In practice, you'd use one technique or the other.
    if (ACCESS_TOKEN_PROVIDED)
    {
        configBuilder = getAccessTokenBuilder();
        Log.d("ACCESS_TOKEN", "PROVIDED");
    }
    else
    {
        configBuilder = getClientIdAndClientSecretBuilder();
        Log.d("ACCESS_TOKEN", "NOT PROVIDED");
    }
    if (IS_DEBUG_BUILD) {
        // Disable cert pinning if debugging (so we can intercept packets)
        configBuilder.enableCertPinning(false);
        configBuilder.setLogLevel(Vimeo.LogLevel.VERBOSE);
    }
    VimeoClient.initialize(configBuilder.build());
}

public Configuration.Builder getAccessTokenBuilder() {
    // The values file is left out of git, so you'll have to provide your own access token
    String accessToken = getString(R.string.access_token);
    return new Configuration.Builder(accessToken);
}

public Configuration.Builder getClientIdAndClientSecretBuilder() {
    // The values file is left out of git, so you'll have to provide your own id and secret
    String clientId = getString(R.string.client_id);
    String clientSecret = getString(R.string.client_secret);
    String codeGrantRedirectUri = getString(R.string.deeplink_redirect_scheme) + "://" +
            getString(R.string.deeplink_redirect_host);
    Configuration.Builder configBuilder =
            new Configuration.Builder(clientId, clientSecret, SCOPE, null,
                    null);
//        configBuilder.setCacheDirectory(this.getCacheDir())
//                .setUserAgentString(getUserAgentString(this)).setDebugLogger(new NetworkingLogger())
//                // Used for oauth flow
//                .setCodeGrantRedirectUri(codeGrantRedirectUri);

    return configBuilder;
}

public static Context getAppContext() {
    return mContext;
}

public static String getUserAgentString(Context context) {
    String packageName = context.getPackageName();

    String version = "unknown";
    try {
        PackageInfo pInfo = context.getPackageManager().getPackageInfo(packageName, 0);
        version = pInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        System.out.println("Unable to get packageInfo: " + e.getMessage());
    }

    String deviceManufacturer = Build.MANUFACTURER;
    String deviceModel = Build.MODEL;
    String deviceBrand = Build.BRAND;

    String versionString = Build.VERSION.RELEASE;
    String versionSDKString = String.valueOf(Build.VERSION.SDK_INT);

    return packageName + " (" + deviceManufacturer + ", " + deviceModel + ", " + deviceBrand +
            ", " + "Android " + versionString + "/" + versionSDKString + " Version " + version +
            ")";
}
}

MainActivity.java

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.widget.Toast;

import com.vimeo.networking.VimeoClient;
import com.vimeo.networking.callbacks.AuthCallback;
import com.vimeo.networking.callbacks.ModelCallback;
import com.vimeo.networking.model.Video;
import com.vimeo.networking.model.error.VimeoError;

public class MainActivity extends AppCompatActivity
{
private VimeoClient mApiClient = VimeoClient.getInstance();
private ProgressDialog mProgressDialog;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setMessage("All of your API are belong to us...");

    // ---- Client Credentials Auth ----
    if (mApiClient.getVimeoAccount().getAccessToken() == null) {
        // If there is no access token, fetch one on first app open
        authenticateWithClientCredentials();
    }
}

// You can't make any requests to the api without an access token. This will get you a basic
// "Client Credentials" gran which will allow you to make requests
private void authenticateWithClientCredentials() {
    mProgressDialog.show();
    mApiClient.authorizeWithClientCredentialsGrant(new AuthCallback() {
        @Override
        public void success()
        {
            Toast.makeText(MainActivity.this, "Client Credentials Authorization Success", Toast.LENGTH_SHORT).show();
            mProgressDialog.hide();

            mApiClient.fetchNetworkContent("https://vimeo.com/179708540", new ModelCallback<Video>(Video.class)
            {
                @Override
                public void success(Video video)
                {
                    // use the video
                    Log.d("VIDEO", "SUCCESS");
                    String html = video.embed != null ? video.embed.html : null;
                    if(html != null)
                    {
                        // html is in the form "<iframe .... ></iframe>"
                        // load the html, for instance, if using a WebView on Android, you can perform the following:
                        WebView webview = (WebView) findViewById(R.id.webView); // obtain a handle to your webview
                        webview.loadData(html, "text/html", "utf-8");
                    }
                }

                @Override
                public void failure(VimeoError error)
                {
                    Log.d("VIDEO", "FAIL");
                    Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
                    // voice the error
                }
            });

        }

        @Override
        public void failure(VimeoError error) {
            Toast.makeText(MainActivity.this, "Client Credentials Authorization Failure", Toast.LENGTH_SHORT).show();
            mProgressDialog.hide();
        }
    });
}
}
4

5 回答 5

6

您可以通过使用外部库 VimeoExtractor 来实现这一点。(我在 kotlin 中的示例) https://github.com/ed-george/AndroidVimeoExtractor

1. 第一步。

将 lib 添加到 gradle。

2. 第二步- 获取流 url

private fun initializePlayer() {
    progressBar.visible() //optional show user that video is loading.
    VimeoExtractor.getInstance().fetchVideoWithURL(VIMEO_VIDEO_URL, null, object : OnVimeoExtractionListener {
        override fun onSuccess(video: VimeoVideo) {
            val videoStream = video.streams["720p"] //get 720p or whatever
            videoStream?.let{
                playVideo(videoStream)
            }
        }

        override fun onFailure(throwable: Throwable) {
            Timber.e("Error durning video stream fetch")
        }
    })
}

3. 第三步- 准备好后玩

fun playVideo(videoStream: String) {
    runOnUiThread {
        progressBar.gone() //hide progressBar
        videoView.setBackgroundResource(R.drawable.splash_backgorund)
        videoView.setVideoPath(videoStream)
        videoView.requestFocus()
        videoView.setOnPreparedListener { mp ->
            mp.isLooping = true
            videoView.start()
        }
    }
}
于 2018-05-25T07:50:10.507 回答
4

Java 版本:我使用了“开膛手杰克”的答案,几乎没有什么变化:

第 1 步:使用以下链接提取视频路径

https://github.com/ed-george/AndroidVimeoExtractor

第二步:获取 Vimeo 视频路径

private void initializePlayer() {
    if(getIntent().getStringExtra("vimeo_vid_id")!=null){
        vid_id = getIntent().getStringExtra("vimeo_vid_id");
    }

    VimeoExtractor.getInstance().fetchVideoWithIdentifier(vid_id, null, new OnVimeoExtractionListener() {
        @Override
        public void onSuccess(VimeoVideo video) {
            String hdStream = video.getStreams().get("720p");
            System.out.println("VIMEO VIDEO STREAM" + hdStream);
            if (hdStream != null) {
                playVideo(hdStream);
            }
        }

        @Override
        public void onFailure(Throwable throwable) {

        }
    });
}

第 3 步:在 VideoView 中播放视频

private void playVideo(final String stream) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            final VideoView videoView = findViewById(R.id.vimeo_vv);
            final MediaController mediacontroller = new MediaController(VimeoVideoActivity.this);
            mediacontroller.setAnchorView(videoView);
            videoView.setMediaController(mediacontroller);

            videoView.setBackgroundColor(Color.TRANSPARENT);
            Uri video = Uri.parse(stream);
            videoView.setVideoURI(video);
            videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    videoView.requestFocus();
                    videoView.start();
                }
            });
            videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mp.release();
                    System.out.println("Video Finish");
                    finish();
                }
            });
        }
    });

}
于 2018-08-24T07:34:02.813 回答
3

问题是您正在尝试使用 url,而 vimeo-networking 库需要 uri。

您拥有的网址是https://vimeo.com/179708540- 这是在 Vimeo 网站上观看视频的网址。要使用 vimeo-networking 库,您必须为您正在使用的方法提供一个 uri(在这种情况下fetchNetworkContent);对于这个问题中的视频,uri 看起来像/videos/179708540。vimeo-networking 库将使用此 uri 构造一个用于连接 Vimeo API 的 url - 它最终看起来像https://api.vimeo.com/videos/179708540.

于 2016-08-22T13:13:57.827 回答
1

您不需要任何库来获取 vimeo 视频网址

您只需要遵循这两个步骤

  1. 使用 vimeo 视频 ID调用url 以获取 JSON 响应。

  2. 解析 JSON 响应

JSONArray streamArray = new JSONObject(<JSON Response String>)
                                      .getJSONObject("request")
                                      .getJSONObject("files")
                                      .getJSONArray("progressive");

    //Get info for each stream available
    for (int streamIndex = 0; streamIndex < streamArray.length(); streamIndex++) 
    {
        JSONObject stream = streamArray.getJSONObject(streamIndex);

        String url = stream.getString("url");
        String quality = stream.getString("quality");
    }
于 2019-09-25T06:23:55.010 回答
0

我通过更简单的身份验证解决了这个问题

您可以根据您选择的播放链接进行身份验证,您可以在下面的链接中使用我的方法

我使用了Vimeo 网络库 ,它是 vimeo 的官方库

这是我的答案的链接我可以配置我应该使用什么 URI 如何通过 Video View 或 Exo Player 播放视频

因此,您的解决方案是您可以将本机播放的代码替换为嵌入代码。嵌入的代码在这里 https://github.com/vimeo/vimeo-networking-java/tree/master#embed

现在将下面我解决方案中的 Native 播放代码替换为嵌入代码 Android Play Vimeo Video // 这就是解决方案

于 2020-11-23T06:59:43.410 回答