Better way to do skip to previous with AVQueuePlayer?

Related searches

I am using an AVQueuePlayer in my app. I have a two swipe gestures to skip to next and skip to previous avplayeritems. Right now to do skip to next I am just calling advanceToNextItem on the avqueueplayer which works well.

However the skip to previous I am removing all items and adding them back in with the previous video up front, this is really slow when skipping to previous multiple times. How can I make this faster just like calling advanceToNextItem?

My code looks like this:

func skipToPrevious() {
    queuePlayer.removeAllItems()
    // move the previous playerItem to the front of the list then add them all back in
    for playerItem in playerItems:
        queuePlayer.insertItem(playerItem, afterItem: nil)

}

It seems like AVQueuePlayer removes the current item from the play queue when calling advanceToNextItem. Theoretically, there is no way to get this item back without rebuilding the queue.

What you could do is use a standard AVPlayer, have an array of AVPlayerItems, and an integer index which keeps the index of the current track.

Swift 3:

let player = AVPlayer()
let playerItems = [AVPlayerItem]() // your array of items
var currentTrack = 0

func previousTrack() {
    if currentTrack - 1 < 0 {
        currentTrack = (playerItems.count - 1) < 0 ? 0 : (playerItems.count - 1)
    } else {
        currentTrack -= 1
    }

    playTrack()
}

func nextTrack() {
    if currentTrack + 1 > playerItems.count {
        currentTrack = 0
    } else {
        currentTrack += 1;
    }

    playTrack()
}

func playTrack() {

    if playerItems.count > 0 {
        player.replaceCurrentItem(with: playerItems[currentTrack])
        player.play()
    }
}

Swift 2.x:

func previousTrack() {
    if currentTrack-- < 0 {
        currentTrack = (playerItems.count - 1) < 0 ? 0 : (playerItems.count - 1)
    } else {
        currentTrack--
    }

    playTrack()
}

func nextTrack() {
    if currentTrack++ > playerItems.count {
        currentTrack = 0
    } else {
        currentTrack++;
    }

    playTrack()
}

func playTrack() {

    if playerItems.count > 0 {
        player.replaceCurrentItemWithPlayerItem(playerItems[currentTrack])
        player.play()
    }
}

ios - Better way to do skip to previous with AVQueuePlayer?, It seems like AVQueuePlayer removes the current item from the play queue when calling advanceToNextItem . Theoretically, there is no way to get this item back� class AVQueuePlayer: AVPlayer Overview Use this class to create and manage a queue of player items composed of local or progressively downloaded file-based media, such as QuickTime movies or MP3 audio files, as well as media served using HTTP Live Streaming.

does the queue handle more than one skip forward smoothly? if so, you could constantly re-insert the previous video back into the queue at index n+2. when the user wishes to play the previous track, you would skip forward twice.

if playing from track A to F without any skips, the pattern would look like this:

A B C D E F
B C D E F

// re-insert A after next track
B C A D E F
C A D E F

// remove A then re-insert B
C D E F
C D B E F
D B E F

// remove B then re-insert C
D E F
D E C F
E C F

// remove C then re-insert D
E F
E F D
F D

// remove D then re-insert E
F
FE

using this pattern you could only smoothly skip backwards once, but it could be modified to allow more.

definitely not an ideal solution, but may work!

ios: Better way to do skip to previous with AVQueuePlayer?, I am using an AVQueuePlayer in my app. I have a two swipe gestures to skip to next and skip to previous avplayeritems. Right now to do skip to� Hi everyone, I have a collection of clips with AVQueuePlayer and I need some feattures: - Transitions between clips - Move in the timeline rewind and

the replaceCurrentItemWithPlayerItem had limitation and should be avoided when possible, in Apple's document, it states "The new item must have the same compositor as the item it replaces, or have no compositor."

instead insert the playerItems one by one using a loop, just create an AVQueuePlayer would be faster:

func skipToPrevious() {
    queuePlayer = AVQueuePlayer.queuePlayerWithItems(playerItem)
}

Better way to do skip to previous with AVQueuePlayer?, I am using an AVQueuePlayer in my app. I have a two swipe gestures to skip to next and skip to previous avplayeritems. Right now to do skip to next I am just� Restore a previous version. Ines sees Todd’s message and realizes her mistake. She opens the research paper in Word and clicks the title. In the drop-down, she clicks See all versions under Version History and can quickly navigate and open a version of the research paper before she made the unwanted changes.

I am thinking a very different approach which is in fact in terms of advanceToNextItem method. You said that advanceToNextItem works fine. So I am wondering if you can implement the skip to previous using advanceToNextItem itself but by pointing the queue two items backwards of the current playing item.

E.g. if you your queue is this and the bold one is the current item

A B C D E F G H

Then set the current item to C and then use advanceToNextItem so that it plays D.

Not sure how your advanceToNextItem is implemented though. So it depends on that.

Skip to Previous AVPlayerItem on , If you want your program can play previous item and play the selected item from Better way to do skip to previous with AVQueuePlayer , However the skip to� I am wondering if the following is really the best way to know when a video is ready to play: Continue reading avfoundation , avqueueplayer , ios , swift , video

You can replace your current item with the previous and add the item, that was replaced:

let previousItem = AVPlayerItem(url: url )
let currentItem = AVPlayerItem(url: currentUrl)

 player.replaceCurrentItem(with: previousItem)
    if player.canInsert(currentItem, after: previousItem) {
         player.insert(currentItem, after: previousItem)
    }

// OR like this

 let previousItem = AVPlayerItem(url: url )
 if let currentItem = player.currentItem {
   player.replaceCurrentItem(with: previousItem)
     if player.canInsert(currentItem, after: previousItem) {
         player.insert(currentItem, after: previousItem)
     }
  }

If you want your program can play previous item and play the selected item from your Better way to do skip to previous with AVQueuePlayer , Skip to Previous� Hopefully, you’ve never experienced anything like this. But, in case you someday do, let me start by assuring you that you can untangle yourself from this horrifying scenario. All you have to do is take the following steps. 1. Stay Calm. When handling a crisis, the first step is to avoid panicking. Sure, it’s easier said than done.

You can't have summer without a shrimp boil or a backyard meal, and luckily this crowd-pleaser is a perfect al fresco meal. This classic shrimp boil recipe is a wonderful, easy way to prepare fresh shrimp. Don't forget the newspaper, because things are about to get messy.

AVQueuePlayer() Default constructor that initializes a new instance of this class with no parameters. AVQueuePlayer(AVPlayerItem[]) AVQueuePlayer(NSObjectFlag) Constructor to call on derived classes to skip initialization and merely allocate the object. AVQueuePlayer(IntPtr)

AVQueuePlayer(AVPlayerItem[]) AVQueuePlayer(IntPtr) A constructor used when creating managed representations of unmanaged objects; Called by the runtime. AVQueuePlayer(NSObjectFlag) Constructor to call on derived classes to skip initialization and merely allocate the object.

Comments
  • I have been facing this problem for a while now as well. I have resorted to recreating the queue player upon a user tapping for previous item. I then use a delay function, and a progress bar on the screen to allow for the queue player to have at least 1.0 second to recreate the queue and buffer a bit of this item. Of course this doesn't solve your problem of wanting faster speed. I will dig around a bit more.
  • The best I have found so far is to hold the avplayeritem in memory after it is done playing so this way it is already buffered then just reinsert the items like I did in the question rather than recreating the queue all together.
  • An idea -> when a item is playing, store the currently playing item in variable and load it into an AVPlayer. When user taps rewind, you can immediately play the AVPlayer, and then you can simply pause and rewind the AVQueuePlayer behind the scenes, so that when the AVPlayer finishes, the queue player is ready to go... A bit hacky sure
  • github.com/dgiovann/AVQueuePlayerPrevious ...a bit old but this may help you
  • This is what I was thinking but was trying to avoid. Is there any example of the source of avqueueplayer? I'd like to build off that
  • @Tyler the source of AVQueuePlayer? I don't think anyone has that as it's internal to Apple. What I can provide for you are the private headers for AVQueuePlayer and AVQueuePlayerInternal.
  • This is the best solution 100%. Key is to keep the avplayeritems in memory rather than reloading them on the skip to previous. Just need to be careful not too many are kept in memory and some are thrown away eventually. Thanks!
  • @Sulthan What if you're on track 0? If you're on the first track and try to go back, you'll go out of bounds unless you go tot he last item in the playlist. This was also quick stub code.
  • @JAL Your second version is much better. I would probably use let previous = (currentTrack - 1 + playerItems.count) % playerItems.count and let next = (currentTrack + 1) % playerItems.count but that's a matter of choice.
  • Why would this have any speed improvements? The reason it is slow is because it is not pre buffering the previous avplayeritem