我正在开发Android应用程序,它将读取mp4视频路径的数组,并在片段中填充每个视频路径并使用VideoView播放它们。
我的代码完美适用于第一个片段但不适用于其他片段而且我一直在拍摄"无法播放此视频"消息。
这是我的代码。
活性
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();
}
}
片段
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;
}
}
我做错了什么?
答案 0 :(得分:2)
每个VideoView都会分配一个新的MediaPlayer,并且文档不鼓励使用其中的多个。
您可以尝试通过调用以下方式手动释放与VideoView关联的MediaPlayer:
videoView.suspend();
不确定如何或何时会打电话,因为您正在使用寻呼机。我有类似的问题,所以我选择创建一个在多个片段之间共享的MediaPlayer。不过,我不得不放弃使用VideoView。