我是新来的这个网站,到目前为止,仅从浏览网站中就帮助了我很多。
我正在构建一个包含来自YouTube的播放列表的应用程序,到目前为止它正在工作,但我只有一个视频,下一个视频只会在按下“下一个”按钮后显示,我想创建该播放列表中所有视频的列表。
目前在使用Java和XML进行Android开发,如果可能的话,希望保持这种方式。
谢谢。
我的代码:
import android.drm.DrmStore;
import android.os.Bundle;
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.YouTubePlayer.ErrorReason;
import com.google.android.youtube.player.YouTubePlayer.PlaybackEventListener;
import com.google.android.youtube.player.YouTubePlayer.PlayerStateChangeListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.OnFullscreenListener;
public class ky extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
public static final String API_KEY = "api";
//http://youtu.be/<VIDEO_ID>
public static final String VIDEO_ID = "PLZeI1YOvEK7EK499BrkBl95VseODbMDa-";
public void onFullscreen(boolean isFullscreen) {
if (isFullscreen)
playbackEventListener.onPlaying();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** attaching layout xml **/
setContentView(R.layout.ky);
/** Initializing YouTube player view **/
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(API_KEY, this);
}
@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, final YouTubePlayer player, boolean wasRestored ) {
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
player.setOnFullscreenListener(new OnFullscreenListener() {
@Override
public void onFullscreen(boolean b) {
if(b)
player.play();
else
player.play();
}
});
/** Start buffering **/
if (!wasRestored) {
player.loadPlaylist(VIDEO_ID);
}
}
private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {
@Override
public void onBuffering(boolean arg0) {
}
@Override
public void onPaused() {
}
@Override
public void onPlaying() {
}
@Override
public void onSeekTo(int arg0) {
}
@Override
public void onStopped() {
}
};
private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
@Override
public void onAdStarted() {
}
@Override
public void onError(ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
}
@Override
public void onVideoStarted() {
}
};
}
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:background="@color/black"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".example">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"/>
</RelativeLayout>