android-black screen on displaying video by using VideoView
Asked Answered
C

9

5

this is my layout :

<LinearLayout 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:orientation="vertical" >

    <FrameLayout
        android:id="@+id/frameLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center" >

        <VideoView
            android:id="@+id/geoloc_anim"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="top|center"
            android:visibility="visible" />

        <FrameLayout
            android:id="@+id/placeholder"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </FrameLayout>
    </FrameLayout>

</LinearLayout>

this is my activity code:

public class MainActivity extends ActionBarActivity implements OnPreparedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        VideoView mVideoView = (VideoView) findViewById(R.id.videoview);
        Uri uri = Uri.parse("android.resource://" + getPackageName()+"/raw/lst2");
        mVideoView.setVideoURI(uri);
        mVideoView.requestFocus();
        mVideoView.setZOrderOnTop(true); 
        mVideoView.start();

    }

     @Override
      public void onPrepared(MediaPlayer mp) {
        mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
          @Override
          public boolean onInfo(MediaPlayer mp, int what, int extra) {
              View placeholder = (View) findViewById(R.id.placeholder);
            if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)  {
              // video started; hide the placeholder.
              placeholder.setVisibility(View.GONE);
              return true;
            }
            return false;
          }
        });
     }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
    }

    public void surfaceCreated(SurfaceHolder holder) {
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
    }

}

It works fine on android 4.2 but it doesn't work properly on android 2.3 . on android 2.3 , the first time it opens it works find but when close the app and open it again ,a black screen comes , something like this : enter image description here

and after one minute or so , it goes from a black screen to a white screen but still nothing plays .

Could you help me to solve this problem ?

Colemancolemanite answered 18/2, 2015 at 6:54 Comment(5)
is it going in onPrepared ?Baldpate
@varunbhardwaj thanks for reply ,I logged and it doesn't go to onPreparedColemancolemanite
perfect!! now you can check the cause as the video is not getting prepared so there might be some issue with the video format or somethingBaldpate
please refer to #9766129Audriaaudrie
For anyone who hears the sound but sees black screen: just make sure you add mediaplayer.setDisplay(holder); in surfaceCreated method.Amatol
K
16

Very late answer. But surely it helps anyone.

set videoView.setZOrderOnTop(true) before start().

https://developer.android.com/reference/android/view/SurfaceView.html#setZOrderOnTop(boolean)

    videoView.setVideoURI(Uri.parse(uriString));
    videoView.setZOrderOnTop(true);//this line solve the problem
    videoView.start();
Kinard answered 11/7, 2016 at 9:13 Comment(2)
Now videoView.setZOrderOnTop(true); giving white screen.Gabe
It does not work if you wana display views on top of video.Nutritive
D
6

I resolved this with toggling alpha for VideoView:

public class VideoPlayer extends VideoView {

    ....
public VideoPlayer(Context context) {
    super(context);
    init();
}

public void init() {
    setAlpha(0); // hides view 
    setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                @Override
                public boolean onInfo(MediaPlayer mp, int what, int extra) {
                    if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                        setAlpha(1); // shows view
                        return true;
                    }
                    return false;
                }
            });
        }
    });

I guess you can do the same if you have external VideoView and have access to it.

Deprave answered 5/9, 2017 at 19:45 Comment(0)
P
3

This code works for me, code tested on android nougat. No require additional code in your java file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_gravity="center"
    tools:context=".SecondaryDisplay"
    android:background="@color/black">

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>
Parenteral answered 27/3, 2020 at 22:40 Comment(0)
L
2

you need in onPrepared change the Window content! like show toask or other,e.g

@Override
            public void onPrepared(final MediaPlayer mp) {
                Toast.makeText(getApplicationContext(),"onPrepared",ToasLENGTH_SHORT).show();
                mVideoView.setZOrderOnTop(false);
                mVideoView.setBackgroundColor(Color.TRANSPARENT);
            }
Lermontov answered 27/11, 2015 at 10:32 Comment(0)
M
1

remove the mVideoView.start() and change your onPrepared to start your video,

@Override
      public void onPrepared(MediaPlayer mp) {
          mp.start();

..............

I do not have a device with android less than 4.2 to test with, but this is how I start videos with my application

Maros answered 18/2, 2015 at 7:15 Comment(1)
it works fine on android 4+ but it has the same problem with android less than 3Colemancolemanite
T
0

Maybe to late, but I found a solution that worked for me. I combined answers from Sagar Limbani and faljbour

videoView.setVideoURI(Uri.parse(path));
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(final MediaPlayer mp) {
        mp.start();
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                if(mp.getCurrentPosition() != 0){
                    View placeholder = findViewById(R.id.placeholder);
                    placeholder.setVisibility(View.GONE);
                } else {
                    new Handler().postDelayed(this, 50);
                }
            }
        });
    }
});

It worked for android 5.1, 5.0 and 4.0

Terrazas answered 26/5, 2015 at 13:35 Comment(0)
W
0

I had the same issue. I found that the main reason for that was the use of FrameLayout as the parent layout. Use RelativeLayout as the parent layout of the VideoView

<LinearLayout 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:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/frameLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center" >

        <VideoView
            android:id="@+id/geoloc_anim"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="top|center"
            android:visibility="visible" />

        <FrameLayout
            android:id="@+id/placeholder"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </FrameLayout>
    </RelativeLayout>

</LinearLayout>
Westberry answered 27/7, 2017 at 7:15 Comment(0)
P
0

You can solve this by using custom VideoPlayer extends VideoView

    public class VideoPlayer extends VideoView {

public VideoPlayer(Context context) {
    super(context);
    init();
}

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        TyrooLog.i(TAG, "onMeasure");
        int width = getDefaultSize(videoWidth, widthMeasureSpec);
        int height = getDefaultSize(videoHeight, heightMeasureSpec);
        if (videoWidth > 0 && videoHeight > 0) {
            if (videoWidth * height > width * videoHeight) {
                TyrooLog.i(TAG, "video too tall, correcting");
                height = width * videoHeight / videoWidth;
            } else if (videoWidth * height < width * videoHeight) {
                TyrooLog.i(TAG, "video too wide, correcting");
                width = height * videoWidth / videoHeight;
            } else {
                TyrooLog.i(TAG, "aspect ratio is correct: " + width+"/"+height+"="+mVideoWidth+"/"+mVideoHeight);
            }
        }
        TyrooLog.i(TAG, "setting size: " + width + 'x' + height);
        setMeasuredDimension(width, height);
    }
}
Psychotomimetic answered 27/2, 2018 at 11:13 Comment(0)
C
0

I faced a similar issue and found that adding mp.seekTo(1); in the onPrepared(MediaPlayer mp) method fixed it.

Canica answered 30/7, 2019 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.