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 ?

SilentKiller
  • 6,944
  • 6
  • 40
  • 75
mahdi yamani
  • 923
  • 3
  • 12
  • 29

9 Answers9

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();
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
5

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.

Roger Alien
  • 3,040
  • 1
  • 36
  • 46
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>
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);
            }
zzzmode
  • 169
  • 4
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

faljbour
  • 1,367
  • 2
  • 8
  • 15
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

Community
  • 1
  • 1
TrueCH
  • 501
  • 1
  • 6
  • 18
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>
Vinil Chandran
  • 1,563
  • 1
  • 19
  • 33
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);
    }
}
0

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

Crompy
  • 645
  • 1
  • 5
  • 11