I am writing a music database manager using python and tkinter. It is nearly all complete but I am stuck on playing songs from a list. The native macOS player NSSound has a built-in callback to a delegate which signals the end of the track so I would like to use that to trigger a virtual event back to the main app to send the next song to play. But I cannot figure out how make the callback work and would appreciate some help (this being just a hobby project which has got somewhat out of hand).
This skeleton code shows the relevant structure of the app and plays the selected file as expected, but none of my numerous attempts at formulating the delegate have worked. As I understand it the player appoints the Delegate class as its delegate, and then this class should have a callback method 'sound' to receive a message when the song ends. I have tried to figure it out from the Apple Developer protocol, and also searched extensively and found only one example.
How do I do get to the 'Success!' message please?
from AppKit import NSSound
import tkinter as tk
from tkinter import filedialog
class Delegate (NSSound):
def init(self):
self = super(Delegate, self).init()
return self
#def sound (...) ??? #this should fire when the song finishes?
# print('Success!')
class App(tk.Tk): #representing the rest of the music database app
def __init__(self):
super().__init__()
file = filedialog.askopenfilename()
song = NSSound.alloc()
song.initWithContentsOfFile_byReference_(file, True)
delegate = Delegate.alloc().init()
song.setDelegate_(delegate)
song.play()
if __name__ == "__main__":
app = App()
app.mainloop()