Youtube播放器API访问被拒绝,找不到属性。

huangapple go评论68阅读模式
英文:

Youtube player API Access denied finding property

问题

package Thrax.com;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

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;

public class YoutubeActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener
{

    static final String GOOGLE_API_KEY = "在这里填入我的Google API_KEY";
    static final String YOUTUBE_VIDEO_ID = "ZnZsVLiVPLs";
    static final String YOUTUBE_PLAYLIST = "PLzcK7AwShFBmc0Pigajs_u5nhQIwARAmE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ConstraintLayout layout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_youtube, null);
        setContentView(layout);

        YouTubePlayerView playerView = new YouTubePlayerView(this);
        playerView.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT , ViewGroup.LayoutParams.MATCH_PARENT ));
        layout.addView(playerView);
        playerView.initialize(GOOGLE_API_KEY , this);

    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {

    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        final int REQUEST_CODE = 1;

        if(youTubeInitializationResult.isUserRecoverableError()){
            youTubeInitializationResult.getErrorDialog(this , REQUEST_CODE).show();

        }

        else{

            String errorMessage = String.format("初始化YouTube播放器时出错(%1$s)" , youTubeInitializationResult.toString());
            Toast.makeText(this , errorMessage , Toast.LENGTH_LONG).show();
        }

    }
}
英文:

Iam trying to build simple youtubeplayer android app, after running my code on my physical device the app runs successfully but the video remains in loading mode, When i checked my logcat i found some issues maybe related or causes this problem:

Youtube播放器API访问被拒绝,找不到属性。

And this is the code i run:

package Thrax.com;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
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;
public class YoutubeActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener
{
static final String GOOGLE_API_KEY = "Implement my google API_KEY HERE";
static final String YOUTUBE_VIDEO_ID = "ZnZsVLiVPLs";
static final String YOUTUBE_PLAYLIST = "PLzcK7AwShFBmc0Pigajs_u5nhQIwARAmE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_youtube);
//        ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.activity_youtube);
ConstraintLayout layout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_youtube, null);
setContentView(layout);
//        Button button1 = new Button(this);
//        button1.setLayoutParams(new ConstraintLayout.LayoutParams(360,100));
//        button1.setText("Button_Dynamic");
//        layout.addView(button1);
YouTubePlayerView playerView = new YouTubePlayerView(this);
playerView.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT , ViewGroup.LayoutParams.MATCH_PARENT ));
layout.addView(playerView);
playerView.initialize(GOOGLE_API_KEY , this);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
final int REQUEST_CODE = 1;
if(youTubeInitializationResult.isUserRecoverableError()){
youTubeInitializationResult.getErrorDialog(this , REQUEST_CODE).show();
}
else{
String errorMessage = String.format("There was a error initializing Youtube player (%1$s)" , youTubeInitializationResult.toString());
Toast.makeText(this , errorMessage , Toast.LENGTH_LONG).show();
}
}
}
</details>
# 答案1
**得分**: 0
将你的`onInitializationSuccess()`方法更改为以下内容:
```java
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);
youTubePlayer.setPlaybackEventListener(playbackEventListener);
if (!b) {
youTubePlayer.cueVideo(VIDEO_ID);
}
}

然后创建以下方法,用于实现playerStateChangeListener()playbackEventListener()

private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
    @Override
    public void onLoading() {

    }

    @Override
    public void onLoaded(String s) {

    }

    @Override
    public void onAdStarted() {

    }

    @Override
    public void onVideoStarted() {

    }

    @Override
    public void onVideoEnded() {

    }

    @Override
    public void onError(YouTubePlayer.ErrorReason errorReason) {

    }
};

private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
    @Override
    public void onPlaying() {

    }

    @Override
    public void onPaused() {

    }

    @Override
    public void onStopped() {

    }

    @Override
    public void onBuffering(boolean b) {

    }

    @Override
    public void onSeekTo(int i) {

    }
};

应该可以正常工作。

英文:

Change your onInitializationSuccess()to have;

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);
youTubePlayer.setPlaybackEventListener(playbackEventListener);
if (!b) {
youTubePlayer.cueVideo(VIDEO_ID);
}
}

And then create Methods to have playerStateChangeListener() and playbackEventListener() like below.

private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onLoading() {
}
@Override
public void onLoaded(String s) {
}
@Override
public void onAdStarted() {
}
@Override
public void onVideoStarted() {
}
@Override
public void onVideoEnded() {
}
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
}
};
private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
@Override
public void onPlaying() {
}
@Override
public void onPaused() {
}
@Override
public void onStopped() {
}
@Override
public void onBuffering(boolean b) {
}
@Override
public void onSeekTo(int i) {
}
};

Should work.

huangapple
  • 本文由 发表于 2020年8月28日 05:59:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63624689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定