1

我正在按照本教程通过 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?

4

2 回答 2

0

您需要创建一个新活动来扩展 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]);
                   } 
于 2013-11-25T20:57:04.320 回答
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) {

    }
}
于 2017-02-20T15:48:33.343 回答