Android视频里面有一个framgent吗?

时间:2015-11-22 12:09:39

标签: android android-fragments

您好我在从网址播放视频时遇到问题。我正在使用片段,每当我运行我的代码时,我都会收到此错误:

  

错误:(32,34)错误:没有为MediaController找到合适的构造函数(MainFragment)   构造函数MediaController.MediaController(Context)不适用   (实际参数MainFragment无法通过方法调用转换转换为Context)   构造函数MediaController.MediaController(Context,boolean)不适用   (实际和正式的参数列表长度不同)   构造函数MediaController.MediaController(Context,AttributeSet)不适用   (实际和正式的参数列表长度不同)

这是我的代码:

public class MainFragment extends Fragment {

private static final String Video_url = "https://dl.dropboxusercontent.com/u/102136471/20151113_223245.mp4";
public MainFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
   VideoView view = (VideoView) getView().findViewById(R.id.videoView);
    Uri video = Uri.parse(Video_url);

    view.setMediaController( new MediaController(this));
    view.setVideoURI(video);
    view.start();
    view.requestFocus();
    return inflater.inflate(R.layout.fragment_main, container, false);

}

我不确定这是否是在片段内打开视频视图的正确方法?

1 个答案:

答案 0 :(得分:2)

view.setMediaController(new MediaController(getActivity()));

使用getActivity()

访问片段中的上下文

校正:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_main, container, false);

// Inflate the layout for this fragment
VideoView view = (VideoView) v.findViewById(R.id.videoView);
Uri video = Uri.parse(Video_url);

view.setMediaController(new MediaController(getActivity()));
view.setVideoURI(video);
view.start();
view.requestFocus();

return v;

 }