Youtube Player与Android App中的缩略图集成

时间:2019-01-08 14:55:44

标签: android youtube

您好,我遵循CodingwithMitch上有关在Android中使用Youtube API的教程。这是一个很好的入门教程。 一切运行正常,但是当播放器闲置时,屏幕只是空白,而不显示视频缩略图/预览。 screenshot of my app

我想让待机屏幕显示vid缩略图,以便我知道将播放什么视频。

MyActivity.xml中的代码如下:

<view                    class="com.google.android.youtube.player.YouTubePlayerView"
                android:id="@+id/youtubePlay"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:orientation="vertical"

YouTube配置代码:

public class YouTubeConfig {public YouTubeConfig() {} private static final String API_KEY = "AIzaSyXXXXXX";public static String getApiKey(){
return API_KEY;}}

在我的MainActivity.java

private static final String TAG = "MainActivity";
YouTubePlayerView mYoutubePlayerView;
Button btnPlay;
YouTubePlayer.OnInitializedListener mOnInitializedListener;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //PlayYouTube
    Log.d(TAG, "onCreate: Starting.");
    btnPlay = (Button) findViewById(R.id.btnPlay);
    mYoutubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubePlay);
    mOnInitializedListener = new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            Log.d(TAG, "onInitializationSuccess: Done Initialising.");
            youTubePlayer.loadPlaylist("PLZRTg9sNaMaFnh04-HODVcYRlFtKO8rsS");
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
            Log.d(TAG, "onInitializationSuccess: Fail to Initialize.");
        }
    };
    btnPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: Initializing Youtube Player.");
            mYoutubePlayerView.initialize(YouTubeConfig.getApiKey(), mOnInitializedListener);
        }
    });

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您需要提供要显示其预览的youtube视频的缩略图。 Get a thumbnail from youtube video