Search code examples
macosperformanceavplayerseektotime

AVPlayer seekToTime spinning pizza


I have a simple AVPlayer-based OS X app that plays local media. It has a skip forward and backward feature, based on -seekToTime:. On some media, there is an annoying 3-7 second delay in getting the media to continue playing (especially going forward). I have tried - seekToTime:toleranceBefore:toleranceAfter: , with variable tolerances. No luck.


Solution

  • Posting previously solved issue for the record... I noticed that the seekToTime: skipping worked fine when the playback was paused. I immediately (i.e., several weeks later) realized that it might make sense to stop the playback before seeking, then restarting. So far, problem is 100% solved, and it is blazing fast. Might be of some use to people trying to do smooth looping (but I don't know how to trigger the completion handler signaling the end of the loop). Don't know if it works with iOS. Sample code is attached:

    -(void) movePlayheadToASpecificTime
    {
        // assumes this is a method for an AVPlayerView subclass, properly wired with IB
        // self.player is a property of AVPlayerView, which points to an AVPlayer
    
        // save the current rate
        float currentPlayingRate = self.player.rate;
    
        // may need fancier tweaking if the time scale changes within asset
        int32_t timeScale = self.player.currentItem.asset.duration.timescale;
    
        // stop playback
        self.player.rate = 0;
    
        Float64 desiredTimeInSeconds = 4.5; // or whatever
    
        // convert desired time to CMTime
        CMTime target = CMTimeMakeWithSeconds(desiredTimeInSeconds, timeScale);
    
        // perform the move
        [self.player seekToTime:target];
    
        // restore playing rate
        self.player.rate = currentPlayingRate;
    
    }