编程语言
首页 > 编程语言> > android – 以编程方式播放YouTubePlayer无法正常工作

android – 以编程方式播放YouTubePlayer无法正常工作

作者:互联网

我正在使用YouTube API在我的应用中播放视频.

我想在用户输入片段时正确播放视频,因此我使用命令player.play();为了自动播放.但是这个命令似乎不起作用……

这是我的代码:

public class YouTubeVideoFragment extends YouTubePlayerFragment{

YouTubePlayer player;

public YouTubeVideoFragment()
{
}


@Override
public void onDestroy()
{
    // TODO Auto-generated method stub
    super.onDestroy();
    if(player!= null)
    {
        player.release();
    }

}

@Override
public void onStop()
{
    // TODO Auto-generated method stub
    super.onStop();
    if(player!= null)
    {
        player.release();
    }
}

public static YouTubeVideoFragment newInstance(String url)
{

    YouTubeVideoFragment f = new YouTubeVideoFragment();

    Bundle b = new Bundle();
    b.putString("url", url);

    f.setArguments(b);
    f.init();

    return f;
}

private void init()
{

    initialize("api_key", new OnInitializedListener() {

        @Override
        public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1)
        {

            Log.e("YouTubeFragment", "Error");
        }

        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored)
        {



            if (!wasRestored)
            {
                player.cueVideo(getArguments().getString("url"));
                player.play();//THIS DOESNT WORK!!!

            }
        }
    });
}

}

解决方法:

如果你需要在片段初始化后立即加载和播放视频,你应该只使用player.loadVideo(videoId)而不是player.cueVideo(videoId)并覆盖onLoaded()钩子.

看看文档:

loadVideo(String videoId)
Loads and plays the specified video.

cueVideo(String videoId)
Loads the specified video’s thumbnail and prepares the player to play the video, but does not download any of the video stream until play() is called.

标签:android,android-youtube-api,android-video-player
来源: https://codeday.me/bug/20190624/1279776.html