android 在与Vimeo Networking java库集成时获取视频文件值null,通过URI播放视频

db2dz4w8  于 2023-01-24  发布在  Android
关注(0)|答案(2)|浏览(140)

我在我的安卓应用程序中使用Vimeo Networking Library,通过Vimeo的官方库,通过Video View播放视频。
我使用令牌对API进行身份验证
代码的问题在于它为videoFiles提供了null值。当我在代码注解之间提供下面提到的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
a)https://player.vimeo.com/videos/123456789
它从失败方法中抛出错误
I/TAG5:视频错误:使用JsonReader.setLenient(true)接受第1行第1列路径$处的格式错误的JSON
b)https://player.vimeo.com/videos/123456789/config
I/TAG1:视频:视频@0 I/标签2:视频文件为空
最后我用链接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();

        }
    });
}
}
j8ag8udp

j8ag8udp1#

我刚刚得到了解决方案,想把它贴在这里。这样可以帮助其他人
答案很简单,我得到了videoFile(TAG1)和link(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();

       }
    });
    }
}
2vuwiymt

2vuwiymt2#

我在库版本3. 12. 0上试过这个&它工作正常:-
请注意,这个访问令牌需要有所有三个访问被检查的时候,它的建设-“公共”,“私人”,“视频文件”

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

在此播放中,如果视频是由您正在使用的访问令牌的同一用户创建的,则数据将存在。
您可以通过比较您正在进入视频对象的用户(上面记录的)和您已经验证的用户(如下所示)来验证这一点:-

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}")
        }
    })

只有那些视频将播放数据的用户是相同的视频创建者/成员vimeo帐户作为您的访问令牌所有者。这是在官方文件中提到这里https://developer.vimeo.com/api/authentication(了解认证过程)

相关问题