Android VideoView black screen
I got the same problem and i found a solution. Its a little bit hacky but it do the trick.
So basically you need to put your VideoView into a FrameLayout.
Over the videoview you need to add another FrameLayout with the background of your video and when your video is loaded and ready to play you hide the placeholder.
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginTop="50dip" >
<VideoView
android:id="@+id/geoloc_anim"
android:layout_width="fill_parent"
android:layout_height="172dip" android:layout_gravity="top|center" android:visibility="visible"/>
<FrameLayout
android:id="@+id/placeholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/fondvert_anim">
</FrameLayout>
In your activity you need to implements OnPreparedListener and add this
//Called when the video is ready to play
public void onPrepared(MediaPlayer mp) {
View placeholder = (View) findViewById(R.id.placeholder);
placeholder.setVisibility(View.GONE);
}
So when the video is ready we hide our placeholder and that trick avoid the black flicker screen.
Hope this help someone.
Android Videoview constantly showing black screen and no audio
add this method instead of your code
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_settings_general,
container, false);
intro = (VideoView) rootView.findViewById(R.id.video_view);
MediaController mediaController= new MediaController(getContext());
intro.setMediaController(mediaController);
Uri uri = Uri.parse("android.resource://" + getContext().getPackageName() + "/" + R.raw.video);
intro.setVideoURI(uri);
intro.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
intro.setZOrderOnTop(true);
intro.start();
}
});
return rootView ;
}
Black screen when I return to video playback activity in Android
Like mentioned in the comments section - You should make use of onPause() and onResume(). In onPause you should stop media player and in onResume you should start it again, as shown below:
@Override
public void onResume(){
super.onResume();
if(videoview != null){
videoview.start();
}
}
You need to change this (inside onCreate
):
VideoView videoview = (VideoView) findViewById(R.id.videoview);
to this:
videoview = (VideoView) findViewById(R.id.videoview);
Otherwise you will not be able to get reference to the videoview outside of onCreate
.
Edit:
private VideoView videoview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starter);
videoview = (VideoView) findViewById(R.id.videoview);
//Your code......
}
//It should be underneath onCreate
@Override
public void onResume(){
super.onResume();
if(videoview != null){
videoview.start();
}
}
VideoView is not working and black screen
Try this way:
videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
videoview.stopPlayback();
videoview.setVideoURI(uri);
videoview.start();
}
});
videoview.setVideoURI(uri);
videoview.start();
You can also set setOnErrorListener
to check for the error:
videoview.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
//check error here
return false;
}
});
Back Button and Black Screen Video
You might want to start the video on onResume() of Activity, instead you having started on onCreate().
There are other things that you have to handle if you start on onResume. But as far I can see from your code, starting a video on Create and moving to other Activity (without finishing) and come back to activity again will call onResume().
Related Topics
Flutter Does Not Find Android Sdk
Android: How to Share Image with Text on Facebook via Intent
Edittext Seterror() with Icon But Without Popup Message
Drawable-Hdpi, Drawable-Mdpi, Drawable-Ldpi Android
Android Webview Inside Scrollview Scrolls Only Scrollview
Why Are These Permissions Being Refused
Close Application and Remove from Recent Apps/
Android: Overlay on Android Camera Preview
Setresult Does Not Work When Back Button Pressed
Android Studio - Creating Modules Without Copying Files
How to Change Bitmap Image Color in Android
Android:Passing a Hash Map Between Activities
How to Animate a Path on an Android Canvas
How to Detect When the User Has Changed the Clock Time on Their Device
How to Automatically Restart a Service Even If User Force Close It