来自Youtube API的视频无法在onClick上播放

时间:2013-11-25 20:49:55

标签: java android youtube youtube-api android-youtube-api

我不确定为什么但是由于某些原因我的视频在点击时似乎没有播放。相反,他们在播放器中排队,但不玩。有没有人以前经历过这个或者知道如何解决这个问题?

来源:

public class Home extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
YouTubeThumbnailView.OnInitializedListener {

    public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    public static final String VIDEO_ID = "o7VVHhK9zf0";
    public static final String VIDEO1_ID = "xVHHJqntuXI";
    public static final String VIDEO2_ID = "YWteQj_q3Ro";
    public static final String VIDEO3_ID = "83ZgtqTw-mI";
    public static final String VIDEO4_ID = "n5wMza29JwI";
    private YouTubePlayer youTubePlayer;
    private YouTubePlayerView youTubePlayerView;
    private YouTubeThumbnailView youTubeThumbnailView1;
    private YouTubeThumbnailView youTubeThumbnailView2;
    private YouTubeThumbnailView youTubeThumbnailView3;
    private YouTubeThumbnailView youTubeThumbnailView4;
    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private DrawerLayout mDrawerLayout;
    private ListView drawerListView;
    private ListView mDrawerList;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private ActionBarDrawerToggle mDrawerToggle;

    private YouTubeThumbnailLoader youTubeThumbnailLoader;
    ScrollView mainScrollView;
    Button fav_up_btn1;
    Button fav_dwn_btn1;
    ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);


        final ActionBar actionBar = getActionBar();
        actionBar.setCustomView(R.layout.actionbar_custom_view_home);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);

        // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);

        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
                drawerLayout, /* DrawerLayout object */
                R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open, /* "open drawer" description */
                R.string.drawer_close /* "close drawer" description */
                );
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);

        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
        youTubePlayerView.initialize(API_KEY, this);

        mainScrollView = (ScrollView) findViewById(R.id.groupScrollView);

        youTubeThumbnailView1 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview1);
        youTubeThumbnailView1.initialize(API_KEY, this);

        fav_up_btn1 = (Button) findViewById(R.id.fav_up_btn1);

        fav_up_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean favIsUp = fav_up_btn1
                        .getBackground()
                        .getConstantState()
                        .equals(getResources().getDrawable(
                                R.drawable.fav_up_btn1).getConstantState());
                // set the background
                fav_up_btn1
                .setBackgroundResource(favIsUp ? R.drawable.fav_dwn_btn1
                        : R.drawable.fav_up_btn1);
            }
        });

        youTubeThumbnailView1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO1_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }
        });

        youTubeThumbnailView2 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview2);
        youTubeThumbnailView2.initialize(API_KEY, this);

        youTubeThumbnailView2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg1) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO2_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }
        });

        youTubeThumbnailView3 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview3);
        youTubeThumbnailView3.initialize(API_KEY, this);

        youTubeThumbnailView3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg1) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO3_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }
        });

        youTubeThumbnailView4 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview4);
        youTubeThumbnailView4.initialize(API_KEY, this);

        youTubeThumbnailView4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg1) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO4_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }

        });

    }

    @Override
    public void onInitializationFailure(Provider provider,
            YouTubeInitializationResult result) {

    }

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

        youTubePlayer = player;

        if (!wasRestored) {
            player.cueVideo(VIDEO_ID);

        }
    }

    @Override
    public void onInitializationFailure(YouTubeThumbnailView thumbnailView,
            YouTubeInitializationResult error) {

    }

    @Override
    public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
            YouTubeThumbnailLoader thumbnailLoader) {

        youTubeThumbnailLoader = thumbnailLoader;
        thumbnailLoader
        .setOnThumbnailLoadedListener(new ThumbnailLoadedListener());

        youTubeThumbnailLoader.setVideo(VIDEO_ID);

    }

    private final class ThumbnailLoadedListener implements
    YouTubeThumbnailLoader.OnThumbnailLoadedListener {

        @Override
        public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {

        }

        @Override
        public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {

        }

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns
        // true
        // then it has handled the app icon touch event
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:1)

替换

youTubePlayer.cueVideo(VIDEO1_ID);
youTubePlayer.play();

使用

youTubePlayer.loadVideo(VIDEO1_ID);