在列表视图中播放 youtube 视频列表

Play list of youtube videos in listview(在列表视图中播放 youtube 视频列表)

本文介绍了在列表视图中播放 youtube 视频列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在播放 youtube 视频时遇到问题.让我解释一下,我有一个片段并放在 listview 上.在该列表中,每个项目都有单独的子视图.我被放置了 <com.google.android.youtube.player.YouTubeThumbnailView> 标签并尝试放置 Fragment.在这两种情况下,我都会遇到 Binary Inflated 异常.请告诉我任何解决我的问题的方法.

I am facing problem with show and play list of youtube videos. Let me explain, I have a fragment and placed on listview. In that list have separate child view for each item. I was placed <com.google.android.youtube.player.YouTubeThumbnailView> tag and also try to place Fragment. In both cases I am getting Binary Inflated exception. Please tell me any solution for my problem.

Process: com.example.sampleexamplefor_recyclerview, PID: 6510
    android.view.InflateException: Binary XML file line #28: Error inflating class com.google.android.youtube.player.YouTubePlayerView
            at android.view.LayoutInflater.createView(LayoutInflater.java:633)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at com.example.sampleexamplefor_recyclerview.RecyclerAdapter.onCreateViewHolder(RecyclerAdapter.java:40)
            at com.example.sampleexamplefor_recyclerview.RecyclerAdapter.onCreateViewHolder(RecyclerAdapter.java:24)
            at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5288)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4551)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1371)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
            at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
            at android.view.View.layout(View.java:15684)
            at android.view.ViewGroup.layout(ViewGroup.java:4981)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15684)
            at android.view.ViewGroup.layout(ViewGroup.java:4981)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15684)
            at android.view.ViewGroup.layout(ViewGroup.java:4981)
            at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
            at android.view.View.layout(View.java:15684)
            at android.view.ViewGroup.layout(ViewGroup.java:4981)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15684)
            at android.view.ViewGroup.layout(ViewGroup.java:4981)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2186)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1920)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
            at android.view.Choreographer.doCallbacks(Choreographer.java:596)
            at android.view.Choreographer.doFrame(Choreographer.java:557)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at com.example.sampleexamplefor_recyclerview.RecyclerAdapter.onCreateViewHolder(RecyclerAdapter.java:40)
            at com.example.sampleexamplefor_recyclerview.RecyclerAdapter.onCreateViewHolder(RecyclerAdapter.java:24)
            at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5288)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4551)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1371)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
            at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
            at android.view.View.layout(View.java:15684)
            at android.view.ViewGroup.layout(ViewGroup.java:4981)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15684)
            at android.view.ViewGroup.layout(ViewGroup.java:4981)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15684)
            at android.view.ViewGroup.layout(ViewGroup.java:4981)
            at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
            at android.view.View.layout(View.java:15684)
            at android.view.ViewGroup.layout(ViewGroup.java:4981)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15684)
            at android.view.ViewGroup.layout(ViewGroup.java:4981)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2186)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1920)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
            at android.view.Choreographer.doCallbacks(Choreographer.java:596)
            at android.view.Choreographer.doFrame(Choreographer.java:557)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
     Caused by:

适配器:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.VideoInfoHolder> implements YouTubePlayer.OnInitializedListener{

    //these ids are the unique id for each video
    String[] VideoID = {"P3mAtvs5Elc", "nCgQDjiotG0", "P3mAtvs5Elc"};
    Context ctx;
    YouTubeThumbnailLoader youTubeThumbnailLoader;
    private MyPlayerStateChangeListener playerStateChangeListener;
    private MyPlaybackEventListener playbackEventListener;
    private YouTubePlayer player;

    public RecyclerAdapter(Context context) {
        this.ctx = context;
    }

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

    @Override
    public void onBindViewHolder(final VideoInfoHolder holder, final int position) {

        holder. youTubeView.initialize("AIzaSyCS8nKb2V9RROPP3ehy4mhQbwhVdfe7V8o", this);
        playerStateChangeListener = new MyPlayerStateChangeListener();
        playbackEventListener = new MyPlaybackEventListener();

        holder.relativeLayoutOverYouTubeThumbnailView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.relativeLayoutOverYouTubeThumbnailView.setVisibility(View.GONE);
                player.play();

            }
        });

        final YouTubeThumbnailLoader.OnThumbnailLoadedListener  onThumbnailLoadedListener = new YouTubeThumbnailLoader.OnThumbnailLoadedListener(){
            @Override
            public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {

            }

            @Override
            public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
                youTubeThumbnailView.setVisibility(View.VISIBLE);
                holder.relativeLayoutOverYouTubeThumbnailView.setVisibility(View.VISIBLE);
            }
        };

        holder.relativeLayoutOverYouTubeThumbnailView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.relativeLayoutOverYouTubeThumbnailView.setVisibility(View.GONE);

            }
        });

        /*holder.youTubeThumbnailView.initialize("AIzaSyCS8nKb2V9RROPP3ehy4mhQbwhVdfe7V8o", new YouTubeThumbnailView.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
                youTubeThumbnailLoader = youTubeThumbnailLoader;
                youTubeThumbnailLoader.setVideo(VideoID[position]);
                youTubeThumbnailLoader.setOnThumbnailLoadedListener(onThumbnailLoadedListener);
            }

            @Override
            public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
                //write something for failure
            }
        });*/
    }

    @Override
    public int getItemCount() {
        return VideoID.length;
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean b) {
        this.player = player;
        player.setPlayerStateChangeListener(playerStateChangeListener);
        player.setPlaybackEventListener(playbackEventListener);

        if (!b) {
            player.cueVideo("fhWaJi1Hsfo"); // Plays https://www.youtube.com/watch?v=fhWaJi1Hsfo
        }
    }

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

    }

    public class VideoInfoHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        protected RelativeLayout relativeLayoutOverYouTubeThumbnailView;
        YouTubeThumbnailView youTubeThumbnailView;
        protected ImageView playButton;
        private YouTubePlayerView youTubeView;

        public VideoInfoHolder(View itemView) {
            super(itemView);
            playButton=(ImageView)itemView.findViewById(R.id.btnYoutube_player);
            playButton.setOnClickListener(this);
            relativeLayoutOverYouTubeThumbnailView = (RelativeLayout) itemView.findViewById(R.id.relativeLayout_over_youtube_thumbnail);
            youTubeThumbnailView = (YouTubeThumbnailView) itemView.findViewById(R.id.youtube_thumbnail);
//            youTubeView = (YouTubePlayerView)itemView. findViewById(R.id.youtube_view);

        }

        @Override
        public void onClick(View v) {

            Intent intent = YouTubeStandalonePlayer.createVideoIntent((Activity) ctx, "AIzaSyCS8nKb2V9RROPP3ehy4mhQbwhVdfe7V8o", VideoID[getLayoutPosition()]);
            ctx.startActivity(intent);

        }
    }


    private final class MyPlaybackEventListener implements YouTubePlayer.PlaybackEventListener {

        @Override
        public void onPlaying() {
            // Called when playback starts, either due to user action or call to play().

        }

        @Override
        public void onPaused() {
            // Called when playback is paused, either due to user action or call to pause().
        }

        @Override
        public void onStopped() {
            // Called when playback stops for a reason other than being paused.
        }

        @Override
        public void onBuffering(boolean b) {
            // Called when buffering starts or ends.
        }

        @Override
        public void onSeekTo(int i) {
            // Called when a jump in playback position occurs, either
            // due to user scrubbing or call to seekRelativeMillis() or seekToMillis()
        }
    }

    private final class MyPlayerStateChangeListener implements YouTubePlayer.PlayerStateChangeListener {

        @Override
        public void onLoading() {
            // Called when the player is loading a video
            // At this point, it's not ready to accept commands affecting playback such as play() or pause()
        }

        @Override
        public void onLoaded(String s) {
            // Called when a video is done loading.
            // Playback methods such as play(), pause() or seekToMillis(int) may be called after this callback.
        }

        @Override
        public void onAdStarted() {
            // Called when playback of an advertisement starts.
        }

        @Override
        public void onVideoStarted() {
            // Called when playback of the video starts.
        }

        @Override
        public void onVideoEnded() {
            // Called when the video reaches its end.
        }

        @Override
        public void onError(YouTubePlayer.ErrorReason errorReason) {
            // Called when an error occurs.
        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"

        card_view:cardCornerRadius="5dp"
        card_view:cardElevation="0dp"
        android:layout_marginTop="10dp"
        card_view:cardBackgroundColor="@android:color/black"

        card_view:cardPreventCornerOverlap="false"
        card_view:cardUseCompatPadding="true">


        <com.google.android.youtube.player.YouTubeThumbnailView
        android:id="@+id/youtube_thumbnail"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:scaleType="centerCrop"
        android:visibility="gone"/>

        <com.google.android.youtube.player.YouTubePlayerView
            android:id="@+id/youtube_view"
            android:layout_width="match_parent"
            android:layout_height="180dp"/>
        <RelativeLayout android:id="@+id/relativeLayout_over_youtube_thumbnail"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:background="#707070"
            android:visibility="visible">

            <ImageView android:id="@+id/btnYoutube_player"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="center"

                android:src="@mipmap/ic_launcher"/>

        </RelativeLayout>
    </android.support.v7.widget.CardView>

</RelativeLayout>

推荐答案

of 文档,

请注意,在播放视频时,此视图的最小尺寸为 200x110 dp.如果您将视图缩小,视频将自动停止播放.此外,在播放视频时,不允许将此片段的视图与其他视图重叠.

Note that while videos are playing, this View has a minimum size of 200x110 dp. If you make the view any smaller, videos will automatically stop playing. Also, it is not permitted to overlay this fragment's view with other views while a video is playing.

这篇关于在列表视图中播放 youtube 视频列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在列表视图中播放 youtube 视频列表

基础教程推荐