AVPlayerItem preload?
Posted: Sun Oct 08, 2023 10:36 pm
Hi, I've been playing with AVQueuePlayer a bit. It uses AVPlayerItem in order to queue items for playback, however it doesn't load added items immediately and waits until the last possible moment. Not only it adds a bit of a lag before it starts playing the next item, you are also not able to get the item information (like audio duration), which kinda ruins the purpose.
Here's an example:
So ideally you need to load the player item before the actual playback and the documentation actually says the following:
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.