问题
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