添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 :

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 ?

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 something – varun bhardwaj Feb 18, 2015 at 8:47 For anyone who hears the sound but sees black screen: just make sure you add mediaplayer.setDisplay(holder); in surfaceCreated method. – Ehsan Jan 30, 2021 at 10:28

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();

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.

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>
@Override
            public void onPrepared(final MediaPlayer mp) {
                Toast.makeText(getApplicationContext(),"onPrepared",ToasLENGTH_SHORT).show();
                mVideoView.setZOrderOnTop(false);
                mVideoView.setBackgroundColor(Color.TRANSPARENT);

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

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>

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);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.