修改BUG
This commit is contained in:
@@ -22,6 +22,7 @@ import androidx.databinding.DataBindingUtil;
|
||||
|
||||
import com.blankj.utilcode.util.LogUtils;
|
||||
import com.blankj.utilcode.util.PathUtils;
|
||||
import com.blankj.utilcode.util.ThreadUtils;
|
||||
import com.google.android.exoplayer2.ExoPlayer;
|
||||
import com.google.android.exoplayer2.MediaItem;
|
||||
import com.google.android.exoplayer2.ui.PlayerView;
|
||||
@@ -299,10 +300,42 @@ public class AvatarFrameView extends FrameLayout implements IAnimListener {
|
||||
clearQueue();
|
||||
return;
|
||||
}
|
||||
// 异步处理URL解析等耗时操作
|
||||
ThreadUtils.executeByIo(new ThreadUtils.SimpleTask<String>() {
|
||||
@Override
|
||||
public String doInBackground() throws Throwable {
|
||||
// 在后台线程进行URL有效性检查或其他预处理
|
||||
if (url == null || url.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return url; // 返回处理后的URL
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(String processedUrl) {
|
||||
if (processedUrl != null) {
|
||||
// 使用post方法确保在下一个UI循环中执行
|
||||
mainHandler.post(() -> {
|
||||
// 再次检查状态
|
||||
if (!isDestroyed && !isPlaying) {
|
||||
playQueue.add(new PlayItem(processedUrl, type2));
|
||||
checkAndStartPlayback();
|
||||
} else {
|
||||
// 如果正在播放,添加到队列中
|
||||
playQueue.add(new PlayItem(processedUrl, type2));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFail(Throwable e) {
|
||||
LogUtils.e("Error processing gift URL: " + e.getMessage());
|
||||
}
|
||||
});
|
||||
// 添加到播放队列
|
||||
// playQueue.offer(new PlayItem(url, type2));
|
||||
playQueue.add(new PlayItem(url, type2));
|
||||
// playQueue.add(new PlayItem(url, type2));
|
||||
Logger.d("AvatarFrameView", "Added to queue, queue size: " + playQueue.size() + ", url: " + url);
|
||||
|
||||
// 如果当前没有在播放,则开始播放
|
||||
@@ -310,7 +343,7 @@ public class AvatarFrameView extends FrameLayout implements IAnimListener {
|
||||
// playNextFromQueue();
|
||||
// }
|
||||
// 改进播放检查逻辑
|
||||
checkAndStartPlayback();
|
||||
// checkAndStartPlayback();
|
||||
}
|
||||
|
||||
|
||||
@@ -829,43 +862,113 @@ public class AvatarFrameView extends FrameLayout implements IAnimListener {
|
||||
private void releaseResources() {
|
||||
LogUtils.d(TAG, "Releasing all resources");
|
||||
|
||||
// if (isDestroyed) return;
|
||||
if (isDestroyed) return;
|
||||
// 使用异步线程处理耗时操作
|
||||
new Thread(() -> {
|
||||
try {
|
||||
// 在后台线程处理文件操作和大对象清理
|
||||
performHeavyCleanup();
|
||||
|
||||
try {
|
||||
// 清理 SVGA 资源
|
||||
if (svgaSurface != null && svgaSurface.getDrawable() instanceof SVGADrawable) {
|
||||
SVGADrawable drawable = (SVGADrawable) svgaSurface.getDrawable();
|
||||
if (drawable != null) {
|
||||
try {
|
||||
drawable.stop();
|
||||
svgaSurface.clearAnimation();
|
||||
svgaSurface.setImageDrawable(null);
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "Error releasing SVGA resources: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
// 回到主线程处理 UI 相关的清理
|
||||
mainHandler.post(() -> {
|
||||
performUICleanup();
|
||||
});
|
||||
} catch (Exception e) {
|
||||
Logger.e(TAG, "Error in async releaseResources: " + e.getMessage());
|
||||
// 出错时仍在主线程清理 UI 资源
|
||||
mainHandler.post(() -> {
|
||||
performUICleanup();
|
||||
});
|
||||
}
|
||||
}).start();
|
||||
|
||||
|
||||
// try {
|
||||
// // 清理 SVGA 资源
|
||||
// if (svgaSurface != null && svgaSurface.getDrawable() instanceof SVGADrawable) {
|
||||
// SVGADrawable drawable = (SVGADrawable) svgaSurface.getDrawable();
|
||||
// if (drawable != null) {
|
||||
// try {
|
||||
// drawable.stop();
|
||||
// svgaSurface.clearAnimation();
|
||||
// svgaSurface.setImageDrawable(null);
|
||||
// } catch (Exception e) {
|
||||
// LogUtils.e(TAG, "Error releasing SVGA resources: " + e.getMessage());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 停止并清理播放器
|
||||
// if (mBinding != null && mBinding.playView != null) {
|
||||
// mBinding.playView.stopPlay();
|
||||
// }
|
||||
//
|
||||
// // 清理 ExoPlayer 资源
|
||||
// if (exoPlayer != null) {
|
||||
// try {
|
||||
// exoPlayer.stop();
|
||||
// exoPlayer.clearVideoSurface();
|
||||
// } catch (Exception e) {
|
||||
// LogUtils.e(TAG, "Error releasing ExoPlayer resources: " + e.getMessage());
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// LogUtils.e(TAG, "Error in releaseResources: " + e.getMessage());
|
||||
// }
|
||||
}
|
||||
/**
|
||||
* 在后台线程执行耗时的清理操作
|
||||
*/
|
||||
private void performHeavyCleanup() {
|
||||
try {
|
||||
// 清理缓存文件(如果需要)
|
||||
// 清理大对象引用等
|
||||
// clearCacheFiles();
|
||||
} catch (Exception e) {
|
||||
Logger.e(TAG, "Error in performHeavyCleanup: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在主线程执行 UI 相关的清理操作
|
||||
*/
|
||||
private void performUICleanup() {
|
||||
try {
|
||||
// 停止并清理播放器
|
||||
if (mBinding != null && mBinding.playView != null) {
|
||||
mBinding.playView.stopPlay();
|
||||
try {
|
||||
mBinding.playView.stopPlay();
|
||||
} catch (Exception e) {
|
||||
Logger.e(TAG, "Error stopping playView: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 清理 ExoPlayer 资源
|
||||
if (exoPlayer != null) {
|
||||
try {
|
||||
// 使用异步停止避免阻塞
|
||||
exoPlayer.stop();
|
||||
exoPlayer.clearVideoSurface();
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "Error releasing ExoPlayer resources: " + e.getMessage());
|
||||
Logger.e(TAG, "Error releasing ExoPlayer resources: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 清理 SVGA 资源
|
||||
if (svgaSurface != null) {
|
||||
try {
|
||||
svgaSurface.pauseAnimation();
|
||||
svgaSurface.clearAnimation();
|
||||
svgaSurface.setImageDrawable(null);
|
||||
} catch (Exception e) {
|
||||
Logger.e(TAG, "Error releasing SVGA resources: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "Error in releaseResources: " + e.getMessage());
|
||||
Logger.e(TAG, "Error in performUICleanup: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 公共释放方法,用于外部主动释放资源
|
||||
*/
|
||||
@@ -877,7 +980,7 @@ public class AvatarFrameView extends FrameLayout implements IAnimListener {
|
||||
mainHandler.post(this::release);
|
||||
return;
|
||||
}
|
||||
// isDestroyed = true;
|
||||
isDestroyed = true;
|
||||
|
||||
try {
|
||||
// 清空播放队列
|
||||
@@ -886,26 +989,36 @@ public class AvatarFrameView extends FrameLayout implements IAnimListener {
|
||||
// 释放所有资源
|
||||
releaseResources();
|
||||
|
||||
// 释放 ExoPlayer
|
||||
if (exoPlayer != null) {
|
||||
try {
|
||||
exoPlayer.stop();
|
||||
exoPlayer.release();
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "Error releasing ExoPlayer: " + e.getMessage());
|
||||
// 延迟清理 ExoPlayer(避免主线程阻塞)
|
||||
mainHandler.postDelayed(() -> {
|
||||
if (exoPlayer != null) {
|
||||
try {
|
||||
exoPlayer.release();
|
||||
} catch (Exception e) {
|
||||
Logger.e(TAG, "Error releasing ExoPlayer: " + e.getMessage());
|
||||
}
|
||||
exoPlayer = null;
|
||||
}
|
||||
exoPlayer = null;
|
||||
}
|
||||
}, 50);
|
||||
|
||||
// 清理 PlayerView
|
||||
if (playerView != null) {
|
||||
try {
|
||||
playerView.setPlayer(null);
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "Error releasing PlayerView: " + e.getMessage());
|
||||
// 延迟清理其他资源
|
||||
mainHandler.postDelayed(() -> {
|
||||
// 清理 PlayerView
|
||||
if (playerView != null) {
|
||||
try {
|
||||
playerView.setPlayer(null);
|
||||
} catch (Exception e) {
|
||||
Logger.e(TAG, "Error releasing PlayerView: " + e.getMessage());
|
||||
}
|
||||
playerView = null;
|
||||
}
|
||||
playerView = null;
|
||||
}
|
||||
|
||||
// 清理 binding
|
||||
if (mBinding != null) {
|
||||
mBinding = null;
|
||||
}
|
||||
}, 100);
|
||||
|
||||
|
||||
// 清理 SVGAImageView
|
||||
if (svgaSurface != null) {
|
||||
@@ -931,7 +1044,7 @@ public class AvatarFrameView extends FrameLayout implements IAnimListener {
|
||||
LogUtils.e(TAG, "Error in AvatarFrameView release: " + e.getMessage());
|
||||
} finally {
|
||||
// 建议进行垃圾回收
|
||||
MemoryOptimizationUtils.forceGC();
|
||||
// MemoryOptimizationUtils.forceGC();
|
||||
}
|
||||
}
|
||||
public void clearQueue() {
|
||||
|
||||
Reference in New Issue
Block a user