无法使用片段播放多个视频

can't play multiple video using fragment

我正在开发 android 应用程序,它将读取一组 mp4 视频路径并将每个路径填充到片段中并使用 VideoView.

播放它们

我的代码适用于第一个片段,但不适用于其余片段,我不断收到 "Can't play this video" 消息。

这是我的代码。

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);
    }
}

页面适配器

    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();
    }
}

片段

    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;
    }
}

我做错了什么?

每个 VideoView 分配一个新的 MediaPlayer 并且文档不鼓励拥有多个。

您可以尝试手动释放与 VideoView 关联的 MediaPlayer,方法是调用:

videoView.suspend();

不确定如何或何时调用它,因为您使用的是寻呼机。我遇到了类似的问题,所以我选择创建一个在多个片段之间共享的 MediaPlayer。但是,我不得不放弃使用 VideoView。