Android VideoView czarny ekran

Szukałem sposobu na pozbycie się paskudnego czarnego ekranu początkowego na VideoView przed uruchomieniem metody start ().

Próbowałem z obrazem tła na widżecie, ale to nie działa zgodnie z oczekiwaniami w ogóle. Próbowałem również umieścić obraz pierwszej klatki w filmie na górze VideoView i ukryć go po metodzie stars (). Dodanie onpreparowanego słuchacza, aby rozpocząć wideo, a następnie ukryć obraz. To działa, ale jest straszny migotanie w przejściu i nie wiem, jak się tego pozbyć.


Dzięki za odpowiedź. Dodanie Mediacontrollera nie miało żadnego wpływu. Problem utrzymuje się (nadal widzę czarny migotanie) i nie chcę mieć widocznych kontrolek wideo. Mój kod wygląda tak:

    VideoView vSurface= (VideoView) findViewById(R.id.surfaceView1);
    vSurface.setVideoURI(Uri.parse("android.resource://com.mypackage/" + R.raw.video1));
    vSurface.setVisibility(View.VISIBLE);
    vSurface.setOnPreparedListener(this);
    vSurface.setDrawingCacheEnabled(true);
    vSurface.setOnErrorListener(this);
Author: M.G, 2012-03-19

21 answers

Mam ten sam problem i znalazłem rozwiązanie. Jest trochę zarozumiały, ale robi sztuczkę. Więc zasadniczo musisz umieścić swój film wideo w ramce. W widoku videoview musisz dodać kolejny FrameLayout z tłem filmu, a gdy film jest załadowany i gotowy do odtwarzania, ukrywasz symbol zastępczy.

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

W swojej aktywności musisz zaimplementować OnPreparedListener i dodać to

//Called when the video is ready to play
public void onPrepared(MediaPlayer mp) {

    View placeholder = (View) findViewById(R.id.placeholder);

    placeholder.setVisibility(View.GONE);
}

Więc kiedy film jest gotowy, ukrywamy nasz symbol zastępczy a ta sztuczka uniknie czarnego ekranu migotania.

Mam nadzieję, że to komuś pomoże.
 30
Author: M to the K,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-02 19:01:58

Spotykam ten sam problem i rozwiązuję go za pomocą zaakceptowanego rozwiązania powyżej plus to:

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

Myślę, że onPrepared oznacza po prostu, że wideo jest gotowe do odtwarzania, ale nie oznacza, że wideo zaczęło odtwarzać. Jeśli Ukryj Element Zastępczy w onPrepared, ekran nadal będzie wyświetlany czarny ekran.

Na moim Note3 i Nexusie To rozwiązanie działa dobrze.

 39
Author: wizardlee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-29 07:58:16

Miałem ten sam problem na Galaxy tab 2, Android 4.1.1.

Do videoView.setZOrderOnTop(true); i dalej videoView.start()

Dla mnie działa dobrze.
 34
Author: jc_35,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-30 14:58:11

Miałem ten sam problem i to mi się udało ..

Gdy chcesz pokazać Wideo, zrób videoView.setZOrderOnTop(false); A gdy chcesz ukryć wideo, wystarczy zrobić videoView.setZOrderOnTop (true);

 24
Author: Ravi Bhojani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-12 15:19:31

Mam ten sam problem, który właśnie użyłem videov.setBackgroundColor (Color.Biały), a następnie onprepare użyłem koloru.Przezroczysty) biały jest dla mnie i tak lepszy od czarnego

 18
Author: Ahmad Dwaik 'Warlock',
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-28 17:34:46

Rozszerzając TextureView, nie dostaję czarnych ekranów na początku ani na końcu. Jest to, jeśli chcesz uniknąć używania ZOrderOnTop(true).

public class MyVideoView extends TextureView implements TextureView.SurfaceTextureListener {
  private MediaPlayer mMediaPlayer;
  private Uri mSource;
  private MediaPlayer.OnCompletionListener mCompletionListener;
  private boolean isLooping = false;


  public MyVideoView(Context context) {
      this(context, null, 0);
  }

  public MyVideoView(Context context, AttributeSet attrs) {
      this(context, attrs, 0);
  }

  public MyVideoView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      setSurfaceTextureListener(this);
  }

  public void setSource(Uri source) {
      mSource = source;
  }

  public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) {
      mCompletionListener = listener;
  }

  public void setLooping(boolean looping) {
     isLooping = looping;
  }

  @Override
  protected void onDetachedFromWindow() {
     // release resources on detach
     if (mMediaPlayer != null) {
         mMediaPlayer.release();
         mMediaPlayer = null;
     }
     super.onDetachedFromWindow();
   }

   /*
    * TextureView.SurfaceTextureListener
    */
    @Override
   public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
     Surface surface = new Surface(surfaceTexture);
     try {
         mMediaPlayer = new MediaPlayer();
         mMediaPlayer.setOnCompletionListener(mCompletionListener);
         mMediaPlayer.setOnBufferingUpdateListener(this);
         mMediaPlayer.setOnErrorListener(this);
         mMediaPlayer.setLooping(isLooping);
         mMediaPlayer.setDataSource(getContext(), mSource);
         mMediaPlayer.setSurface(surface);
         mMediaPlayer.prepare();
         mMediaPlayer.start();
     } catch (IllegalArgumentException e) {
         e.printStackTrace();
     } catch (SecurityException e) {
         e.printStackTrace();
     } catch (IllegalStateException e) {
         e.printStackTrace();
         mMediaPlayer.reset();
     } catch (IOException e) {
         e.printStackTrace();
     }
   }

   @Override
   public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}

  @Override
  public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
     surface.release();
     return true;
  }

  @Override
  public void onSurfaceTextureUpdated(SurfaceTexture surface) {}
}
 14
Author: Carl B,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-03 14:29:07

To mi pomogło:

videoView.setBackgroundColor(Color.WHITE); // Your color.
videoView.start();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        videoView.setBackgroundColor(Color.TRANSPARENT);
    }
});
Co najmniej dwa lata później, ale mam nadzieję, że to było pomocne.
 6
Author: emmgfx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-11 07:43:19

To jest zdecydowanie hacky, ale lepsze niż nakładanie obrazu (IMO).

boolean mRestored = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mRestored = savedInstanceState != null;
}

@Override
public void onPrepared(MediaPlayer mp) {

    if (!mRestored) vSurface.seekTo(1);
}

Zakładając, że wkładasz rzeczy do savedInstanceState w onSaveInstanceState.

 4
Author: Paul Burke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-14 23:22:32

Wystarczy użyć VideoView#setBackgroundDrawable(), tak myślę.

  1. Ustawienia początkowe.

    VideoView.setBackgroundDrawable(yourdrawableid);
    
  2. Start video

    VideoView.start();
    VideoView.setBackgroundDrawable(0);
    
 2
Author: Takeaki Kubota,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-07 13:21:24

Po prostu pokaż klatkę z filmu jako podgląd.

vSurface.SeekTo(100);
 2
Author: j7nn7k,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-30 15:25:02

Dla ludzi wciąż szukających odpowiedzi na to, wywołanie VideoView.start() i VideoView.pause() kolejno wewnątrz onPrepared zadziałało dla mnie. Wiem, że może to nie być idealny sposób na osiągnięcie tego, jednak może to być ten z minimalnym obejściem wymaganym w kodzie. Mam nadzieję, że to też zadziała.

@Override
public void onPrepared(MediaPlayer mp) {
    mVideoView.start();
    mVideoView.pause();
}
 2
Author: Gagan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-16 07:02:51

Ten działa dla mnie:

W XML: Videoview ukryj się za względnym układem z białym tłem

    <VideoView
      android:id="@+id/myVideo"
      android:layout_below="@+id/logo_top"
      android:layout_width="200dp"
      android:layout_height="200dp"
      android:layout_centerHorizontal="true" 
    />
    <RelativeLayout
      android:id="@+id/mask"
      android:background="#FFFFFF"
      android:layout_below="@+id/logo_top"
      android:layout_centerHorizontal="true"
      android:layout_width="200dp"  android:layout_height="200dp"
    >
    </RelativeLayout>

I w aktywności: onCreate

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.acceuil);
    myVideo = (VideoView)  findViewById(R.id.myVideo);
    mask = (RelativeLayout)  findViewById(R.id.mask);
    String path = "android.resource://" 
      + getPackageName() + "/" + R.raw.anim_normal;
    myVideo.setVideoURI(Uri.parse(path));
    myVideo.start();
}

OnStart:

 public void onStart() { 
    final long time = System.currentTimeMillis();
    super.onStart();
    new CountDownTimer(5000, 100) { 
    @Override
        public void onTick(long l) {

            long time2 = System.currentTimeMillis();
            if((time2 - time) > 500) {
                mask.setVisibility(View.GONE);
            }
        }

}.start();
Mam nadzieję, że to pomoże.
 1
Author: Ramza,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-20 13:12:58

Żadne z powyższych nie zadziałało. W moim przypadku {[2] } zostanie wywołany, zanim czarna ramka zniknie, więc nadal będę widział czarną ramkę.

Potrzebowałem rozwiązania, w którym film pojawił się krótko po pierwszej klatce.

Czyli ustawiłem VideoView alpha na 0 w xml:

android:alpha="0"

A potem przed uruchomieniem filmiku animuję Alfę z powrotem do 1:

videoView.animate().alpha(1);
videoView.seekTo(0);
videoView.start();

Alternatywnie, możesz po prostu opublikować opóźnione uruchamianie, aby ustawić Alfę na 1, zamiast ją animować.

 1
Author: Siavash,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-21 03:51:34

Trochę za późno na tę odpowiedź, ale może inni użytkownicy mają ten sam problem i znaleźć to pytanie..

Poradziłem sobie z tym, ustawiając Tło początkowo Źródło, a następnie, podczas uruchamiania wideo, ustawiłem tło na niewidoczny kolor..

VideoView myView = findViewById(R.id.my_view);
myView.setBackgroundResource(R.drawable.some_resource);
// some stuff

// this is when starting the video
myView.setVideoUri(someUri);
// also set MediaController somewhere...
//...
// now set the backgroundcolor to be not visible (first val of Color.argb(..) is the alpha)
myView.setBackGroundColor(Color.argb(0, 0, 0, 0));
//...
myView.start();
 0
Author: MalaKa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-31 13:45:20

Wolałbym takie rozwiązanie : https://stackoverflow.com/a/11016465/2267723

Its about surface, więc utwórz atrapę @ fragment layout i nie zobaczysz ponownie tego czarnego błysku ..

 0
Author: Maher Abuthraa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:05

To jest fajne rozwiązanie:

package com.example.videoviewpractice;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {

    VideoView myVideoView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initVideo();
}

private void initVideo() {
    myVideoView = (VideoView) findViewById(R.id.videoView1);
    String url = "http://mtc.cdn.vine.co/r/videos/3DF00EB7001110633055418310656_1e50d6d9a65.3.2.mp4?" + 
            "versionId=KVMUFFGqe6rYRrGKgl8hxL6eakVAErPy";
    myVideoView.setVideoURI(Uri.parse(url));
    myVideoView.setMediaController(new MediaController(this));
    myVideoView.requestFocus();
}

public void gone(View v){
    myVideoView.setZOrderOnTop(true);
    View placeholder = (View) findViewById(R.id.placeholder);

    placeholder.setVisibility(View.GONE);
    myVideoView.start();
}

}

Activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<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/videoView1"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_gravity="top|center"
        android:visibility="visible" />

    <FrameLayout
        android:id="@+id/placeholder"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_gravity="top|center"
        android:background="@drawable/ic_launcher"
        android:onClick="gone" >
    </FrameLayout>

</FrameLayout>

</LinearLayout>
 0
Author: OShiffer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-17 21:46:17

Aby uniknąć irytujących migotania i problemów z czarnym ekranem napisałem FrameVideoView.

Korzysta z "rozwiązania zastępczego" i (jeśli urządzenie ma API poziomu 14 lub wyższego) z TextureView, który jest znacznie bardziej wydajny niż VideoView.

Napisałem Artykuł na naszym blogu, aby opisać to, co faktycznie robi.

Jest prosty w użyciu:

Dodaj FrameVideoView do layoutu:

<mateuszklimek.framevideoview.FrameVideoView
    android:id="@+id/frame_video_view"
    android:layout_width="@dimen/video_width"
    android:layout_height="@dimen/video_height"
  />

Znajdź jego instancję w Activity i wywołaj odpowiednie metody w onResume i onPause:

public class SampleActivity extends Activity {

  private FrameVideoView videoView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple);

    String uriString = "android.resource://" + getPackageName() + "/" + R.raw.movie;
    videoView = (FrameVideoView) findViewById(R.id.frame_video_view);
    videoView.setup(Uri.parse(uriString), Color.GREEN);
  }

    @Override
    protected void onResume() {
      super.onResume();
      videoView.onResume();
    }

    @Override
    protected void onPause() {
      videoView.onPause();
      super.onPause();
    }
  }
 0
Author: klimat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-15 13:41:02

Użyj svVideoView.seekTo (position) .

Podaj pozycję w ciągu 5 (ms).

onPause():
position=svVideoView.getCurrentPosition()

onResume():
svVideoView.seekTo(position);
 0
Author: Raj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-05 11:00:16

Miałem ten sam problem. Odkryłem, że głównym powodem tego było użycie FrameLayout jako układu nadrzędnego. Użyj RelativeLayout jako nadrzędnego układu VideoView

 0
Author: Vinil Chandran,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-27 07:13:37

Działa na mnie zarówno na aktywności, jak i na fragmencie.

VideoView mVideo = (VideoView) findViewById(R.id.yourViewViewId);
          mVideo.setVideoURI(mUri);
          mVideo.setZOrderOnTop(false);

SurfaceHolder surfaceholder = mVideo.getHolder();
surfaceholder.setFormat(PixelFormat.TRANSPARENT);
 0
Author: Nam lê đình,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-07 11:13:37

Zobacz to

VideoView videoView = (VideoView) findViewById(R.id.VideoView);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        Uri video = Uri.parse("android.resource://your_package_name/"+R.raw.monkeysonthebed_video);

        videoView.setMediaController(mediaController);
        videoView.setVideoURI(video);
        videoView.start();
 -1
Author: Venkata Krishna,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-19 05:58:58