gpt4 book ai didi

android - ViewPager 和 YouTubePlayer

转载 作者:行者123 更新时间:2023-11-29 17:47:16 43 4
gpt4 key购买 nike

在我的 Activity 中,我有一个 viewPager,例如 3 页。在所有这些页面中,我都有一个带有不同 ID 视频的 YouTubePlayer。

问题是所有 YouTubePlayer 组件同时有相同的视频。

具体来说:

当显示第一页时,YouTubePlayer 显示第一个视频,所以一切似乎都正常。当我尝试滚动到第二页时,我可以看到第二个 YouTubePlayer 显示相同的视频。当第二页完全显示时(滚动后),在 View 寻呼机后面准备第三页。所以在第 3 页上,第 3 个 id video 被设置为 YouTubePlayer 组件。此时当前显示的第2页自动切换到第3个视频。

这就像 YouTubePlayer 的实例对于所有页面都是单一的。但是在每个页面上 new FragmentYouTubePlayer() 都被正确调用。

我不明白问题出在哪里。最后,我想知道是否可以同时使用多个 YouTubePlayer 组件(在 viewpager 中)?

感谢您的帮助。

最佳答案

我不记得到底出了什么问题,但是是的,我解决了我的问题。所以,如果它可以帮助你,这里是代码:

在我的主要 Activity 中,使用 PagerAdapter :

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}

@Override
public android.support.v4.app.Fragment getItem(int position) {
fragmentYouTubeContent = FragmentYouTubeContent.create(position);
return fragmentYouTubeContent;
}

@Override
public int getCount() {
int nb = 3;
return nb;
}
}

我的 FragmentYouTubeContent.class

package com.orange.OrangeJobs;


import java.util.List;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeStandalonePlayer;
import com.google.android.youtube.player.YouTubeThumbnailLoader;
import com.google.android.youtube.player.YouTubeThumbnailView;


import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;


public class FragmentYouTubeContent extends Fragment implements YouTubeThumbnailView.OnInitializedListener {

public static final int REQ_START_STANDALONE_PLAYER = 101;
private static final int REQ_RESOLVE_SERVICE_MISSING = 2;
private static final int RECOVERY_DIALOG_REQUEST = 1;

/**
* The fragment's page number.
*/
private int mPageNumber;
private String urlYouTube;
private boolean canHideStatusBar = false;
private Dialog errorDialog;
FrameLayout rl;
private YouTubeThumbnailView thumbnailView;

/**
* Factory method for this fragment class. Constructs a new fragment for the given page number.
*/
public static FragmentYouTubeContent create(int pageNumber) {
FragmentYouTubeContent fragment = new FragmentYouTubeContent();
fragment.setPageNumber(pageNumber);
return fragment;
}

public FragmentYouTubeContent() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@SuppressLint("InlinedApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setRetainInstance(true);
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.youtube, container, false);
urlYouTube = "YOUR_VIDEO_URL";
if (!"".equals(urlYouTube)) {
thumbnailView = (YouTubeThumbnailView) rootView.findViewById(R.id.youtubethumbnailview);
thumbnailView.initialize(Params.YOUTUBE_KEY, this);
thumbnailView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// Launch standalone YoutTube player
Intent intent = null;
intent = YouTubeStandalonePlayer.createVideoIntent(getActivity(), YOUTUBE_KEY, urlYouTube, 0, true, false);
if (intent != null) {
if (canResolveIntent(intent)) {
canHideStatusBar = true;
startActivityForResult(intent, REQ_START_STANDALONE_PLAYER);
} else {
// Could not resolve the intent - must need to install or update the YouTube API service.
YouTubeInitializationResult
.SERVICE_MISSING
.getErrorDialog(getActivity(), REQ_RESOLVE_SERVICE_MISSING).show();
}
}
}});
}
return rootView;
}

@Override
public void onResume() {
super.onResume();
boolean isLandscape = false;
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
isLandscape = true;
}
else {
isLandscape = false;
}
if (canHideStatusBar && this.isVisible() && isLandscape) {
Utils.hideStatusBar(getActivity());
canHideStatusBar = false;
}
}

@Override
public void onPause() {
super.onPause();
}


@Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView, YouTubeThumbnailLoader thumbnailLoader) {
thumbnailLoader.setVideo(urlYouTube);
}

@Override
public void onInitializationFailure(YouTubeThumbnailView thumbnailView, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
if (errorDialog == null || !errorDialog.isShowing()) {
errorDialog = errorReason.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST);
errorDialog.show();
}
} else {
String errorMessage = String.format(getString(R.string.error_thumbnail_view), errorReason.toString());
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
}
}

// ***** Private methods *************************************************************************************

private boolean canResolveIntent(Intent intent) {
List<ResolveInfo> resolveInfo = getActivity().getPackageManager().queryIntentActivities(intent, 0);
return resolveInfo != null && !resolveInfo.isEmpty();
}

// ***** Properties methods *************************************************************************************
/**
* Returns the page number represented by this fragment object.
*/
public int getPageNumber() {
return mPageNumber;
}

/**
* @param pageNumber the pageNumber to set
*/
public void setPageNumber(int pageNumber) {
this.mPageNumber = pageNumber;
}

/**
* @return the canHideStatusBar
*/
public boolean isCanHideStatusBar() {
return canHideStatusBar;
}

/**
* @param canHideStatusBar the canHideStatusBar to set
*/
public void setCanHideStatusBar(boolean canHideStatusBar) {
this.canHideStatusBar = canHideStatusBar;
}

}

我的布局:youtube.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:background="@color/black"
android:scrollbarStyle="outsideOverlay"
android:orientation="horizontal" >


<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtubethumbnailview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="@string/empty_string"
android:src="@drawable/ic_media_embed_play"
/>

</RelativeLayout>

关于android - ViewPager 和 YouTubePlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25624454/

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