我想在远程视频准备播放后隐藏ImageView
缩略图,意味着onPrepared
执行但imageView.setVisibility(View.GONE)
根本不起作用。
我看到了这个答案one,two,我认为原因是SurfaceView
或VideoView
。根据答案
我尝试过同时使用MediaPlayer
和VideoView
代码
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>
请帮忙!我被困了