In my android app I have a feedwall for images, gifs and videos.For playing videos I am making use of Android YouTube api which plays videos from YouTube links(URLs). The feedwall is a listview which makes use of a Base Adapter. For videos I am using a FrameLayout which is contained inside a RelativeLayout as below
<RelativeLayout
android:id="@+id/feedVideoRelLayout"
android:layout_width="wrap_content"
android:layout_height="300dp">
<FrameLayout
android:id="@+id/youTubePlayerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
I am casting this Framelayout as a YouTubePlayerSupportFragment as below
YouTubePlayerSupportFragment youTubePlayerLayout;
viewHolder.youTubePlayerLayout = (YouTubePlayerSupportFragment) activityRef.getSupportFragmentManager().findFragmentById(R.id.youTubePlayerLayout);
Now for playing videos in my feedwall,inside the getView method of the adapter I am using
viewHolder.youTubePlayerLayout = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction;
transaction = activityRef.getSupportFragmentManager().beginTransaction();
transaction.add(R.id.youTubePlayerLayout, viewHolder.youTubePlayerLayout).commit();
viewHolder.youTubePlayerLayout.initialize(DeveloperKey.DEVELOPER_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.loadVideo(Utils.getYouTubeVideoIdFromUrl(feedModelArrayList.get(position).getPostMediaURL()));
player.setShowFullscreenButton(false);
player.play();
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
String errorMessage = arg0.toString();
Toast.makeText(activityRef, errorMessage, Toast.LENGTH_LONG).show();
Log.d("errorMessage:", errorMessage);
}
});
In my feedwall whenever a video link is detected the video starts playing in the YouTube player.Now the problem is, this happens only for the first time. After scrolling down if again a video link is detected the player loads the video but stops immediately after playing, within a second. Secondly, the first video feed which played successfully also behaves same, even after tapping the play button.
In the logcat I get the warnings like
W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is not contained inside its ancestor android.widget.FrameLayout{d56f6d9 V.E...... ......I. 0,0-0,0}. The distances between the ancestor's edges and that of the YouTubePlayerView is: left: 0, top: 0, right: 0, bottom: 0 (these should all be positive).
I went through all the threads related to this warning on SO but unfortunately none of them were useful.