如何在ViewPager中从服务器上的VideoView播放video

我试图开发一个应用程序, 从服务器检索video,并在ViewPager内videoview播放,从原始文件夹的video工作正常,但他们有两个问题:

  • 1:某些video不播放。 或黑色活动表演。
  • 2:页面滚动时video不停止。

所以如何使用URL而不是`

android.resource://mypackagename/video1.mp4 

以及如何在PageIsChanged时暂停/停止。 任何教程或任何命中,我将感谢这一点。

这里我的代码是源代码

ViewPagerAdapter.java

 import android.content.Context; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.net.Uri; 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.ImageView; import android.widget.LinearLayout; import android.widget.MediaController; import android.widget.TextView; import android.widget.VideoView; public class ViewPagerAdapter extends PagerAdapter { Context context; String[] rank; String[] country; String[] population; int[] flag; LayoutInflater inflater; static int[] arrayvid; private VideoView mVideoView; public ViewPagerAdapter(Context context, String[] rank, String[] country, String[] population, int[] flag, int[] arrayvid) { this.context = context; this.rank = rank; this.country = country; this.population = population; this.flag = flag; this.arrayvid = arrayvid; } @Override public int getCount() { return rank.length; } @Override public boolean isViewFromObject(View view, Object object) { return view == ((LinearLayout) object); } @Override public Object instantiateItem(ViewGroup container, final int position) { // Declare Variables TextView txtrank; TextView txtcountry; TextView txtpopulation; ImageView imgflag; inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View itemView = inflater.inflate(R.layout.viewpager_item, container, false); // Locate the TextViews in viewpager_item.xml txtrank = (TextView) itemView.findViewById(R.id.rank); txtcountry = (TextView) itemView.findViewById(R.id.country); txtpopulation = (TextView) itemView.findViewById(R.id.population); imgflag = (ImageView) itemView.findViewById(R.id.flag); mVideoView = (VideoView) itemView.findViewById(R.id.VVExe); txtrank.setText(rank[position]); txtcountry.setText(country[position]); txtpopulation.setText(population[position]); imgflag.setImageResource(flag[position]); mVideoView.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.setLooping(true); } }); MediaController mediaController = new MediaController(context, false); mediaController.setAnchorView(mVideoView); mVideoView.setMediaController(mediaController); ((ViewPager) container).addView(itemView); return itemView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // Remove viewpager_item.xml from ViewPager ((ViewPager) container).removeView((LinearLayout) object); } public void pausevideo() { mVideoView.stopPlayback(); } public void play(int position) { mVideoView.setVideoURI(Uri .parse("android.resource://mypackagename/" + arrayvid[position])); mVideoView.requestFocus(); mVideoView.start(); } } 

MainActivity.java

 import android.app.Activity; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.widget.ImageButton; public class MainActivity extends Activity { // Declare Variables ViewPager viewPager; PagerAdapter adapter; String[] rank; private ImageButton play; String[] country; String[] population; int[] flag; public int position; int[] arrayvid; boolean isRunning = true; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the view from viewpager_main.xml setContentView(R.layout.viewpager_main); play = (ImageButton) findViewById(R.id.btnPlayAB); // Generate sample data rank = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; country = new String[] { "China", "India", "United States", "Indonesia", "Brazil", "Pakistan", "Nigeria", "Bangladesh", "Russia", "Japan" }; population = new String[] { "1,354,040,000", "1,210,193,422", "315,761,000", "237,641,326", "193,946,886", "182,912,000", "170,901,000", "152,518,015", "143,369,806", "127,360,000" }; flag = new int[] { R.drawable.china, R.drawable.india, R.drawable.unitedstates, R.drawable.indonesia, R.drawable.brazil, R.drawable.pakistan, R.drawable.nigeria, R.drawable.bangladesh, R.drawable.russia, R.drawable.japan }; arrayvid = new int[] { R.raw.basiccrunch, R.raw.bicyclecrunch, R.raw.reversecrunch, R.raw.longarmcrunch, R.raw.crossovercrunch, R.raw.rightobliquecrunch, R.raw.leftobliquecrunch, R.raw.halfcurl, R.raw.verticallegcrunch, R.raw.plank }; // Locate the ViewPager in viewpager_main.xml viewPager = (ViewPager) findViewById(R.id.pager); // Pass results to ViewPagerAdapter Class adapter = new ViewPagerAdapter(MainActivity.this, rank, country, population, flag, arrayvid); // Binds the Adapter to the ViewPager viewPager.setAdapter(adapter); play.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (isRunning) { ((ViewPagerAdapter) adapter).play(position); isRunning = false; play.setBackgroundResource(R.drawable.pausee); } else { ((ViewPagerAdapter) adapter).pausevideo(); isRunning = true; play.setBackgroundResource(R.drawable.playy); } } }); } 

}

viewpager_item.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" android:padding="10dp" android:weightSum="10" > <LinearLayout android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1.6" android:orientation="horizontal" > <LinearLayout android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="3" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/ranklabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ranklabel" /> <TextView android:id="@+id/rank" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/countrylabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/countrylabel" /> <TextView android:id="@+id/country" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/populationlabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/populationlabel" /> <TextView android:id="@+id/population" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> <ImageView android:id="@+id/flag" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1" android:background="#000000" android:padding="1dp" /> </LinearLayout> <VideoView android:id="@+id/VVExe" android:layout_width="wrap_content" android:layout_height="0dip" android:layout_marginTop="5dp" android:layout_weight="7.9" android:gravity="center" /> </LinearLayout> 

viewpager_main.xml

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:weightSum="10" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="wrap_content" android:layout_height="0dip" android:layout_weight="8.6" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1.4" android:gravity="center" android:padding="10dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:background="#696969" android:gravity="center" android:orientation="horizontal" android:weightSum="10" > <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > </LinearLayout> <ImageButton android:id="@+id/btnprevAB" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:background="@drawable/prevsel" /> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > </LinearLayout> <ImageButton android:id="@+id/btnPlayAB" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:background="@drawable/playsel" /> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > </LinearLayout> <ImageButton android:id="@+id/btnNextAB" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:background="@drawable/nextsel" /> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > </LinearLayout> </LinearLayout> </LinearLayout> 

在这里输入图像说明

Solutions Collecting From Web of "如何在ViewPager中从服务器上的VideoView播放video"

我认为最好使用FragmentStatePagerAdapter而不是PagerAdapter 。 在这种情况下,您不再需要OnPageChangeListener并且可以使用片段生命周期callback方法(如onResumeonPause来播放或暂停video。

以及如何在PageIsChanged时暂停/停止。 任何教程或任何命中,我将感谢这一点。

您可以使用下面的链接来了解如何使用此适配器,然后您可以使用片段生命周期方法创build自己的逻辑。

Android FragmentStatePagerAdapter示例

FragmentStatePagerAdapter

我写了FrameViedoView ,提高了播放video的性能。

它也适用于ViewPager

如何使用它?

http://bright.github.io/maven-repo/添加到您的存储库:

 repositories { maven { url "http://bright.github.io/maven-repo/" } } 

然后在模块中声明一个依赖关系:

 dependencies { compile('mateuszklimek.framevideoview:framevideoview:1.1.0@aar') //other dependencies } 

在示例中,您可以find如何在ViewPager或简单的Activity使用它。

阅读关于FrameVideoView 博客文章 。

  1. 要检测ViewPager是否正在滚动/更改页面 ,只需要将OnPageChangeListener附加到ViewPager

  2. 要从远程URL播放video,您可以使用标准的MediaController / MediaPlayer 。 看看这个tutoria升。

  viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { //do the needful action } @Override public void onPageSelected(int position) { System.out.println("selected " + position); //do the needful action } } @Override public void onPageScrollStateChanged(int state) { } }); 

您将不得不从您的OnPageChangeListener访问您的video。 必须这样做,你必须:

view.setTag("view"+position); 在你的public Object instantiateItem(ViewGroup container, int position)

并且,您可以从OnPageChangeListener获取它之后:

 View viewTag = viewPager.findViewWithTag("view" + viewPager.getCurrentItem()); 

接着

 VideoView videoViewTag = (VideoView)viewTag.findViewById(R.id.videoView);