0

我在我的 android 应用程序中使用Vimeo 网络库通过使用 Vimeo 的官方库通过Video View播放视频。

我使用 Token 验证 API

代码的问题是它给了我videoFiles的值。当我在代码注释之间给出下面提到的b格式的链接时

这是我的代码

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

我不知道我应该传递哪个 URI,所以我以 2 格式传递 URI

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

它从失败方法中抛出了错误

I/TAG5:Vimeo 错误:使用 JsonReader.setLenient(true) 在第 1 行第 1 列路径 $ 接受格式错误的 JSON

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

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

所以最后我使用链接 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();

        }
    });
}
}
4

1 回答 1

0

我刚刚得到解决方案,想在这里发布。所以它可以帮助其他人

答案很简单,我得到了视频文件(TAG1)和链接(TAG2)绕过这种格式的链接

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

所以最终的代码会是这样的

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

而不是这个

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

这是我的完整代码,可帮助我使用 Vimeo 网络库在 android 应用程序中播放视频

呈现最终代码

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

       }
    });
    }
}
于 2020-11-23T05:41:11.010 回答