Browse Source

fix: 视频切换优化

dejiAi
高志龙 3 months ago
parent
commit
7be4b31379
  1. 2
      app/build.gradle
  2. 107
      app/src/main/java/qianmu/container/activity/H5/WebViewActivity.java

2
app/build.gradle

@ -10,7 +10,7 @@ android {
minSdkVersion 22
targetSdkVersion 30
versionCode 6
versionName "V2.0.7.49"
versionName "V2.0.7.50"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'

107
app/src/main/java/qianmu/container/activity/H5/WebViewActivity.java

@ -1,6 +1,7 @@
package qianmu.container.activity.H5;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
@ -21,6 +22,7 @@ import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.webkit.ConsoleMessage;
@ -107,9 +109,10 @@ public class WebViewActivity extends BaseActivity {
static final int TYPE_HINT_PASSWORD = 3;//隐藏密码输入框
static final int TYPE_START_SERVER = 4;//重新启动web服务
static String HtmlUrl = "http://127.0.0.1:8080/index.html";//webServer服务地址 http://192.168.1.218:5173/
//static String HtmlUrl = "http://192.168.1.196:5500/index.html";
// static String HtmlUrl = "http://192.168.1.196:5500/index.html";
int time = 0;
private VideoView currentVideo;
private VideoView targetVideo; // 当前正在切换的目标视频
private SocketClient localSocketClient;
private Map<String, Uri> videoMap = new HashMap<>();
private SoundPool soundPool;
@ -451,59 +454,93 @@ public class WebViewActivity extends BaseActivity {
default:
nextVideo = "".equals(videoAfter) ? binding.bgVideo : binding.bgVideo1;
}
// 检查是否是重复切换
if (nextVideo == currentVideo) {
return;
}
currentVideo.clearAnimation();
nextVideo.clearAnimation();
LoggerUtil.e("切换视频", type.trim());
VideoView farVideo;
farVideo = currentVideo;
currentVideo = nextVideo;
// 旧视频淡出
// 如果上一个切换还没完成,立即停止上一个目标视频
if (targetVideo != null && targetVideo != nextVideo) {
try {
if (targetVideo.isPlaying()) {
targetVideo.pause();
targetVideo.stopPlayback();
}
} catch (Exception e) {
LoggerUtil.e("changeVideo", "停止上一个目标视频失败", e);
}
}
// 记录新的目标视频
targetVideo = nextVideo;
// 立即停止当前视频
VideoView farVideo = currentVideo;
try {
if (farVideo.isPlaying()) {
farVideo.pause();
farVideo.stopPlayback();
}
} catch (Exception e) {
LoggerUtil.e("changeVideo", "停止当前视频失败", e);
}
// 开始淡入动画
farVideo.animate()
.alpha(0.5f)
.setDuration(300)
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
farVideo.pause();
farVideo.stopPlayback();
}
.alpha(0.3f)
.setDuration(100)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
farVideo.setVisibility(View.GONE);
farVideo.setAlpha(1f); // 恢复初始状态,以备下次使用
farVideo.setAlpha(1f);
}
@Override
public void onAnimationCancel(Animator animation) {}
public void onAnimationCancel(Animator animation) {
farVideo.setVisibility(View.GONE);
farVideo.setAlpha(1f);
}
})
.setInterpolator(new AccelerateDecelerateInterpolator())
.start();
@Override
public void onAnimationRepeat(Animator animation) {}
});
// 更新当前视频引用
currentVideo = nextVideo;
// 新视频淡入
nextVideo.setAlpha(0.3f); // 先设置为完全透明
// 准备新视频
nextVideo.setAlpha(0.5f);
nextVideo.setVisibility(View.VISIBLE);
// 确保视频已经准备好
if (!nextVideo.isPlaying()) {
try {
nextVideo.seekTo(0);
nextVideo.start();
} catch (Exception e) {
LoggerUtil.e("changeVideo", "启动新视频失败", e);
}
}
// 开始淡入动画
nextVideo.animate()
.alpha(1f)
.setDuration(500)
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
if (!nextVideo.isPlaying()) {
nextVideo.start();
}
}
.setDuration(300) // 缩短动画时间,让切换更迅速
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if (!nextVideo.isPlaying()) {
nextVideo.start();
}
targetVideo = null;
}
@Override
public void onAnimationCancel(Animator animation) {}
@Override
public void onAnimationRepeat(Animator animation) {}
});
public void onAnimationCancel(Animator animation) {
targetVideo = null;
}
})
.setInterpolator(new AccelerateDecelerateInterpolator())
.start();
}
@Override

Loading…
Cancel
Save