|
|
|
@ -4,24 +4,24 @@ import android.content.Context; |
|
|
|
import android.graphics.Color; |
|
|
|
import android.graphics.PixelFormat; |
|
|
|
import android.graphics.drawable.Drawable; |
|
|
|
import android.net.Uri; |
|
|
|
import android.util.AttributeSet; |
|
|
|
import android.util.Log; |
|
|
|
import android.view.SurfaceView; |
|
|
|
import android.view.TextureView; |
|
|
|
import android.view.View; |
|
|
|
import android.view.ViewGroup; |
|
|
|
import android.widget.FrameLayout; |
|
|
|
import android.widget.VideoView; |
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
import androidx.annotation.Nullable; |
|
|
|
|
|
|
|
import com.google.android.exoplayer2.PlaybackException; |
|
|
|
import com.google.android.exoplayer2.ExoPlayer; |
|
|
|
import com.google.android.exoplayer2.MediaItem; |
|
|
|
import com.google.android.exoplayer2.PlaybackException; |
|
|
|
import com.google.android.exoplayer2.Player; |
|
|
|
import com.google.android.exoplayer2.SimpleExoPlayer; |
|
|
|
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; |
|
|
|
import com.google.android.exoplayer2.ui.PlayerView; |
|
|
|
import com.google.android.exoplayer2.ui.StyledPlayerView; |
|
|
|
|
|
|
|
/** |
|
|
|
@ -30,7 +30,7 @@ import com.google.android.exoplayer2.ui.StyledPlayerView; |
|
|
|
* Date: 2021/5/19 |
|
|
|
* Time: 13:59 |
|
|
|
*/ |
|
|
|
public class CustomerVideoView extends FrameLayout { |
|
|
|
public class CustomerVideoView extends FrameLayout { |
|
|
|
|
|
|
|
private StyledPlayerView playerView; |
|
|
|
private ExoPlayer player; |
|
|
|
@ -38,6 +38,7 @@ import com.google.android.exoplayer2.ui.StyledPlayerView; |
|
|
|
private OnErrorListener onErrorListener; |
|
|
|
private OnPreparedListener onPreparedListener; |
|
|
|
private OnInfoListener onInfoListener; |
|
|
|
private Boolean isFirstFame = false; |
|
|
|
|
|
|
|
// 监听器接口,保持与 VideoView 兼容
|
|
|
|
public interface OnCompletionListener { |
|
|
|
@ -81,21 +82,18 @@ import com.google.android.exoplayer2.ui.StyledPlayerView; |
|
|
|
// 设置背景透明
|
|
|
|
playerView.setBackgroundColor(Color.TRANSPARENT); |
|
|
|
playerView.setShutterBackgroundColor(Color.TRANSPARENT); |
|
|
|
|
|
|
|
// 3. 设置使用透明背景
|
|
|
|
playerView.setUseArtwork(false); // 如果不需要 artwork
|
|
|
|
playerView.setDefaultArtwork(null); // 清除默认 artwork
|
|
|
|
|
|
|
|
// 4. 设置 SurfaceView 透明
|
|
|
|
View videoSurfaceView = playerView.getVideoSurfaceView(); |
|
|
|
if (videoSurfaceView != null) { |
|
|
|
videoSurfaceView.setBackgroundColor(Color.TRANSPARENT); |
|
|
|
|
|
|
|
// 如果使用 SurfaceView,设置格式支持透明
|
|
|
|
if (videoSurfaceView instanceof SurfaceView) { |
|
|
|
Log.e("View: ","SurfaceView"); |
|
|
|
SurfaceView surfaceView = (SurfaceView) videoSurfaceView; |
|
|
|
surfaceView.setZOrderOnTop(true); // 必须设置为 true 才能使透明生效
|
|
|
|
surfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT); |
|
|
|
replaceWithTextureView(playerView, (SurfaceView) surfaceView); |
|
|
|
}else if (videoSurfaceView instanceof TextureView) { |
|
|
|
// TextureView 默认支持透明
|
|
|
|
videoSurfaceView.setBackgroundColor(Color.TRANSPARENT); |
|
|
|
@ -103,6 +101,25 @@ import com.google.android.exoplayer2.ui.StyledPlayerView; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void replaceWithTextureView(StyledPlayerView playerView, SurfaceView oldSurface) { |
|
|
|
ViewGroup parent = (ViewGroup) oldSurface.getParent(); |
|
|
|
if (parent != null) { |
|
|
|
// 创建新的 TextureView
|
|
|
|
TextureView textureView = new TextureView(getContext()); |
|
|
|
textureView.setLayoutParams(oldSurface.getLayoutParams()); |
|
|
|
|
|
|
|
// 替换
|
|
|
|
int index = parent.indexOfChild(oldSurface); |
|
|
|
parent.removeView(oldSurface); |
|
|
|
parent.addView(textureView, index); |
|
|
|
|
|
|
|
// 设置到播放器
|
|
|
|
if (playerView.getPlayer() != null) { |
|
|
|
playerView.getPlayer().setVideoTextureView(textureView); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void initializePlayer() { |
|
|
|
// 创建 ExoPlayer 实例
|
|
|
|
player = new SimpleExoPlayer.Builder(getContext()) |
|
|
|
@ -129,15 +146,17 @@ import com.google.android.exoplayer2.ui.StyledPlayerView; |
|
|
|
if (onCompletionListener != null) { |
|
|
|
onCompletionListener.onCompletion(); |
|
|
|
} |
|
|
|
isFirstFame = false; |
|
|
|
} |
|
|
|
if(playbackState == Player.STATE_READY){ |
|
|
|
Log.e("loadVideo: ","STATE_READY"); |
|
|
|
// 视频准备就绪
|
|
|
|
if (onPreparedListener != null) { |
|
|
|
onPreparedListener.onPrepared(); |
|
|
|
} |
|
|
|
if (playerView != null) { |
|
|
|
playerView.setVisibility(VISIBLE); |
|
|
|
playerView.setAlpha(0); |
|
|
|
setVisibility(VISIBLE); |
|
|
|
if(playerView != null && !isFirstFame){ |
|
|
|
playerView.setAlpha(0f); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -150,14 +169,19 @@ import com.google.android.exoplayer2.ui.StyledPlayerView; |
|
|
|
} |
|
|
|
@Override |
|
|
|
public void onRenderedFirstFrame() { |
|
|
|
// 视频开始渲染第一帧
|
|
|
|
Log.e("loadVideo: ","FirstFrame"); |
|
|
|
if (onInfoListener != null) { |
|
|
|
onInfoListener.onInfo(); |
|
|
|
onInfoListener=null; |
|
|
|
} |
|
|
|
isFirstFame = true; |
|
|
|
if(playerView != null){ |
|
|
|
playerView.setAlpha(1); |
|
|
|
playerView.setAlpha(1f); |
|
|
|
} |
|
|
|
// postDelayed(new Runnable() {
|
|
|
|
// @Override
|
|
|
|
// public void run() {
|
|
|
|
// }
|
|
|
|
// }, 100);
|
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
@ -206,14 +230,16 @@ import com.google.android.exoplayer2.ui.StyledPlayerView; |
|
|
|
@Override |
|
|
|
public void setBackgroundColor(int color) { |
|
|
|
super.setBackgroundColor(color); |
|
|
|
|
|
|
|
if (color != Color.TRANSPARENT && playerView != null) { |
|
|
|
playerView.setVisibility(GONE); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void setBackgroundResource(int resid) { |
|
|
|
super.setBackgroundResource(resid); |
|
|
|
if (playerView != null) { |
|
|
|
playerView.setVisibility(GONE); |
|
|
|
playerView.setVisibility(INVISIBLE); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -345,3 +371,4 @@ import com.google.android.exoplayer2.ui.StyledPlayerView; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|