在Fragment里面的VideoView,onPreparedListener永远不会调用(android)

时间:2017-05-28 17:39:07

标签: android android-fragments android-videoview

我正在使用VideoView播放视频,起初我把它放在一个活动中并且工作得很好,但是因为我试图将功能放在API中,所以我必须把视频放在片段中。我这样做并编写了一个使用该片段的Activity,但似乎我在VideoView上设置的OnPreparedListener从未被调用过(我看到了这个调试,因为我的进度对话框永远不会从屏幕上消失)。我想弄明白为什么。这是我的代码:

片段代码:

public class VideoFragment extends Fragment {

private VideoView videoView;
private int position = 0;
private MediaController mediaController;
private ProgressDialog progressDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    String uriPath = "android.resource://" + getActivity().getPackageName()
            + "/" + R.raw.vid_ej;
    Uri uri = Uri.parse(uriPath);

    View rootView = inflater.inflate(R.layout.video_fragment, container, false);

    if(mediaController == null){
        mediaController = new MediaController(getActivity());
    }

    videoView = (VideoView) rootView.findViewById(R.id.video_view);

    progressDialog = new ProgressDialog(getActivity());
    progressDialog.setTitle("Progreso");
    progressDialog.setMessage("Cargando video...");
    progressDialog.setCancelable(false);
    progressDialog.show();

    videoView.setMediaController(mediaController);

    mediaController.show();

    try {
        videoView.setVideoURI(uri);
    }
    catch (Exception e) {
        Log.e("msg", e.getMessage());
        e.printStackTrace();
    }
    videoView.requestFocus();

    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            progressDialog.dismiss();

            mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                @Override
                public void onSeekComplete(MediaPlayer mp) {
                    videoView.pause();
                    //mp.start();

                }
            });

            videoView.seekTo(position);
            if (position == 0) {
                videoView.start();
            } else {
                videoView.pause();
            }
            /*
            if (position > 0) {
                Log.e("msg", "Position>0: " + position);
                videoView.pause();
                //mediaPlayer.seekTo(position);
                Log.e("msg", "currentposition: " + videoView.getCurrentPosition());
            }
            else {
                //mediaPlayer.start();
            }*/
        }
    });
    return rootView;

}


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    position=videoView.getCurrentPosition();
    outState.putInt("Position", position);
    videoView.pause();
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);

    if(savedInstanceState != null) {
        position = savedInstanceState.getInt("Position");
        videoView.seekTo(position);
    }
}

片段布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_gravity="center">
<VideoView
    android:id="@+id/video_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true">
</VideoView>

</RelativeLayout>

使用片段的主要活动:

public class PruebaVideoMain extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.prueba_video_main);
 }
}

主要活动布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_gravity="center" >

<fragment
    android:id="@+id/videoFragment"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="match_parent"
    class="example.com.probando.videoPlayer.VideoFragment"
    tools:layout="@layout/video_fragment"/>

</RelativeLayout> 

1 个答案:

答案 0 :(得分:0)

我通过更改主布局中的这一行解决了这个问题:

android:layout_width="0dp"

android:layout_width="match_parent"