Getting video files value null while integrating with Vimeo Networking java Library, to play the video by URI

808 Views Asked by At

I used Vimeo Networking Library in my android app to play the video by Video View by useing the official library of Vimeo.

I authenticate the API with Token

The problem with the code is that it gives me the null value for the videoFiles. When I give the link in b format mentioned below between the code comment

Here is my code

public class PlayActivity extends AppCompatActivity {

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

        videoView = findViewById(R.id.player);
// Getting access Token

        String accessToken = getString(R.string.access_token);
        Configuration.Builder configBuilder = new Configuration.Builder(accessToken)
                .enableCertPinning(false);
//Vimeo Client autenticated

        VimeoClient.initialize(configBuilder.build());
// the video uri; if you have a video, this is video.uri

I don't know which URI I should Pass, So I pass the URI in 2 format

a) https://player.vimeo.com/videos/123456789

It throws me the error from the failure method

I/TAG5: Vimeo error : Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

b) https://player.vimeo.com/videos/123456789/config

I/TAG1: Video: com.vimeo.networking.model.Video@0 I/TAG2: VideoFiles null

So finally I use link b

    final String uri = "https://player.vimeo.com/videos/123456789/config"; 
    GsonDeserializer gsonDeserializer = new GsonDeserializer();
    VimeoClient.getInstance().fetchNetworkContent(uri, new ModelCallback<Video>(Video.class) {
        @Override
        public void success(Video video) {
            Toast.makeText(PlayActivity.this, "Sucessful" + video, Toast.LENGTH_SHORT).show();
            Log.i("TAG1", "Video: " + video);


            ArrayList<VideoFile> videoFiles = video.files;
            Log.i("TAG2", "VideoFiles " + videoFiles);
// I am getting null Value of **videoFiles** and it's not passing the if block with link b above mentioned 

            if (videoFiles != null && !videoFiles.isEmpty()) {
                VideoFile videoFile = videoFiles.get(0); // you could sort these files by size, fps, width/height
                Log.i("TAG3", "VideoFiles " + videoFiles);
                String link = videoFile.getLink();
                Log.i("TAG4", "link " + link);
                // load link
                MediaController mediaController = new MediaController(PlayActivity.this);
                mediaController.setAnchorView(videoView);

                videoView.setVisibility(View.VISIBLE);
                videoView.setVideoURI(Uri.parse(link));
                videoView.setMediaController(null);
                videoView.requestFocus();
                videoView.start();
            }
        }

        @Override
        public void failure(VimeoError error) {

            Log.i("TAG5", "vimeo error  : " + error.getErrorMessage());
            Toast.makeText(PlayActivity.this, "failure due to " + error.getErrorMessage(), Toast.LENGTH_SHORT).show();

        }
    });
}
}
2

There are 2 best solutions below

1
On

I just got the solution, thought of posting it here. So it can help other

The answer is straightforward, and I got the videoFile (TAG1) and link (TAG2) bypassing the link in this format

https://api.vimeo.com/me/videos/123456789

So the final code will be like this

final String uri = "https://api.vimeo.com/me/videos/123456789";

instead of this

final String uri = "https://player.vimeo.com/videos/123456789/config"; 

Here is my complete code that helps me to play the video in android app by using Vimeo networking library

Presenting the final code

public class PlayActivity extends AppCompatActivity {

VideoView videoView;

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

    videoView = findViewById(R.id.player);
    // Getting access Token

    String accessToken = getString(R.string.access_token);
    Configuration.Builder configBuilder = new Configuration.Builder(accessToken)
            .enableCertPinning(false);
    //Vimeo Client autenticated

    VimeoClient.initialize(configBuilder.build());
    // the video uri; if you have a video, this is video.uri

    final String uri = "https://api.vimeo.com/me/videos/123456789"; 

    VimeoClient.getInstance().fetchNetworkContent(uri, new ModelCallback<Video>(Video.class) {
    
    @Override
    public void success(Video video) {
        Toast.makeText(PlayActivity.this, "Sucessful" + video, Toast.LENGTH_SHORT).show();
       
        ArrayList<VideoFile> videoFiles = video.files;
        Log.i("TAG1", "videoFiles " + videoFiles);
        if (videoFiles != null && !videoFiles.isEmpty()) {
            VideoFile videoFile = videoFiles.get(0); // you could sort these files by size, fps, width/height
            
            String link = videoFile.getLink();
            Log.i("TAG2", "link " + link);
            // load link
            // use the link to play the video by **EXO Player** or **Video View**
           // Start your video player here
           }
         }

    @Override
    public void failure(VimeoError error) {

        Log.i("TAG3", "vimeo error  : " + error.getErrorMessage());
        Toast.makeText(PlayActivity.this, "failure due to " + error.getErrorMessage(), Toast.LENGTH_SHORT).show();

       }
    });
    }
}
0
On

I have tried this on library version 3.12.0 & it works:-

Pls note that this access token needs to have all the three access to be check at the time of building it - "public", "private", "video file"

val confBuilder = VimeoApiConfiguration.Builder(accessToken)
val configuration = confBuilder.build()
Authenticator.initialize(configuration)
VimeoApiClient.initialize(configuration,Authenticator.instance())
VimeoApiClient.instance()
   .fetchVideo(finalUrl, null, null, null, object : VimeoCallback<Video>{
 //here url should be like "https://api.vimeo.com/videos/{video_id}" otherwise it wasn't working whatever the url was
     override fun onError(error: VimeoResponse.Error) {
        Logger.d("TAG", "vimeo error = ${error.message}")
        Toast.makeText(applicationContext, error.message, Toast.LENGTH_SHORT).show()
        }
    override fun onSuccess(response: VimeoResponse.Success<Video>) {
      val video = response.data
      Log.d("TAG", "vimeo video user = ${response.data.user?.name}")
        val play: Play? = video.play
        val progressiveFiles: List<ProgressiveVideoFile>? = play.progressive
      val linkToMp4File: String? = progressiveFiles?.get(0)?.link
                                runExoplayer(linkToMp4File)
       }
      })
       

In this Play data will be there iff the video is created by the same user whose access token you are using.

You can verify this by comparing the user you are getting inside the video object (Logged above) with the user with which you have authenticated (shown below) :-

VimeoApiClient.instance().fetchCurrentUser(null, null, object : VimeoCallback<User>{
        override fun onError(error: VimeoResponse.Error) {

        }
        override fun onSuccess(response: VimeoResponse.Success<User>) {
            Logger.d("TAG", "vimeo user = ${response.data.name}")
        }
    })
    
    

Only those videos will have Play data whose user is same as video creator/member on vimeo account as your access token owner. This is mentioned in the official document here https://developer.vimeo.com/api/authentication (Understanding the authentication process)