修复当正在播放时,投屏设备被强制退出程序,再次查找该设备投屏失败问题,添加所有控制按钮验证是否已选择文件和设备

This commit is contained in:
yqsphp
2026-01-15 15:00:06 +08:00
parent eecf175e7f
commit 7102e77b2a
3 changed files with 49 additions and 16 deletions

View File

@@ -14,15 +14,17 @@ class DeviceList:
:return:
"""
if device["udn"] not in self.list:
self.list.setdefault(device["udn"], {"stat":False, "controller": None, "pause": False})
self.list.setdefault(device["udn"], {"stat":False, "location":device["location"], "controller": None, "pause": False})
return self.list[device["udn"]]
def update_device(self, device, stat, controller = None, pause = None):
def update_device(self, device, stat, controller = None, pause = None, location = None):
"""
更新设备信息
:param device:设备
:param stat:设备状态播放 bool
:param controller:控制对象
:param pause: 暂停状态
:param location: 设备文档地址
:return:
"""
if device["udn"] in self.list:
@@ -33,6 +35,9 @@ class DeviceList:
self.list[key].update({"stat":stat,"controller":controller})
if pause is not None:
self.list[key]["pause"] = pause
# 设备文档地址
if location is not None:
self.list[key]["location"] = location
return True

View File

@@ -37,14 +37,8 @@ class MainWindow(QMainWindow, UiWindow):
self.logger = AppLogger('MediaCastUI')
# 初始化ui
self.init_ui()
# 初始化服务
self.init_connections()
def init_connections(self):
"""初始化信号连接"""
# 设备发现
self.refresh_devices()
# 启动HTTP文件服务器
self.start_http_server()
@@ -224,7 +218,15 @@ class MainWindow(QMainWindow, UiWindow):
stat = self.selected_device_list.get_device(device)
#如果当前设备已存在,将控制对象赋值
if stat != False and stat["controller"] is not None:
self.dlna_controller = stat["controller"]
# 重新刷新判断获取设备uuid的location是否一致
if stat["location"] == device["location"]:
self.dlna_controller = stat["controller"]
else:
# 当投屏设备发生强制结束时,再次启动它的端口会变,这时从新实例化控制对象
self.dlna_controller = DLNAController(device)
# 还原控制按钮
self.selected_device_list.update_device(device, False, self.dlna_controller, False, device["location"])
self.enable_control_buttons(True)
# 当前选中设备如果正在播放中,控制按钮都不变
if stat != False and stat["stat"] == True:
@@ -269,12 +271,9 @@ class MainWindow(QMainWindow, UiWindow):
def start_casting(self):
"""开始投屏"""
if not self.selected_device :
self.log_message("请先选择设备", "error")
return
if not self.selected_file:
self.log_message("请先选择文件", "error")
if not self.select_device_file_stat():
return
try:
# 检查当前选中设备的控制对象
device = self.selected_device_list.get_device(self.selected_device)
@@ -311,7 +310,7 @@ class MainWindow(QMainWindow, UiWindow):
# 如果已经创建了DLNA控制器
if device != False and device["pause"] == True:
# 从暂停状态恢复播放
if self.dlna_controller.play():
if self.dlna_controller.play(self.speed):
self.selected_device_list.update_device(self.selected_device, True, pause=False)
# 设置控制按钮
self.enable_control_buttons(False)
@@ -345,6 +344,9 @@ class MainWindow(QMainWindow, UiWindow):
def pause_casting(self):
"""暂停投屏"""
if not self.select_device_file_stat():
return
if self.dlna_controller:
self.dlna_controller.pause()
# 重置暂停状态
@@ -357,6 +359,9 @@ class MainWindow(QMainWindow, UiWindow):
def stop_casting(self):
"""停止投屏"""
if not self.select_device_file_stat():
return
if self.dlna_controller:
self.dlna_controller.stop()
# 重置停止状态
@@ -368,11 +373,17 @@ class MainWindow(QMainWindow, UiWindow):
def volume_changed(self, value):
"""音量改变"""
if not self.select_device_file_stat():
return
if self.dlna_controller:
self.dlna_controller.set_volume(value)
def toggle_mute(self, checked):
"""切换静音"""
if not self.select_device_file_stat():
return
if self.dlna_controller:
self.dlna_controller.set_mute(checked)
icon = "🔇" if checked else "🔊"
@@ -386,6 +397,9 @@ class MainWindow(QMainWindow, UiWindow):
设置播放速度
:return:
"""
if not self.select_device_file_stat():
return
self.speed = self.speed_btn.currentData()
self.log_message(f"设置播放速度:{self.speed_btn.currentText()}")
@@ -408,6 +422,20 @@ class MainWindow(QMainWindow, UiWindow):
self.volume_slider.setEnabled(False if enabled else True)
self.mute_btn.setEnabled(False if enabled else True)
def select_device_file_stat(self):
"""
控制按钮都需要验证
:return:
"""
if not self.selected_device :
self.log_message("请先选择设备", "error")
return False
if not self.selected_file:
self.log_message("请先选择文件", "error")
return False
return True
def closeEvent(self, event):
"""窗口关闭事件"""
# 停止HTTP服务器

View File

@@ -54,7 +54,7 @@ class UiWindow:
"""
def init_ui(self):
"""初始化UI界面"""
version = "v1.1.0"
version = "v1.2.0"
app_name = "多媒体投屏 by yqsphp"
self.setWindowTitle(app_name)