ImageVIew缩略图setVisibility GONE无法在onPrepared

时间:2017-04-14 11:51:49

标签: android android-mediaplayer surfaceview android-videoview android-textureview

我想在远程视频准备播放后隐藏ImageView缩略图,意味着onPrepared执行但imageView.setVisibility(View.GONE)根本不起作用。

我看到了这个答案onetwo,我认为原因是SurfaceViewVideoView。根据答案

我尝试过同时使用MediaPlayerVideoView

使用 MediaPlayer SurfaceView

代码

mMediaPlayer = new MediaPlayer();
        holder.surfaceView.setDrawingCacheEnabled(true);
        try {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource(mContext, Uri.parse(video_url));
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                    ((Activity)mContext).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                        holder.imgThumbnail.getParent().requestTransparentRegion(holder.imgThumbnail);
                        holder.imgThumbnail.setVisibility(View.GONE);
                        holder.imgThumbnail.getParent().requestTransparentRegion(holder.imgThumbnail);
                        }
                    });
                    Toast.makeText(mContext,"onPrepared",Toast.LENGTH_LONG).show();

                }
            });

        } catch (IllegalArgumentException | SecurityException | IllegalStateException | IOException e) {
            e.printStackTrace();
        }

        mMediaPlayer.prepareAsync();
SurfaceView

XML

<RelativeLayout
        android:layout_width="410dp"
        android:id="@+id/v_view"
        android:visibility="visible"
        android:layout_height="307.50dp">

        <SurfaceView
            android:id="@+id/video_view"
            android:layout_width="410dp"
            android:layout_height="307.50dp"
             />

        <ImageView
            android:id="@+id/imgThumbnail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible"
            android:scaleType="centerCrop" />
    </RelativeLayout>

代码使用 VideoView

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        Log.i(TAG,"onPrepared");
        new  Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                videoView.start();
                Toast.makeText(mContext,"start video",Toast.LENGTH_LONG).show();

                ((Activity)mContext).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {


                    holder.imgThumbnail.getParent().
                    requestTransparentRegion(holder.imgThumbnail);

                    holder.imgThumbnail.setVisibility(View.GONE);
                    holder.imgThumbnail.getParent().
                    requestTransparentRegion(holder.imgThumbnail);
                    }
                });
            }
        },100);
    }
});
VideoView

XML

<RelativeLayout
        android:layout_width="410dp"
        android:id="@+id/v_view"
        android:visibility="visible"
        android:layout_height="307.50dp">

        <VideoView
            android:id="@+id/video_view"
            android:layout_width="410dp"
            android:layout_height="307.50dp"
             />

        <ImageView
            android:id="@+id/imgThumbnail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:background="@color/back_orange"
            android:scaleType="centerCrop" />
    </RelativeLayout>

请帮忙!我被困了

0 个答案:

没有答案