Android - Playing Youtube Videos using Youtube Player API in ListView

左心房为你撑大大i 提交于 2019-12-08 14:08:36

问题


I have tried using WebViews to display Youtube Videos in ListView, what happens is when i scroll off screen and back to the cell the Video was located the Video disappears meaning that the WebView does not render back the WebView with the Video in it. So i tried using YoutubePlayerAPI but i am finding it difficult to understand how i can insert these Videos using the YoutubePlayerAPI.

Could someone please advise?

I have tried using YoutubePlayerView in my ArrayAdapter but i get Inflate errors, i have also created YoutubeBaseActivity and YoutubeFragment but cant understand how i get the layout or ui to display these videos in my ListView Cells.


回答1:


How i did it

Download the youtube android SDK from here then get the jar file and copy it to libs folder on your app.

Enter this in dependency in gradle.build under dependencies

compile files('libs/YouTubeAndroidPlayerApi.jar')

Syc and build.

Create an activity that extends YoutubeBaseActivity like below and implement the YoutubePlayer.OnInitializedListener as shown below. Override the methods on the interace. Android studio can help you do this.

public class Display extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

Then, when it loads successfully,

@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean b) {
    if (!b) {
        player.cueVideo(videoID);
    }
}

This will load the youtube player and you play on the API

Happy coding, sorry am late



来源:https://stackoverflow.com/questions/35633683/android-playing-youtube-videos-using-youtube-player-api-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!