【问题标题】:Is there a way to avoid the momentary black screen prior to video playback in VideoPlayer?有没有办法避免在 VideoPlayer 中播放视频之前出现瞬间黑屏?
【发布时间】:2014-01-18 02:18:04
【问题描述】:

我正在使用全屏 VideoView 并播放原始文件夹中

出于某种原因,无论我做什么(并且我已经尝试了所有我能想到的方法),在视频播放之前播放器都会黑屏大约 1/4 秒。

我尝试将VideoView 的可见性设置为隐藏,并使用onPreparedListener 回调在它回来之前不公开它。我已经尝试在其上放置一个通用计时器以在将其可见性重新设置为可见之前给它一秒钟,我已经尝试将seek()ing 设置为 1 秒位置,我什至尝试加载视频@ 987654325@(直到应用程序稍后才需要它)以便在用户到达它时加载并准备好运行,但无论我做什么,它都坚持在播放之前坐在闪烁的黑暗中。

希望得到一些关于我可以做些什么来让VideoView 在它真正准备好开始播放之前不出现的指导。

我很高兴发布代码,但它是非常通用的VideoView 播放代码。我没有做任何不寻常的事情。

【问题讨论】:

    标签: android android-videoview


    【解决方案1】:

    因此,解决方案是我正在将视频加载到可见性未设置为 View.VISIBLE 的 VideoView 中。

    至少在撰写本文时(Android v.4.3),VideoView 本身必须可见才能开始加载过程。但是,很高兴成为包装器 Layout 的子代,其可见性设置为 View.INVISIBLE(没有尝试过 View.GONE,但怀疑也可以)。

    所以...关键是要有一个不可见的包装布局,在 VideoView 上设置一个 OnPrepared 侦听器,并在触发时公开包装布局。在我的例子中,我注意到仍然有闪烁,所以我在暴露父布局之前使用 Handler 和 Runnable 添加了另外 400 毫秒的延迟,它可以完美地工作。

    <!-- THIS VIEW SHOULD BE INVISIBLE SO THAT WE CAN PRE-LOAD THE VIDEO WITHOUT THE BLACK SCREEN -->
    <RelativeLayout
        android:id="@+id/vidContainer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="230dp" 
        android:layout_marginBottom="200dp"
        android:visibility="invisible"
        >
    
        <!-- THIS VIEW MUST BE VISIBLE IN ORDER TO LOAD VIDEO!!!!!! -->
        <VideoView
            android:id="@+id/videoView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerInParent="true"
             />
    </RelativeLayout>
    

    然后在安卓中

    private void playVideo(int vidid) {
        //vid id is whatever the video resource is, as in R.raw.myVideo
        Uri vid = Uri.parse("android.resource://" + getPackageName() + "/"
                + vidid);
        winnerVidPlayer = (VideoView) findViewById(R.id.videoView);
    
        //Let the preparer fire and when it does, wait a little longer to avoid ANY flicker before exposing
        //the wrapper Layout
        winnerVidPlayer.setOnPreparedListener(new OnPreparedListener() {        
            @Override
            public void onPrepared(MediaPlayer mp) {    
                new Handler().postDelayed(new Runnable(){
                    public void run(){                                                              
    
            ((RelativeLayout)findViewById(R.id.vidContainer)).setVisibility(View.VISIBLE);  
    
                                       }
                },400);
    
            }           
        });
    
       winnerVidPlayer.setVideoURI(vid); 
       winnerVidPlayer.start();       
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-22
      • 2020-08-07
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2015-09-08
      相关资源
      最近更新 更多