Here's an example:
Code: Select all
EnableExplicit
ImportC "-framework AVKit" : EndImport
; taken from https://www.purebasic.fr/english/viewtopic.php?p=544211#p544211
Structure CMTime
value.q
timeScale.l
flags.l
CMTimeEpoch.q
EndStructure
Define status,duration.CMTime
Define player = CocoaMessage(0, 0, "AVQueuePlayer new")
Define url = CocoaMessage(0,0,"NSURL fileURLWithPath:$",@"/path/to/file.mp3")
Define playerItem = CocoaMessage(0,0,"AVPlayerItem playerItemWithURL:",url)
CocoaMessage(0,player,"insertItem:",playerItem,"afterItem:",0)
; waiting doesn't do anything
;Delay(1000)
; here the status will always be 0 since the item is not yet loaded
status = CocoaMessage(0,playerItem,"status")
CocoaMessage(@duration,playerItem,"duration")
MessageRequester("test","press ok to play, current item status is " + Str(status) + ", duration is " + Str(duration\value))
CocoaMessage(0,player,"play")
Delay(1000)
; here the status should be 1 since we started playing and gave it some time to load, we also can get a duration now
status = CocoaMessage(0,playerItem,"status")
CocoaMessage(@duration,playerItem,"duration")
MessageRequester("test","started playing, current item status is " + Str(status) + ", duration is " + Str(duration\value / duration\timeScale))
I tired looking into it, but it's all asynchronous and I have no idea about how to use such things in PB. Could anyone please help? Is it even possible?If you require inspecting an asset before you enqueue it for playback, call its load(_:) method to retrieve the values of one or more properties. Alternatively, you can tell the player item to automatically load the required properties by passing them to its init(asset:automaticallyLoadedAssetKeys:) initializer.