片段内的VideoView,从未调用过onPreparedListener(android)

Posted

技术标签:

【中文标题】片段内的VideoView,从未调用过onPreparedListener(android)【英文标题】:VideoView inside Fragment, onPreparedListener never called (android) 【发布时间】:2017-05-28 17:39:07 【问题描述】:

我正在使用 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_
   android:layout_
   android:layout_gravity="center">
<VideoView
    android:id="@+id/video_view"
    android:layout_
    android:layout_
    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_
 android:layout_
 android:layout_gravity="center" >

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

</RelativeLayout> 

【问题讨论】:

请将 class="example.com.probando.videoPlayer.VideoFragment" 改为 android:name="example.com.probando.videoPlayer.VideoFragment" @Sunhee 我刚试过,没用。感谢您的建议 【参考方案1】:

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

android:layout_

android:layout_

【讨论】:

以上是关于片段内的VideoView,从未调用过onPreparedListener(android)的主要内容,如果未能解决你的问题,请参考以下文章

Fragment 中的 onActivityResult 从未调用过

PFLoginViewController didLogin 在使用 Facebook 登录时从未调用过

onActivityResult 从未在我的嵌套片段中调用

刷新片段内的视图

如何让 videoView 在片段内播放?

Android:片段内的按钮操作