在获取弹幕时抛异常后尝试关闭数据库

This commit is contained in:
zlzw 2024-11-28 18:21:50 +08:00
parent 52e4312a32
commit 3f10e19e50

View File

@ -89,36 +89,42 @@ public class LiveDanmuService {
public LiveVideoDanmu getDanmu(String roomId, String videoId, int page) {
LiveVideoDanmu danmus = new LiveVideoDanmu();
BiliLiveDatabase liveDatabase = new BiliLiveDatabase(LiveRoomConfig.buildConfig(roomId));
LiveVideoDatabaseBean videoBean = liveDatabase.getVideo(videoId);
if (videoBean == null) {
return new LiveVideoDanmu();
}
long startTime = videoBean.getStartTime().getTime();
long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime();
List<LiveSuperChatDatabaseBean> superChatList = liveDatabase.getOfTime(startTime, endTime, LiveSuperChatDatabaseBean.class);
try {
LiveVideoDatabaseBean videoBean = liveDatabase.getVideo(videoId);
if (videoBean == null) {
return new LiveVideoDanmu();
}
long startTime = videoBean.getStartTime().getTime();
long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime();
List<LiveSuperChatDatabaseBean> superChatList = liveDatabase.getOfTime(startTime, endTime, LiveSuperChatDatabaseBean.class);
long count = liveDatabase.getCount(new LiveDanmuDatabaseBean().getTableName());
int pageSize = 10000;
int pageCount = (int) Math.ceil((double) count / pageSize);
long count = liveDatabase.getCount(new LiveDanmuDatabaseBean().getTableName());
int pageSize = 10000;
int pageCount = (int) Math.ceil((double) count / pageSize);
List<LiveVideoDanmu.Danmu> danmuList = liveDatabase.getOfTime(startTime, endTime, page, pageSize, LiveDanmuDatabaseBean.class)
.stream()
.map(item -> createDanmu(item, startTime))
.filter(item-> item.getTime()>=0)
.toList();
danmus.getDanmu().addAll(danmuList);
danmus.setDanmuCount(count);
if (page < pageCount) {
danmus.setNextDanmu(true);
List<LiveVideoDanmu.Danmu> danmuList = liveDatabase.getOfTime(startTime, endTime, page, pageSize, LiveDanmuDatabaseBean.class)
.stream()
.map(item -> createDanmu(item, startTime))
.filter(item -> item.getTime() >= 0)
.toList();
danmus.getDanmu().addAll(danmuList);
danmus.setDanmuCount(count);
if (page < pageCount) {
danmus.setNextDanmu(true);
}
for (LiveSuperChatDatabaseBean bean : superChatList) {
LiveVideoDanmu.SuperChat superChat = new LiveVideoDanmu.SuperChat(startTime, bean);
danmus.getSuperChat().add(superChat);
}
} catch (Exception e) {
Log.e(e);
} finally {
liveDatabase.close();
}
for (LiveSuperChatDatabaseBean bean : superChatList) {
LiveVideoDanmu.SuperChat superChat = new LiveVideoDanmu.SuperChat(startTime, bean);
danmus.getSuperChat().add(superChat);
}
liveDatabase.close();
return danmus;
}