You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

import threading
import time
import mido
from mido import MidiFile
from StoppableThread import StoppableThread
from Requestor import Requestor
class MediaPlayer:
def __init__(self, host, port):
self.requestor = Requestor(host, port)
self.song_thread = None
self.output = mido.open_output()
def request_song(self, song_name):
bytes_song = self.requestor.getSong(song_name)
with open('received_song.mid', 'wb') as file:
file.write(bytes_song)
if self.song_thread is not None and self.song_thread.is_alive():
self.song_thread.stop()
self.song_thread = StoppableThread(target=self.play_song, args=('received_song.mid',))
self.song_thread.start()
def play_song(self, song_path):
mid = MidiFile(song_path)
try:
for msg in mid.play():
if threading.current_thread().stopped():
break
self.output.send(msg)
except Exception as e:
print(e)
finally:
self.output.reset()
def stop_song(self):
if self.song_thread is not None and self.song_thread.is_alive():
self.song_thread.stop()

Powered by INFORMATICA.FP.EDU.ES.