play when switch
This commit is contained in:
parent
f0bba3a390
commit
2ac979e207
@ -21,48 +21,48 @@ class TrackMetadata:
|
|||||||
|
|
||||||
class MediaSourceController(ABC):
|
class MediaSourceController(ABC):
|
||||||
"""Базовый класс контроллера источника медиа."""
|
"""Базовый класс контроллера источника медиа."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Название источника."""
|
"""Название источника."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_volume(self) -> int:
|
def get_volume(self) -> int:
|
||||||
"""Получить текущую громкость (0-100)."""
|
"""Получить текущую громкость (0-100)."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def set_volume(self, value: int) -> None:
|
def set_volume(self, value: int) -> None:
|
||||||
"""Установить громкость (0-100)."""
|
"""Установить громкость (0-100)."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_metadata(self) -> TrackMetadata:
|
def get_metadata(self) -> TrackMetadata:
|
||||||
"""Получить метаданные текущего трека."""
|
"""Получить метаданные текущего трека."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def play(self) -> None:
|
def play(self) -> None:
|
||||||
"""Воспроизведение."""
|
"""Воспроизведение."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def pause(self) -> None:
|
def pause(self) -> None:
|
||||||
"""Пауза."""
|
"""Пауза."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def toggle_play(self) -> None:
|
def toggle_play(self) -> None:
|
||||||
"""Переключить воспроизведение/пауза."""
|
"""Переключить воспроизведение/пауза."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def next_track(self) -> None:
|
def next_track(self) -> None:
|
||||||
"""Следующий трек."""
|
"""Следующий трек."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def previous_track(self) -> None:
|
def previous_track(self) -> None:
|
||||||
"""Предыдущий трек."""
|
"""Предыдущий трек."""
|
||||||
@ -71,26 +71,26 @@ class MediaSourceController(ABC):
|
|||||||
|
|
||||||
class BluetoothController(MediaSourceController):
|
class BluetoothController(MediaSourceController):
|
||||||
"""Контроллер для Bluetooth аудио."""
|
"""Контроллер для Bluetooth аудио."""
|
||||||
|
|
||||||
def __init__(self, bt_service: Any):
|
def __init__(self, bt_service: Any):
|
||||||
self._bt_service = bt_service
|
self._bt_service = bt_service
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
return "Bluetooth"
|
return "Bluetooth"
|
||||||
|
|
||||||
def get_volume(self) -> int:
|
def get_volume(self) -> int:
|
||||||
# TODO: получить громкость из Bluetooth сервиса
|
# TODO: получить громкость из Bluetooth сервиса
|
||||||
return 50
|
return 50
|
||||||
|
|
||||||
def set_volume(self, value: int) -> None:
|
def set_volume(self, value: int) -> None:
|
||||||
# TODO: установить громкость в Bluetooth сервис
|
# TODO: установить громкость в Bluetooth сервис
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def get_metadata(self) -> TrackMetadata:
|
def get_metadata(self) -> TrackMetadata:
|
||||||
if self._bt_service is None:
|
if self._bt_service is None:
|
||||||
return TrackMetadata()
|
return TrackMetadata()
|
||||||
|
|
||||||
metadata = self._bt_service.get_player_metadata()
|
metadata = self._bt_service.get_player_metadata()
|
||||||
return TrackMetadata(
|
return TrackMetadata(
|
||||||
title=metadata.get("title", ""),
|
title=metadata.get("title", ""),
|
||||||
@ -101,23 +101,23 @@ class BluetoothController(MediaSourceController):
|
|||||||
duration=metadata.get("duration", 0) or 0,
|
duration=metadata.get("duration", 0) or 0,
|
||||||
status=metadata.get("status", ""),
|
status=metadata.get("status", ""),
|
||||||
)
|
)
|
||||||
|
|
||||||
def play(self) -> None:
|
def play(self) -> None:
|
||||||
if self._bt_service:
|
if self._bt_service:
|
||||||
self._bt_service.player_play()
|
self._bt_service.player_play()
|
||||||
|
|
||||||
def pause(self) -> None:
|
def pause(self) -> None:
|
||||||
if self._bt_service:
|
if self._bt_service:
|
||||||
self._bt_service.player_pause()
|
self._bt_service.player_pause()
|
||||||
|
|
||||||
def toggle_play(self) -> None:
|
def toggle_play(self) -> None:
|
||||||
if self._bt_service:
|
if self._bt_service:
|
||||||
self._bt_service.player_toggle()
|
self._bt_service.player_toggle()
|
||||||
|
|
||||||
def next_track(self) -> None:
|
def next_track(self) -> None:
|
||||||
if self._bt_service:
|
if self._bt_service:
|
||||||
self._bt_service.player_next()
|
self._bt_service.player_next()
|
||||||
|
|
||||||
def previous_track(self) -> None:
|
def previous_track(self) -> None:
|
||||||
if self._bt_service:
|
if self._bt_service:
|
||||||
self._bt_service.player_previous()
|
self._bt_service.player_previous()
|
||||||
@ -125,20 +125,20 @@ class BluetoothController(MediaSourceController):
|
|||||||
|
|
||||||
class CarPlayController(MediaSourceController):
|
class CarPlayController(MediaSourceController):
|
||||||
"""Контроллер для CarPlay (заглушка)."""
|
"""Контроллер для CarPlay (заглушка)."""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._volume = 50
|
self._volume = 50
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
return "CarPlay"
|
return "CarPlay"
|
||||||
|
|
||||||
def get_volume(self) -> int:
|
def get_volume(self) -> int:
|
||||||
return self._volume
|
return self._volume
|
||||||
|
|
||||||
def set_volume(self, value: int) -> None:
|
def set_volume(self, value: int) -> None:
|
||||||
self._volume = max(0, min(100, value))
|
self._volume = max(0, min(100, value))
|
||||||
|
|
||||||
def get_metadata(self) -> TrackMetadata:
|
def get_metadata(self) -> TrackMetadata:
|
||||||
# Заглушка - нет данных
|
# Заглушка - нет данных
|
||||||
return TrackMetadata(
|
return TrackMetadata(
|
||||||
@ -150,23 +150,23 @@ class CarPlayController(MediaSourceController):
|
|||||||
duration=0,
|
duration=0,
|
||||||
status="stopped",
|
status="stopped",
|
||||||
)
|
)
|
||||||
|
|
||||||
def play(self) -> None:
|
def play(self) -> None:
|
||||||
# TODO: реализовать через CarPlay API
|
# TODO: реализовать через CarPlay API
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def pause(self) -> None:
|
def pause(self) -> None:
|
||||||
# TODO: реализовать через CarPlay API
|
# TODO: реализовать через CarPlay API
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def toggle_play(self) -> None:
|
def toggle_play(self) -> None:
|
||||||
# TODO: реализовать через CarPlay API
|
# TODO: реализовать через CarPlay API
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def next_track(self) -> None:
|
def next_track(self) -> None:
|
||||||
# TODO: реализовать через CarPlay API
|
# TODO: реализовать через CarPlay API
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def previous_track(self) -> None:
|
def previous_track(self) -> None:
|
||||||
# TODO: реализовать через CarPlay API
|
# TODO: реализовать через CarPlay API
|
||||||
pass
|
pass
|
||||||
@ -177,76 +177,78 @@ class MediaController(QObject):
|
|||||||
Главный контроллер медиа.
|
Главный контроллер медиа.
|
||||||
Выбирает активный контроллер в зависимости от режима.
|
Выбирает активный контроллер в зависимости от режима.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
volume_changed = Signal(int)
|
volume_changed = Signal(int)
|
||||||
metadata_changed = Signal(object) # TrackMetadata
|
metadata_changed = Signal(object) # TrackMetadata
|
||||||
|
|
||||||
def __init__(self, bt_service: Any = None, parent: QObject = None):
|
def __init__(self, bt_service: Any = None, parent: QObject = None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self._bt_service = bt_service
|
self._bt_service = bt_service
|
||||||
self._controllers: dict[str, MediaSourceController] = {}
|
self._controllers: dict[str, MediaSourceController] = {}
|
||||||
self._current_mode: str = "bluetooth"
|
self._current_mode: str = "bluetooth"
|
||||||
|
|
||||||
# Регистрируем контроллеры
|
# Регистрируем контроллеры
|
||||||
self._register_controllers()
|
self._register_controllers()
|
||||||
|
|
||||||
def _register_controllers(self) -> None:
|
def _register_controllers(self) -> None:
|
||||||
"""Зарегистрировать все доступные контроллеры."""
|
"""Зарегистрировать все доступные контроллеры."""
|
||||||
self._controllers["bluetooth"] = BluetoothController(self._bt_service)
|
self._controllers["bluetooth"] = BluetoothController(self._bt_service)
|
||||||
self._controllers["carplay"] = CarPlayController()
|
self._controllers["carplay"] = CarPlayController()
|
||||||
# В будущем можно добавить:
|
# В будущем можно добавить:
|
||||||
# self._controllers["aux"] = AuxController()
|
# self._controllers["aux"] = AuxController()
|
||||||
|
|
||||||
def set_mode(self, mode: str) -> None:
|
def set_mode(self, mode: str) -> None:
|
||||||
"""Установить активный режим."""
|
"""Установить активный режим."""
|
||||||
if mode in self._controllers:
|
if mode in self._controllers:
|
||||||
# Если переключаемся с Bluetooth на CarPlay - ставим паузу
|
# Если переключаемся с Bluetooth на CarPlay - ставим паузу
|
||||||
if self._current_mode == "bluetooth" and mode == "carplay":
|
if self._current_mode == "bluetooth" and mode == "carplay":
|
||||||
self._controllers["bluetooth"].pause()
|
self._controllers["bluetooth"].pause()
|
||||||
|
elif self._current_mode == "carplay" and mode == "bluetooth":
|
||||||
|
self._controllers["bluetooth"].play()
|
||||||
self._current_mode = mode
|
self._current_mode = mode
|
||||||
# Сигнал о смене режима для обновления UI
|
# Сигнал о смене режима для обновления UI
|
||||||
self.metadata_changed.emit(self.get_metadata())
|
self.metadata_changed.emit(self.get_metadata())
|
||||||
|
|
||||||
def get_mode(self) -> str:
|
def get_mode(self) -> str:
|
||||||
"""Получить текущий режим."""
|
"""Получить текущий режим."""
|
||||||
return self._current_mode
|
return self._current_mode
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _current_controller(self) -> MediaSourceController:
|
def _current_controller(self) -> MediaSourceController:
|
||||||
"""Получить активный контроллер."""
|
"""Получить активный контроллер."""
|
||||||
return self._controllers.get(self._current_mode, self._controllers["bluetooth"])
|
return self._controllers.get(self._current_mode, self._controllers["bluetooth"])
|
||||||
|
|
||||||
# === Делегирующие методы ===
|
# === Делегирующие методы ===
|
||||||
|
|
||||||
def get_volume(self) -> int:
|
def get_volume(self) -> int:
|
||||||
"""Получить текущую громкость."""
|
"""Получить текущую громкость."""
|
||||||
return self._current_controller.get_volume()
|
return self._current_controller.get_volume()
|
||||||
|
|
||||||
def set_volume(self, value: int) -> None:
|
def set_volume(self, value: int) -> None:
|
||||||
"""Установить громкость."""
|
"""Установить громкость."""
|
||||||
self._current_controller.set_volume(value)
|
self._current_controller.set_volume(value)
|
||||||
self.volume_changed.emit(value)
|
self.volume_changed.emit(value)
|
||||||
|
|
||||||
def get_metadata(self) -> TrackMetadata:
|
def get_metadata(self) -> TrackMetadata:
|
||||||
"""Получить метаданные текущего трека."""
|
"""Получить метаданные текущего трека."""
|
||||||
return self._current_controller.get_metadata()
|
return self._current_controller.get_metadata()
|
||||||
|
|
||||||
def play(self) -> None:
|
def play(self) -> None:
|
||||||
"""Воспроизведение."""
|
"""Воспроизведение."""
|
||||||
self._current_controller.play()
|
self._current_controller.play()
|
||||||
|
|
||||||
def pause(self) -> None:
|
def pause(self) -> None:
|
||||||
"""Пауза."""
|
"""Пауза."""
|
||||||
self._current_controller.pause()
|
self._current_controller.pause()
|
||||||
|
|
||||||
def toggle_play(self) -> None:
|
def toggle_play(self) -> None:
|
||||||
"""Переключить воспроизведение/пауза."""
|
"""Переключить воспроизведение/пауза."""
|
||||||
self._current_controller.toggle_play()
|
self._current_controller.toggle_play()
|
||||||
|
|
||||||
def next_track(self) -> None:
|
def next_track(self) -> None:
|
||||||
"""Следующий трек."""
|
"""Следующий трек."""
|
||||||
self._current_controller.next_track()
|
self._current_controller.next_track()
|
||||||
|
|
||||||
def previous_track(self) -> None:
|
def previous_track(self) -> None:
|
||||||
"""Предыдущий трек."""
|
"""Предыдущий трек."""
|
||||||
self._current_controller.previous_track()
|
self._current_controller.previous_track()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user