Does AVQueuePlayer support asynchronously adding AVPlayerItems?
I have a video player that needs to play a sequence of videos from the network. The URLs for these videos are not known in advance, as they come from XML or JSON responses from other HTTP requests.
As the video URLs become known, I create AVPlayerItems
and 开发者_运维技巧add them to an AVQueuePlayer
. I observe the player status and the item status to determine which item is playing etc.
The problem is that the player seems to get into various weird states where it returns AVFoundationErrorDomain error -11800
and from then on the player refuses to play anything. Even after deallocating the AVQueuePlayer
and starting a new one, videos that previously played refuse to play.
Is adding new AVPlayerItems
to a playing AVQueuePlayer
a supported operation, or should I be doing this another way? If it is supported, what should I be looking out for/doing (Eg. thread issues? KVO issues?) to make this work?
The answer is yes, AVQueuePlayer
does work with adding AVPlayerItems
asynchronously. The important part is in the documentation:
AVPlayer serializes notifications of changes that occur dynamically during playback on a dispatch queue. By default, this queue is the main queue (see dispatch_get_main_queue). To ensure safe access to a player’s nonatomic properties while dynamic changes in playback state may be reported, you must serialize access with the receiver’s notification queue. In the common case, such serialization is naturally achieved by invoking AVPlayer’s various methods on the main thread or queue.
Since KVO observers might fire on other threads, it's critical to call all methods on AVPlayer
(and AVQueuePlayer
) from the main thread.
If you don't, you'll see the weird behaviour and AVFoundationErrorDomain -11800
as things get into inconsistent states
精彩评论