我正在开发Android应用,就像使用youtube在视图寻呼机中播放多个视频一样。我无法播放所有视频。只有第一个和丢失的位置视频正在播放,其他视频显示为空白屏幕。
这是我的主要代码 -
public class MainActivity extends FragmentActivity {
// Widgets
private VerticalViewPager viewPager;
private PagerAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
find_view_by_id();
fetchVideo();
}
private void find_view_by_id() {
viewPager = (VerticalViewPager) findViewById(R.id.pager);
}
private void fetchVideo() {
.
.
..adapter = new PagerAdapter(getSupportFragmentManager(),video_detail);
viewPager.setAdapter(adapter);
}
我的适配器类 - >
public class ViewPagerAdapter extends PagerAdapter implements YouTubePlayer.OnInitializedListener {
// Widgets
private YouTubePlayerView youTubeView;
private TextView txttitle, txtexplanation;
// Variables
public static final String API_KEY = "xxx";
public static String VIDEO_ID = "";
private ArrayList<String> mTitle;
private ArrayList<String> mExplanation;
private ArrayList<String> mArrayvid;
// Others
Context mContext;
LayoutInflater inflater;
public ViewPagerAdapter( Context context, ArrayList<String> title, ArrayList<String> explanation, ArrayList<String> arrayvid) {
this.mContext = context;
this.mTitle = title; // title
this.mExplanation = explanation; // description
this.mArrayvid = arrayvid; //video link
}
@Override
public int getCount() {
return mArrayvid.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container, false);
txttitle = (TextView) itemView.findViewById(R.id.txt_title);
txtexplanation = (TextView) itemView.findViewById(R.id.txt_desc);
youTubeView = (YouTubePlayerView) itemView.findViewById(R.id.youtube_player);
txttitle.setText(mTitle.get(position));
txtexplanation.setText(mExplanation.get(position));
String[] videoId = mArrayvid.get(position).split("/");
VIDEO_ID = videoId[videoId.length - 1];
Log.e("videoid", mArrayvid.get(position));
youTubeView.initialize(VIDEO_ID, this);
(container).addView(itemView);
return itemView;
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
if (youTubePlayer != null) {
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
youTubePlayer.loadVideo(VIDEO_ID);
youTubePlayer.play();
if (!wasRestored) {
youTubePlayer.cueVideo(VIDEO_ID);
}
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Toast.makeText(mContext, "Failed to initialize.", Toast.LENGTH_LONG).show();
Log.e("Init Error: ", youTubeInitializationResult.toString());
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((LinearLayout) object);
}
}
我的xml布局 - - &gt;
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
和主要xml布局 - &gt;&gt;
<view
android:id="@+id/pager"
class="com.example.inbridge08.videoswap.VerticalViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8.6" />
并且plz建议像这样的 lib或github项目。谢谢