gpt4 book ai didi

android - 如何通过 YouTube 播放器自动播放视频

转载 作者:行者123 更新时间:2023-11-30 00:51:27 25 4
gpt4 key购买 nike

我正在开发一个应用程序,在该应用程序中,我在 RecyclerView 中显示了 YouTube 视频缩略图,顶部有一个 YouTube 播放器 fragment ,用于播放用户选择的视频。我成功了。但问题是我想自动播放用户选择的视频。下面是我的代码:main_activity.xml:

<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:weightSum="2"
android:orientation="vertical"
tools:context="com.example.pc.fkidshell.Main2Activity">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/VideoFragment"
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:layout_below="@+id/my_thirdtoolbar"/>

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/VideoList"
android:layout_below="@+id/VideoFragment"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>

secvideo_row.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/thumbnailView"
android:layout_gravity="center_horizontal"/>

</LinearLayout>

Main_Activity.java:

public class Main2Activity extends AppCompatActivity implements YouTubeThumbnailView.OnInitializedListener, YouTubeThumbnailLoader.OnThumbnailLoadedListener, YouTubePlayer.OnInitializedListener {
YouTubePlayerFragment playerFragment;
YouTubePlayer Player;
YouTubeThumbnailView thumbnailView;
YouTubeThumbnailLoader thumbnailLoader;
RecyclerView VideoList;
RecyclerView.Adapter adapter;
List<Drawable> thumbnailViews;
List<String> VideoId;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
thumbnailViews = new ArrayList<>();
VideoList = (RecyclerView) findViewById(R.id.VideoList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
VideoList.setLayoutManager(layoutManager);
adapter = new Main2Activity.VideoListAdapter();
VideoList.setAdapter(adapter);
VideoId = new ArrayList<>();
thumbnailView = new YouTubeThumbnailView(this);
thumbnailView.initialize("AIzaSyAXlMCst9tNrTGz4xAZ0mY6mJlkNU-3DAs", this);
playerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.VideoFragment);
playerFragment.initialize("AIzaSyAXlMCst9tNrTGz4xAZ0mY6mJlkNU-3DAs", this);
}

@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
thumbnailLoader = youTubeThumbnailLoader;
youTubeThumbnailLoader.setOnThumbnailLoadedListener(Main2Activity.this);
thumbnailLoader.setPlaylist("PLXRActLQ03oY_6AQb-5EMuKFYQA_fDE40");
}

@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
}

public void add() {
adapter.notifyDataSetChanged();
if (thumbnailLoader.hasNext())
thumbnailLoader.next();
}

@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
thumbnailViews.add(youTubeThumbnailView.getDrawable());
VideoId.add(s);
add();
}

@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {

}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
Player = youTubePlayer;
Player.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
@Override
public void onFullscreen(boolean b) {
VideoList.setVisibility(b ? View.GONE : View.VISIBLE);
}
});
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}

public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.MyView> {

public class MyView extends RecyclerView.ViewHolder {
ImageView imageView;
public MyView(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.thumbnailView);
}
}

@Override
public VideoListAdapter.MyView onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.secvideo_row, parent, false);
return new MyView(itemView);
}

@Override
public void onBindViewHolder(VideoListAdapter.MyView holder, final int position) {
holder.imageView.setImageDrawable(thumbnailViews.get(position));
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Player.loadVideo(VideoId.get(position));
//Player.loadVideo(VideoId.get(position));
}
});
}

@Override
public int getItemCount() {
return thumbnailViews.size();
}
}
}

最佳答案

初始化 Youtube 播放器时,设置播放器状态更改监听器,以便您可以在加载视频时播放视频,并确保您的 Activity 实现播放器状态回调。详细代码:

    @Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
Player = youTubePlayer;
Player.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
@Override
public void onFullscreen(boolean b) {
VideoList.setVisibility(b ? View.GONE : View.VISIBLE);
}
});
Player.setPlayerStateChangeListener(this); //set player state change listener
}

@Override
public void onAdStarted() {
}

@Override
public void onLoaded(String videoId) {
if(!TextUtils.isEmpty(videoId) && Player != null)
Player.play(); //auto play
}

@Override
public void onLoading() {
}

@Override
public void onVideoEnded() {
}

@Override
public void onVideoStarted() {

}

@Override
public void onError(ErrorReason reason) {
Log.e("onError", "onError : " + reason.name());
}

关于android - 如何通过 YouTube 播放器自动播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40959119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com