i'm developing android app that will read an array of mp4 video path and populate each one in a fragment and play them using VideoView.
my code works perfectly for the first fragment but not for the rest and i keep getting "Can't play this video" message.
this is my code.
activity
public class SwipeActivity extends FragmentActivity {
private ViewPager mPager;
private MyPageAdapter mPagerAdapter;
static String json;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
json = "[{\"path\":\"storage\\/emulated\\/0\\/MyApp\\/VID_1.mp4\"},{\"path\":\"storage\\/emulated\\/0\\/MyApp\\/VID_2.mp4\"}]";
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new MyPageAdapter(getFragmentManager(), json);
mPager.setAdapter(mPagerAdapter);
}
}
PageAdapter
public class MyPageAdapter extends FragmentStatePagerAdapter {
JSONArray jsonArray;
FragmentManager fragmentManager;
Context mContext;
public MyPageAdapter(FragmentManager fm, String json) {
super(fm);
fragmentManager = fm;
try {
jsonArray = new JSONArray(json);
} catch (JSONException e) {
}
}
@Override
public Fragment getItem(int i) {
Fragment fragment = null;
JSONObject obj = null;
String path = "";
try {
obj = jsonArray.getJSONObject(i);
path = obj.getString("path");
fragment= VideoFragment.create(i, path);
} catch (JSONException e) {
Log.e(TAG, "JSONException: ", e);
}
return fragment;
}
@Override
public int getCount() {
return jsonArray.length();
}
}
fragment
public class VideoFragment extends Fragment {
public static final String ARG_PAGE = "page";
private int mPageNumber;
String path;
VideoView videoView;
ImageView play;
public static VideoFragment create(int pageNumber, String mpath) {
VideoFragment fragment = new VideoFragment(mpath);
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public VideoFragment(String mpath) {
path = mpath;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout containing a title and body text.
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_video, container, false);
File file = new File(path);
if(file.exists()) {
path = file.getAbsolutePath();
Log.d(TAG, "exists: " + path);
} else {
Log.d(TAG, "dont exists");
}
android.view.Display display = getActivity().getWindowManager().getDefaultDisplay();
android.graphics.Point size = new android.graphics.Point();
display.getSize(size);
play = (ImageView) rootView.findViewById(R.id.play);
videoView=(VideoView)rootView.findViewById(R.id.video);
videoView.setVideoPath(path);
videoView.requestFocus();
videoView.seekTo(10);
play.setColorFilter(android.graphics.Color.WHITE);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: ");
//play.setVisibility(View.GONE);
videoView.start();
}
});
return rootView;
}
public int getPageNumber() {
return mPageNumber;
}
}
what am i doing wrong?
Each VideoView allocates a new MediaPlayer and having multiple of them is discouraged by the documentation.
You can try and manually release the MediaPlayer associated with a VideoView by calling:
Not sure how or when you will call it though, since you're using a Pager. I've had a similar problem so I chose to create a MediaPlayer that's shared between multiple fragments. I had to drop the use of the VideoView, however.