来自 onClickListener 的 Android YouTube API 访问播放器
Posted
技术标签:
【中文标题】来自 onClickListener 的 Android YouTube API 访问播放器【英文标题】:Android YouTube API access player from onClickListener 【发布时间】:2013-09-11 14:53:32 【问题描述】:我正在按照本教程通过 YouTube API 设置自定义 YouTube 播放器:http://www.techrepublic.com/blog/android-app-builder/using-googles-youtube-api-in-your-android-apps/
这是加载预设 YouTube 视频的代码:
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener
static private final String DEVELOPER_KEY = "mykey";
static private final String VIDEO = "Yc8YrVc47TI";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(DEVELOPER_KEY, this);
@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult error)
Toast.makeText(this, "Oh no! "+error.toString(), Toast.LENGTH_LONG).show();
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored)
player.loadVideo(VIDEO);
现在我正在尝试通过单击一个按钮来加载不同的 YouTube 视频。所以我为我的button1
设置了一个onClickListener
并尝试访问YouTube 播放器,但它不起作用。
public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener
static private final String DEVELOPER_KEY = "mykey";
static private final String VIDEO = "Yc8YrVc47TI";
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(DEVELOPER_KEY, this);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener()
public void onClick(View v)
youTubeView.loadVideo("Erd2k6EKxCQ");
);
@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult error)
Toast.makeText(this, "Oh no! "+error.toString(), Toast.LENGTH_LONG).show();
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored)
player.loadVideo(VIDEO);
如何将特定视频从 button1 的 onClickListener 加载到 YouTubePlayer player.component?
【问题讨论】:
【参考方案1】:您需要创建一个新活动来扩展 YouTubePlayerView 上下文。这就是我的活动的样子,但是您需要从 main 创建一个意图来调用它。 IE 这是一个与 main 不同的类。
public class Beat extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener, OnClickListener
static private final String DEVELOPER_KEY = "your key here";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.beat);
// changes to xml file named beat
// initializes the video
YouTubePlayerView yt2 = (YouTubePlayerView)findViewById(R.id.youtube_view2);
yt2.initialize(DEVELOPER_KEY, this);
setupActionBar();
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored)
player.loadVideo(Main.VIDEO[0]);
【讨论】:
我解决了我的问题。我已经按照你说的做了,所以不是这样。问题是我在 main 函数上方定义了一个视图 - 因此 main 甚至无法启动。【参考方案2】:public class Community extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener,View.OnClickListener
final static String youtube_key = " you key ";
YouTubePlayerView youTubePlayerView;
YouTubePlayer.OnInitializedListener onInitializedListener;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_community);
youTubePlayerView = (YouTubePlayerView) findViewById(R.id.home_community_youtubeview);
youTubePlayerView.initialize(youtube_key,this);
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b)
youTubePlayer.loadVideo("vk1Rk5AaDO4");
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult)
@Override
public void onClick(View view)
【讨论】:
以上是关于来自 onClickListener 的 Android YouTube API 访问播放器的主要内容,如果未能解决你的问题,请参考以下文章
java 警报对话框。字体:来自https://stackoverflow.com/questions/2115758/how-do-i-display-an-alert-dialog-on-andr
ViewPager 中的 Fragment 按钮在错误引用时触发 onClickListener
如何在 Android 应用程序中为联系我们表单设置 onClickListener
Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法