英文:
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:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论