YouTube在Android中播放400错误

时间:2016-02-26 10:43:30

标签: android android-youtube-api

我尝试使用youtube视频API播放视频

当我输入静态ID时,表示当时代码正在工作时在文件中声明ID。

但是当我尝试从getIntent()获取ID时,我也获得了ID但视频无法播放。

我知道网络存在问题。

以下是我的代码。

这里是我的代码

private static final int RECOVERY_DIALOG_REQUEST = 1;
String DEVELOPER_KEY = "AryAGafadXF9ECWqjdPtsahshsrJHzg";
// YouTube player view
private YouTubePlayerView youTubeView;
String youtubeLink;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.youtube_main);
    Intent intent =getIntent();
    youtubeLink=intent.getStringExtra("youtubeLink");
    youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);

    // Initializing video player with developer key
    youTubeView.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(
                getString(R.string.error_player), errorReason.toString());
        Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
    }
}

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

        // loadVideo() will auto play video
        // Use cueVideo() method, if you don't want to play it automatically
        player.loadVideo(youtubeLink);

        // Hiding player controls
        player.setPlayerStyle(PlayerStyle.CHROMELESS);
    }
}

@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(Config.DEVELOPER_KEY, this);
    }
}

private YouTubePlayer.Provider getYouTubePlayerProvider() {
    return (YouTubePlayerView) findViewById(R.id.youtube_view);
}

}

我的错误

无法正常工作我遇到400网络问题错误

主要活动代码

    //youtube image display id and imageView
    youtube_image=(ImageView)findViewById(R.id.revi_fulldes_movieyoutubeImg);
    youtube_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent youtube= new Intent(getApplicationContext(),PlayYouTube1st.class);
            youtube.putExtra("youtubeLink",youtubeLink);
            startActivity(youtube);


        }
    });

    youtubeLink=Reviews_update.revData.get(mov_pos).getYoutubeLink();


    mov_pos=Integer.parseInt((getIntent().getExtras()).getString("mov_pos"));

1 个答案:

答案 0 :(得分:0)

当您在loadVideo(videoID)方法中将youtube full url作为参数时,会发生这种情况。

youtubeLink=intent.getStringExtra("youtubeLink");
 //check is youtube videoID 
//not give entire youtube url

仅将 YouTube ID ,而不是整个YouTube网址(youtubeLink)添加到loadVideo()方法。