我正在按照本教程通过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视频。因此,我为onClickListener
设置button1
并尝试访问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?
答案 0 :(得分:0)
您需要创建一个新活动来扩展YouTubePlayerView上下文。这就是我的活动的样子,但你需要从main创建一个intent来调用它。 IE这是一个与主要不同的类。
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]);
}
答案 1 :(得分:0)
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) {
}
}