gpt4 book ai didi

java - 如何让 videoView 在 fragment 内播放?

转载 作者:行者123 更新时间:2023-12-01 13:23:49 26 4
gpt4 key购买 nike

我对 fragment 非常陌生,我正在尝试将 videoView 加载到 fragment 中。我的布局文件布局正确(因为我可以在图像之间滑动),但我不确定如何将 videoView 加载到 fragment 中。我的 Activity 代码如下:

package com.example.PLS;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.VideoView;

public class CustomPagerAdapter extends PagerAdapter
{
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0: {
resId = R.layout.page1;
break;
}
case 1: {
resId = R.layout.page2;
break;
}
case 2: {
resId = R.layout.page3;
break;
}
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public int getCount() {
return 3;
}
}

package com.example.PLS;

import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.widget.VideoView;

public class MyActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create and set adapter
CustomPagerAdapter adapter = new CustomPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.customviewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

我不确定在哪里或如何放置告诉 videoView 在 page1 中播放视频的代码。我应该把它放在哪里以及它看起来如何?

最佳答案

有点题外话:您确定您使用的是 fragment 而不仅仅是 View 吗?据我所知,您可以添加和删除 View 。

或者你在布局文件中做了什么?如果可以的话,您能提供xml布局资源吗?

参见can i use view pager with views (not with fragments)

对于您的问题:调用 addView 后,您可以执行以下操作(或将其添加到按钮监听器):

    String videoUri = getVideoUriForPosition(position);
// TODO check if videoUri is not null or something

mPlayerView = (VideoView) mView.findViewById(R.id.video_player);
mPlayerView.requestFocus();
if(mPlayerView == null) {
Log.e(TAG, "Video view null");
} else {
mPlayerView.setVideoURI(Uri.parse(videoUri));
mPlayerView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

public void onPrepared(MediaPlayer mp) {
mPlayerView.start();
});
}

关于java - 如何让 videoView 在 fragment 内播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21894007/

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