gpt4 book ai didi

android - Fragment 内的 VideoView,从未调用过 onPreparedListener (android)

转载 作者:行者123 更新时间:2023-11-29 02:40:53 27 4
gpt4 key购买 nike

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

fragment 代码:

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

fragment 布局:

<?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>

使用 fragment 的主要 Activity :

public class PruebaVideoMain extends Activity {

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

主要 Activity 布局:

<?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>

最佳答案

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

android:layout_width="0dp"

对于

android:layout_width="match_parent"

关于android - Fragment 内的 VideoView,从未调用过 onPreparedListener (android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44230195/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com