当前位置: 动力学知识库 > 问答 > 编程问答 >

Android VideoView on a fragment not working on first view, works after orientation change to landscape

问题描述:

I am veteran developer but a beginner at Android development. I am stuck trying to get a videoview on my Android app to show a video from the web. All I see is the progressDialog loading screen with a white background when I start the fragment. The video will start playing immediately after I change orientation of the device from portrait to landscape which would suggest I simply have a layout setting wrong. But when I initially start in landscape the video does not play either. It will play if I switch to portrait, and then back to landscape. It is a very odd behavior.

I did try to research this issue only to find that video playing on Android devices seems to be a major pain point for most Android developers. Even the google developer API documentation seems incomplete regarding this topic. Below is my code, maybe someone can see what I am missing.

VideoFragment.java

@Override

public void onActivityCreated(Bundle savedInstanceState)

{

super.onActivityCreated(savedInstanceState);

video = (VideoView) getView().findViewById(R.id.standardVideoView);

getActivity().getWindow().setFormat(PixelFormat.TRANSLUCENT);

MediaController mediaController = new MediaController(getActivity());

mediaController.setMediaPlayer(video);

video.setMediaController(mediaController);

video.setVideoURI(Uri.parse("http://<PRIVATE DOMAIN>.com/KLFY_20130806112335457AB.mp4"));

video.requestFocus();

video.start(); // this is called successfully every time the fragment is created

progressDialog = ProgressDialog.show(getActivity(), "", "Buffering video...", true);

progressDialog.setCancelable(true);

video.setOnPreparedListener(new MediaPlayer.OnPreparedListener()

{

// this event is only raised after the device changes to landscape from portrait

public void onPrepared(MediaPlayer mp)

{

Log.i("DEBUGGING", "VIDEO PREPARED");

progressDialog.dismiss();

}

});

}

video_fragment.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/videoViewLayout"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center"

android:background="#000000">

<VideoView

android:id="@+id/standardVideoView"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_gravity="center" />

</LinearLayout>

Thanks for the help in advance!

分享给朋友:
您可能感兴趣的文章:
随机阅读: