J'essaie de développer une application qui récupère des vidéos du serveur et joue sur une vue vidéo à l'intérieur de Viewpager, la vidéo du dossier raw fonctionne bien, mais il y a 2 problèmes:
alors comment utiliser l'URL au lieu de `
Android.resource://mypackagename/video1.mp4
et comment faire une pause/arrêter quand PageIsChanged. tout tutoriel ou des hits, je serai reconnaissant pour cela.
Ici mon code est Code source
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>
Je pense qu'il est préférable d'utiliser FragmentStatePagerAdapter
au lieu de PagerAdapter
. Dans ce scénario, vous n'avez plus besoin de OnPageChangeListener
et vous pouvez utiliser des méthodes de rappel de cycle de vie de fragment telles que onResume
et onPause
pour lire ou mettre en pause votre vidéo.
et comment faire une pause/arrêter quand PageIsChanged. un tutoriel ou des hits i sera reconnaissant pour cela.
Vous pouvez utiliser les liens ci-dessous pour comprendre comment utiliser cet adaptateur. Vous pouvez ensuite créer votre propre logique à l'aide de la méthode du cycle de vie des fragments.
J'ai écrit FrameViedoView qui améliore les performances de lecture de vidéos.
Cela fonctionne aussi pour ViewPager
.
Comment l'utiliser?
Ajoutez http://bright.github.io/maven-repo/
à vos référentiels:
repositories {
maven {
url "http://bright.github.io/maven-repo/"
}
}
puis déclarez une dépendance à l'intérieur d'un module:
dependencies {
compile('mateuszklimek.framevideoview:framevideoview:1.1.0@aar')
//other dependencies
}
Dans exemples vous pouvez trouver comment l'utiliser dans ViewPager
ou en simple Activity
.
Lisez mon blog post about FrameVideoView
.
Pour détecter si ViewPager
est scrolling/changer de page, il vous suffit de joindre un OnPageChangeListener
à votre ViewPager
.
Pour lire une vidéo à partir d'une URL distante, vous pouvez utiliser la variable MediaController
/MediaPlayer
standard. Jetez un oeil à ce tutoria l.
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) {
}
});
Vous devrez accéder à votre vidéo depuis votre OnPageChangeListener . Pour le faire, vous devez:
view.setTag("view"+position);
dans votre public Object instantiateItem(ViewGroup container, int position)
Et après que vous puissiez le récupérer à partir de OnPageChangeListener:
View viewTag = viewPager.findViewWithTag("view" + viewPager.getCurrentItem());
et alors
VideoView videoViewTag = (VideoView)viewTag.findViewById(R.id.videoView);