Skip to content

Instantly share code, notes, and snippets.

@netgfx
Last active March 4, 2025 07:16
Show Gist options
  • Select an option

  • Save netgfx/42028481b49e33699ad048a6a2f87c64 to your computer and use it in GitHub Desktop.

Select an option

Save netgfx/42028481b49e33699ad048a6a2f87c64 to your computer and use it in GitHub Desktop.
Add observers to AVPlayer
// listening for current item change
self.audioQueueObserver = self.playerQueue?.observe(\.currentItem, options: [.new]) {
[weak self] (player, _) in
print("media item changed...")
}
// listening for current item status change
self.audioQueueStatusObserver = self.playerQueue?.currentItem?.observe(\.status, options: [.new, .old], changeHandler: {
(playerItem, change) in
if playerItem.status == .readyToPlay {
print("current item status is ready")
}
})
// listening for buffer is empty
self.audioQueueBufferEmptyObserver = self.playerQueue?.currentItem?.observe(\.playbackBufferEmpty, options: [.new]) {
[weak self] (_, _) in
print("buffering...")
}
// listening for event that buffer is almost full
self.audioQueueBufferAlmostThereObserver = self.playerQueue?.currentItem?.observe(\.playbackLikelyToKeepUp, options: [.new]) {
[weak self] (_, _) in
print("buffering ends...")
}
// listening for event that buffer is full
self.audioQueueBufferFullObserver = self.playerQueue?.currentItem?.observe(\.playbackBufferFull, options: [.new]) {
[weak self] (_, _) in
print("buffering is hidden...")
}
// listening for event about the status of the playback
self.audioQueueStallObserver = self.playerQueue?.observe(\.timeControlStatus, options: [.new, .old], changeHandler: {
(playerItem, change) in
if #available(iOS 10.0, *) {
switch (playerItem.timeControlStatus) {
case AVPlayerTimeControlStatus.paused:
print("Media Paused")
case AVPlayerTimeControlStatus.playing:
print("Media Playing")
case AVPlayerTimeControlStatus.waitingToPlayAtSpecifiedRate:
print("Media Waiting to play at specific rate!")
}
}
else {
// Fallback on earlier versions
}
})
// listening for change event when player stops playback
self.audioQueueWaitingObserver = self.playerQueue?.observe(\.reasonForWaitingToPlay, options: [.new, .old], changeHandler: {
(playerItem, change) in
if #available(iOS 10.0, *) {
print("REASON FOR WAITING TO PLAY: ", playerItem.reasonForWaitingToPlay?.rawValue as Any)
}
else {
// Fallback on earlier versions
}
})
@rockiesgrizzly
Copy link
Copy Markdown

These observer examples were helpful. Thanks.

@lsamaria
Copy link
Copy Markdown

this was very helpful!

@laura-digio
Copy link
Copy Markdown

very convenient, you rock!

@darkisa
Copy link
Copy Markdown

darkisa commented Aug 29, 2021

Very helpful!

@akshatk
Copy link
Copy Markdown

akshatk commented Oct 12, 2021

Very helpful. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment