我正在使用Android Studio开发应用程序,我需要使用Youtube API。问题是我想要播放视频 每次用户点击" Reproducir"(" play")。所以我研究了如何加载视频,每个人都说要使用这个功能 初始化将调用onInitializationSuccess并在那里播放。但是,如果我想加载新视频,解析网址并播放新视频,我就无法做到。每次用户点击Reproducir时我都尝试调用初始化,但在这种情况下,它是ig。
public class AddVideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{
private YouTubePlayerView youTubeView;
private EditText urlTxtView;
private Button playButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_video);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_player_add);
urlTxtView = (EditText) findViewById(R.id.urlYoutube);
playButton = (Button) findViewById(R.id.buttonPlay);
playButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
youTubeView.initialize(Config.YOUTUBE_API_KEY, (YouTubePlayer.OnInitializedListener) view.getContext());
}
});
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
String url = urlTxtView.getText().toString();
Log.d("1",url );
youTubePlayer.loadVideo("a4NT5iBFuZs");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "ERROR:"+result.toString(), Toast.LENGTH_LONG).show();
}
XML布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/action_add_video"
android:textStyle="normal|bold"
android:textSize="30sp"
android:gravity="center_horizontal"
android:layout_marginBottom="20dp" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_player_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"/>
<EditText
android:id="@+id/tituloVideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Titulo"
android:inputType="textPersonName" />
<EditText
android:id="@+id/urlYoutube"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="URL o buscar con @youtube"
android:inputType="textPersonName" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/buttonCancelar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancelar" />
<Button
android:id="@+id/buttonPlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reproducir" />
<Button
android:id="@+id/buttonGuardar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Guardar" />
</LinearLayout>
</LinearLayout>
日志: W / YouTubeAndroidPlayerAPI:YouTube视频播放因播放器顶部未经授权的覆盖而停止。 YouTubePlayerView被android.view.View {a21cb6f V.ED .... ........ 0,0-768,50#102002f android:id / statusBarBackground}遮挡。底边距离YouTubePlayerView顶部边缘36 px。
这里有一个想法的图像:
答案 0 :(得分:0)
如果您要重新启动YouTube视频视频,如果已经初始化,则需要先发布自己的视频。
请检查以下代码 具体来说,我已经在Play按钮上更改了初始化部分
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (youTubePlayer != null) {
youTubePlayer.release();
}
youTubeView.initialize("AIzaSyDo9p_Qjy8XRBdFnVOaIx586MzHHLOUopw", (YouTubePlayer.OnInitializedListener) view.getContext());
}
});
整个活动代码
public class AddVideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private YouTubePlayerView youTubeView;
private EditText urlTxtView;
private Button playButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_video);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_player_add);
urlTxtView = (EditText) findViewById(R.id.urlYoutube);
playButton = (Button) findViewById(R.id.buttonPlay);
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (youTubePlayer != null) {
youTubePlayer.release();
}
youTubeView.initialize(Config.YOUTUBE_API_KEY, (YouTubePlayer.OnInitializedListener) view.getContext());
}
});
}
private YouTubePlayer youTubePlayer;
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
this.youTubePlayer = youTubePlayer;
String url = urlTxtView.getText().toString();
Log.d("1", url);
if (!wasRestored) {/// here videoid you need to pass not entire video URL
youTubePlayer.loadVideo(url);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "ERROR:" + result.toString(), Toast.LENGTH_LONG).show();
}
}
要加载视频,您需要传递视频ID而不是视频网址。所以假设在编辑文本视频ID将被通过或者如果您将从网址获取视频ID并且将加载视频
this.youTubePlayer = youTubePlayer;
String url = urlTxtView.getText().toString();
Log.d("1", url);
if (!wasRestored) {/// here videoid you need to pass not entire video URL
youTubePlayer.loadVideo(url);
}
以下日志
日志:W / YouTubeAndroidPlayerAPI:YouTube视频播放因此而停止 未经授权的覆盖在玩家之上。 YouTubePlayerView是 被android.view.View遮挡{a21cb6f V.ED .... ........ 0,0-768,50
102002f android:id / statusBarBackground}。底边36 px低于YouTubePlayerView的上边缘。
您可以参考答案https://stackoverflow.com/a/29676512/1140237
在您的情况下,此日志...问题与应用程序或活动主题有关。不提供ActionBarTheme来解决该错误