I use View Pager with a snippet to display the image and video, I can display the image and video correctly, but I have a problem when I sit down for the video, then the video plays, but I scroll through the next or previous, then the videos are still playing on the next or the previous screen, but when I move two slides side by side or earlier, the video stops, but why not on the next or previous slide.
I am looking for him more, but I had no solution, any help would be noticeable. Thanks in advance.
Here is my code:
This is a fragment class.
public class ContentFragment extends Fragment { private final String imageResourceId; private String type; public ContentFragment(String imageResourceId,String type) { System.out.println("Path In cons="+imageResourceId+"and type is="+type); this.imageResourceId = imageResourceId; this.type= type; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e("Test", "hello"); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.content_layout, container, false); TouchImageView imageView = (TouchImageView) view.findViewById(R.id.touchImage); imageView.setImageResource(R.id.touchImage); imageView.setMaxZoom(10f); VideoView videoView =(VideoView) view.findViewById(R.id.videoView1); if(type.equals("image")) { imageView.invalidate(); imageView.setVisibility(View.VISIBLE); videoView.setVisibility(View.GONE); try { System.out.println("IN Content Fragment"+imageResourceId.toString()); Bitmap bmp = BitmapFactory.decodeFile(imageResourceId.toString()); imageView.setImageBitmap(bmp); } catch(Exception e) { System.out.println("Error Of image File"+e); } } else try { if(type.equals("video")){ videoView.invalidate(); videoView.setVisibility(View.VISIBLE); imageView.setVisibility(View.GONE); String path = imageResourceId.toString(); videoView.setVideoURI(Uri.parse(path)); videoView.setMediaController(new MediaController(getActivity())); videoView.setFocusable(true); videoView.start(); } } catch(Exception e) { e.printStackTrace(); } return view; } }
This is the activity of the pager adapter
public class MediaActivity extends FragmentActivity { private MyAdapter mAdapter; private ViewPager mPager; public ArrayList<Content> contentList; Context context; LinearLayout numberOfPageLayout; SharedPreferences sharedPreferences; Handler progressHandler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_media); context=(Context) getApplicationContext(); mPager = (ViewPager) findViewById(R.id.pager); progressHandler = new Handler(); contentList=new ArrayList<Content>(); new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) {
android android-viewpager videoview fragment imageview
Ankit
source share