gpt4 book ai didi

android - 以编程方式播放 YouTubePlayer 不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:09:11 26 4
gpt4 key购买 nike

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

我想在用户输入 fragment 时立即播放视频,所以我使用命令 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!!!

}
}
});
}

最佳答案

如果您需要在 fragment 初始化后立即加载和播放视频,您应该简单地使用 player.loadVideo(videoId) 而不是 player.cueVideo(videoId) 并覆盖 onLoaded() 钩子(Hook)。

查看文档:

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 - 以编程方式播放 YouTubePlayer 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22176565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com