4

I want to play my Vimeo videos and I've followed the steps in https://github.com/vimeo/vimeo-networking-java.

I used this method to get the Video object and then load it into a WebView.

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

However, when I logged the result, it seems to indicate that it fails.

Here are my 2 Java files.

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();
        }
    });
}
}
Trancol
  • 227
  • 1
  • 4
  • 15

5 Answers5

6

You can achive this by using external library VimeoExtractor. (my example in kotlin) https://github.com/ed-george/AndroidVimeoExtractor

1. Step one.

Add lib to gradle.

2. Step Two - get stream 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. Step three - play when ready

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()
        }
    }
}
Community
  • 1
  • 1
Jakub S.
  • 5,580
  • 2
  • 42
  • 37
  • it really helped me I used this approach using java not kotlin but there ".setVideoPath" didn't work for me. in place of that I used it as - Uri video = Uri.parse(stream); videoView.setVideoURI(video); – Chirag Jain Aug 24 '18 at 07:27
4

Java Version: I used "Jack the Ripper" answer with little changes:

Step 1: Used below link to extract video path

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

Step 2: get Vimeo video path

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) {

        }
    });
}

Step 3: Play video in 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();
                }
            });
        }
    });

}
Chirag Jain
  • 628
  • 11
  • 24
3

The problem is that you are trying to use a url and the vimeo-networking library is expecting a uri.

The url you have is https://vimeo.com/179708540 - this is a web url to see a video on the Vimeo site. To use the vimeo-networking library you must provide a uri to the method you are using (in this case fetchNetworkContent); for the video in this question the uri would look like /videos/179708540. The vimeo-networking library will then use this uri to construct a url for connection with the Vimeo API - it will end up looking like https://api.vimeo.com/videos/179708540.

Kevin Z
  • 333
  • 1
  • 10
  • I'm getting nullpointerexception when I replace the url with "/videos/179708540" – Trancol Aug 23 '16 at 02:32
  • I just replaced the example code found in the vimeo-networking library with your authenticateWithClientCredentials block above (commenting out your webview lines) - it runs without any exceptions. If you are experiencing an exception, it would be helpful if you stated exactly what is going wrong, or in this case, what is null. – Kevin Z Aug 23 '16 at 12:03
  • I'm not sure either, the error log doesn't point to any specific line(s). Just now, I did what you did as well by replacing authenticateWithClientCredentials block with the example in the vimeo-networking and there's no error, and I can see the webview loads the data. However, I can't play the video because it says the technology is out of date. – Trancol Aug 24 '16 at 03:48
1

You don't need any library to get vimeo video url

You need to follow just these two steps

  1. Call this url with vimeo video ID to get the JSON Response.

  2. Parse the JSON Response

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");
    }
41 72 6c
  • 1,600
  • 5
  • 19
  • 30
0

I solved the issue by authenticating with simpler authentication

You can authenticate as per your choice for playing the link you can use my method in the link below by

I used Vimeo networking library which is the official library of vimeo

Here is the link to my answer I able to configure what URI should I use How to play the video by Video View or Exo Player

So, your solution is you can replace the code of native playback to embed code. The code for embed is here https://github.com/vimeo/vimeo-networking-java/tree/master#embed

Now replace the Native play code in my solution below to the embed code Android Play Vimeo Video // this is the solution

Aditya Anand
  • 367
  • 3
  • 9