如何在同一活动中添加其他YouTube播放器

时间:2018-11-08 06:28:24

标签: android android-studio android-fragments

如何在同一活动中添加其他youtube播放器,我是否需要其他api密钥才能用于另一个youtube播放器视图

我正在使用的YouTube库是Android Youtube Player

这是我的Java类的代码,其中包括我尝试编写的代码

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.YouTubePlayerFragment;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import android.os.Bundle;
import android.widget.Toast;
import android.content.Intent;

public class MainActivity extends YouTubeBaseActivity
    implements YouTubePlayer.OnInitializedListener{

public static final String DEVELOPER_KEY = "YOUR API KEY";
private static final String VIDEO_ID = "YOUR VIDEO ID";
private static final int RECOVERY_DIALOG_REQUEST = 1;
YouTubePlayerFragment myYouTubePlayerFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myYouTubePlayerFragment = (YouTubePlayerFragment)getFragmentManager()
            .findFragmentById(R.id.youtubeplayerfragment);
    myYouTubePlayerFragment.initialize(DEVELOPER_KEY, this);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
                                    YouTubeInitializationResult 

errorReason) {
    if (errorReason.isUserRecoverableError()) {
        errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
    } else {
        String errorMessage = String.format(
                "There was an error initializing the YouTubePlayer  
(%1$s)",
                errorReason.toString());
        Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
    }
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer  

player,
                                    boolean wasRestored) {
    if (!wasRestored) {
        player.cueVideo(VIDEO_ID);
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent  
data) {
    if (requestCode == RECOVERY_DIALOG_REQUEST) {
        // Retry initialization if user performed a recovery action
        getYouTubePlayerProvider().initialize(DEVELOPER_KEY, this);
    }
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
    return (YouTubePlayerView)findViewById(R.id.youtubeplayerfragment);
}
}

0 个答案:

没有答案