I have a fragment that allows users to select from a mixed list of video and image content. When selecting a multimedia item, display is toggled between an ImageView
and VideoView
depending on the media type. The problem is, when the user selects a video, only the audio is played, and the VideoView
remains black.
This is the code for displaying video: (Most of the code at the bottom is trial-and-error to see if something is missing to fix this issue)
mainVideo.setVisibility(View.VISIBLE);
mainImage.setVisibility(View.INVISIBLE);
getActivity().getWindow().setFormat(PixelFormat.TRANSLUCENT);
parent.showLoader(R.string.buffering);
mainVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
parent.hideLoader();
mainVideo.requestFocus();
mainVideo.start();
}
});
mainVideo.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
showErrorMessage(R.string.multimedia_load_failed, R.string.please_try_again_later);
parent.hideLoader();
return true;
}
});
Uri video = Uri.parse(mm.url);
mainVideo.setMediaController(new MediaController(parent));
mainVideo.setKeepScreenOn(true);
mainVideo.bringToFront();
mainVideo.setDrawingCacheEnabled(true);
mainVideo.setActivated(true);
mainVideo.setEnabled(true);
mainVideo.setVideoURI(video);
Log.i(TAG, "Loading video: " + video.toString());
(parent
is a reference to the activity that has some convenience functions like showLoader
and whatnot.)
Here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="275dp"
android:layout_height="275dp"
android:layout_gravity="center_horizontal"
android:padding="1dp"
android:background="@color/light_grey">
<ImageView
android:id="@+id/mm_main_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black"
android:scaleType="fitXY"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:id="@+id/mm_main_video"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
</VideoView>
</RelativeLayout>
</FrameLayout>
<TextView
android:id="@+id/mm_index_count"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_margin="@dimen/large_padding"
android:layout_gravity="center_horizontal"
android:textColor="@color/white"/>
<HorizontalScrollView
android:id="@+id/mm_horizontal_scroll"
android:layout_height="110dp"
android:layout_width="wrap_content">
<LinearLayout
android:id="@+id/mm_media_list"
android:layout_height="match_parent"
android:layout_width="wrap_content">
<!-- Content filled dynamically -->
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
I have looked all over StackOverflow and around the web for the last two days, but cannot find a solution to this problem anywhere. Any help is appreciated.
All of the videos (and images for that matter) are remote, they are not on the Android device.
Try to remove any background color defined in the xml. It might cover the video view.