Switched from playsound to vlc
This commit is contained in:
parent
a84b3ec01a
commit
5c7e9aeb19
61
clock.py
61
clock.py
|
@ -5,6 +5,7 @@ import math
|
|||
import sys
|
||||
import traceback
|
||||
import copy
|
||||
import os
|
||||
|
||||
from kivy.config import Config
|
||||
#Config.set('graphics', 'width', '800')
|
||||
|
@ -22,9 +23,8 @@ from kivy.clock import Clock
|
|||
from kivy.lang import Builder
|
||||
from kivy.graphics import Color, Line, Rectangle
|
||||
|
||||
from multiprocessing import Process
|
||||
from playsound import playsound
|
||||
import pulsectl
|
||||
import vlc
|
||||
|
||||
Builder.load_string('''
|
||||
<MyClockWidget>:
|
||||
|
@ -212,17 +212,10 @@ Builder.load_string('''
|
|||
|
||||
Position = collections.namedtuple('Position', 'x y')
|
||||
|
||||
global sound_process
|
||||
sound_process = None
|
||||
|
||||
def play_sound(source):
|
||||
while True:
|
||||
print("beep beep!")
|
||||
playsound(source)
|
||||
|
||||
class AlarmSettings():
|
||||
alarm_time = datetime.datetime(2022, 12, 10, 7, 30, 0, 0)
|
||||
alarm_activated = False
|
||||
alarm_playing = False
|
||||
|
||||
alarm_modified = False
|
||||
led_color = [0, 0, 0]
|
||||
|
@ -289,6 +282,32 @@ class MyClockWidget(FloatLayout):
|
|||
draw_list_curr_frame = []
|
||||
|
||||
touch_prev = Touch()
|
||||
vlc = vlc.Instance()
|
||||
player = vlc.media_player_new()
|
||||
|
||||
if (os.uname()[4][:3] == 'arm') or (os.uname()[4][:7] == 'aarch64'):
|
||||
is_arm = True
|
||||
else:
|
||||
is_arm = False
|
||||
|
||||
def play_sound(self, source):
|
||||
alarm_settings = App.get_running_app().alarm_settings
|
||||
|
||||
if (self.player.get_state() == vlc.State.NothingSpecial) or \
|
||||
(self.player.get_state() == vlc.State.Paused) or \
|
||||
(self.player.get_state() == vlc.State.Stopped) or \
|
||||
(self.player.get_state() == vlc.State.Ended) or \
|
||||
(self.player.get_state() == vlc.State.Error):
|
||||
print("beep beep! " + source)
|
||||
media = self.vlc.media_new(source)
|
||||
self.player.set_media(media)
|
||||
self.player.play()
|
||||
alarm_settings.alarm_playing = True
|
||||
|
||||
def stop_sound(self):
|
||||
self.player.stop()
|
||||
alarm_settings = App.get_running_app().alarm_settings
|
||||
alarm_settings.alarm_playing = False
|
||||
|
||||
def hide_widget(self, widget, hide=True):
|
||||
if hasattr(widget, 'saved_attrs'):
|
||||
|
@ -417,17 +436,14 @@ class MyClockWidget(FloatLayout):
|
|||
self.sun_rise()
|
||||
|
||||
def check_play_sound(self):
|
||||
global sound_process
|
||||
alarm_settings = App.get_running_app().alarm_settings
|
||||
|
||||
if alarm_settings.alarm_activated == False:
|
||||
return
|
||||
|
||||
if self.seconds_to_next_alarm < 0.1:
|
||||
print("Rise and shine baby!" + str(alarm_settings.sound_source))
|
||||
if alarm_settings.sound_source != "" and sound_process is None:
|
||||
sound_process = Process(target=play_sound, args=(alarm_settings.sound_source,))
|
||||
sound_process.start()
|
||||
if (self.seconds_to_next_alarm < 0.1) or (alarm_settings.alarm_playing == True):
|
||||
if alarm_settings.sound_source != "":
|
||||
self.play_sound(alarm_settings.sound_source)
|
||||
|
||||
def calc_seconds_to_next_alarm(self):
|
||||
alarm_settings = App.get_running_app().alarm_settings
|
||||
|
@ -756,8 +772,6 @@ class MyClockWidget(FloatLayout):
|
|||
super(MyClockWidget, self).on_touch_move(touch)
|
||||
|
||||
def on_touch_down(self, touch):
|
||||
global sound_process
|
||||
|
||||
alarm_settings = App.get_running_app().alarm_settings
|
||||
|
||||
time = alarm_settings.alarm_time
|
||||
|
@ -786,8 +800,7 @@ class MyClockWidget(FloatLayout):
|
|||
elif self.view == "settings_menu":
|
||||
pass
|
||||
elif self.view == "clock":
|
||||
if sound_process is not None:
|
||||
kill_sound_process()
|
||||
self.stop_sound()
|
||||
|
||||
super(MyClockWidget, self).on_touch_down(touch)
|
||||
|
||||
|
@ -814,14 +827,8 @@ class MyApp(App):
|
|||
Clock.schedule_interval(clock_widget.update_display, 1.0/update_rate)
|
||||
return clock_widget
|
||||
|
||||
def kill_sound_process():
|
||||
global sound_process
|
||||
if sound_process is not None:
|
||||
sound_process.kill()
|
||||
sound_process = None
|
||||
|
||||
def except_hook(type, value, tb):
|
||||
kill_sound_process()
|
||||
return
|
||||
|
||||
if __name__ == '__main__':
|
||||
# sys.excepthook = except_hook
|
||||
|
|
Loading…
Reference in New Issue