Looping a video with AVFoundation AVPlayer?









up vote
128
down vote

favorite
62












Is there a relatively easy way of looping a video in AVFoundation?



I've created my AVPlayer and AVPlayerLayer like so:



avPlayer = [[AVPlayer playerWithURL:videoUrl] retain];
avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];

avPlayerLayer.frame = contentView.layer.bounds;
[contentView.layer addSublayer: avPlayerLayer];


and then I play my video with:



[avPlayer play];


The video plays fine but stops at the end. With the MPMoviePlayerController all you have to do is set its repeatMode property to the right value. There doesn't appear to be a similar property on AVPlayer. There also doesn't seem to be a callback that will tell me when the movie has finished so I can seek to the beginning and play it again.



I'm not using MPMoviePlayerController because it has some serious limitations. I want to be able to play back multiple video streams at once.










share|improve this question



















  • 1




    See this answer for a link to actual working code: stackoverflow.com/questions/7822808/…
    – MoDJ
    Aug 19 '13 at 17:39














up vote
128
down vote

favorite
62












Is there a relatively easy way of looping a video in AVFoundation?



I've created my AVPlayer and AVPlayerLayer like so:



avPlayer = [[AVPlayer playerWithURL:videoUrl] retain];
avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];

avPlayerLayer.frame = contentView.layer.bounds;
[contentView.layer addSublayer: avPlayerLayer];


and then I play my video with:



[avPlayer play];


The video plays fine but stops at the end. With the MPMoviePlayerController all you have to do is set its repeatMode property to the right value. There doesn't appear to be a similar property on AVPlayer. There also doesn't seem to be a callback that will tell me when the movie has finished so I can seek to the beginning and play it again.



I'm not using MPMoviePlayerController because it has some serious limitations. I want to be able to play back multiple video streams at once.










share|improve this question



















  • 1




    See this answer for a link to actual working code: stackoverflow.com/questions/7822808/…
    – MoDJ
    Aug 19 '13 at 17:39












up vote
128
down vote

favorite
62









up vote
128
down vote

favorite
62






62





Is there a relatively easy way of looping a video in AVFoundation?



I've created my AVPlayer and AVPlayerLayer like so:



avPlayer = [[AVPlayer playerWithURL:videoUrl] retain];
avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];

avPlayerLayer.frame = contentView.layer.bounds;
[contentView.layer addSublayer: avPlayerLayer];


and then I play my video with:



[avPlayer play];


The video plays fine but stops at the end. With the MPMoviePlayerController all you have to do is set its repeatMode property to the right value. There doesn't appear to be a similar property on AVPlayer. There also doesn't seem to be a callback that will tell me when the movie has finished so I can seek to the beginning and play it again.



I'm not using MPMoviePlayerController because it has some serious limitations. I want to be able to play back multiple video streams at once.










share|improve this question















Is there a relatively easy way of looping a video in AVFoundation?



I've created my AVPlayer and AVPlayerLayer like so:



avPlayer = [[AVPlayer playerWithURL:videoUrl] retain];
avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];

avPlayerLayer.frame = contentView.layer.bounds;
[contentView.layer addSublayer: avPlayerLayer];


and then I play my video with:



[avPlayer play];


The video plays fine but stops at the end. With the MPMoviePlayerController all you have to do is set its repeatMode property to the right value. There doesn't appear to be a similar property on AVPlayer. There also doesn't seem to be a callback that will tell me when the movie has finished so I can seek to the beginning and play it again.



I'm not using MPMoviePlayerController because it has some serious limitations. I want to be able to play back multiple video streams at once.







ios objective-c swift avfoundation avplayer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 12:21









ARGeo

5,29052047




5,29052047










asked Mar 19 '11 at 9:05









orj

7,347105466




7,347105466







  • 1




    See this answer for a link to actual working code: stackoverflow.com/questions/7822808/…
    – MoDJ
    Aug 19 '13 at 17:39












  • 1




    See this answer for a link to actual working code: stackoverflow.com/questions/7822808/…
    – MoDJ
    Aug 19 '13 at 17:39







1




1




See this answer for a link to actual working code: stackoverflow.com/questions/7822808/…
– MoDJ
Aug 19 '13 at 17:39




See this answer for a link to actual working code: stackoverflow.com/questions/7822808/…
– MoDJ
Aug 19 '13 at 17:39












16 Answers
16






active

oldest

votes

















up vote
257
down vote



accepted










You can get a Notification when the player ends. Check AVPlayerItemDidPlayToEndTimeNotification



When setting up the player:



ObjC



 avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[avPlayer currentItem]];


this will prevent the player to pause at the end.



in the notification:



- (void)playerItemDidReachEnd:(NSNotification *)notification 
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];



this will rewind the movie.



Don't forget un unregister the notification when releasing the player.



Swift



NotificationCenter.default.addObserver(self,
selector: #selector(playerItemDidReachEnd(notification:)),
name: Notification.Name.AVPlayerItemDidPlayToEndTime,
object: avPlayer?.currentItem)

@objc func playerItemDidReachEnd(notification: Notification)
if let playerItem: AVPlayerItem = notification.object as? AVPlayerItem
playerItem.seek(to: kCMTimeZero, completionHandler: nil)







share|improve this answer


















  • 6




    ...and if you want to play it right after [p seekToTime:kCMTimeZero] (a "rewind" of sorts), simply do [p play] again.
    – thomax
    Dec 28 '11 at 11:31






  • 23




    this should not be necessary... if you do avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; it will not stop, so no need to set it to play again
    – Bastian
    Dec 31 '11 at 14:44






  • 2




    in my case it stops then.
    – headkit
    Aug 30 '12 at 15:05






  • 12




    This solution works, but it is not completely seamless. I have a very small pause. Am I doing something wrong?
    – Joris van Liempd iDeveloper
    Dec 21 '12 at 11:20






  • 3




    @Praxiteles you need to unregister it when the view ist destroyed, or when you remove the videoplayer or whatever you do ,) You can use [[NSNotificationCenter defaultCenter] removeObserver:self]; for example, when self is listening to the notifications.
    – Bastian
    Jun 24 '15 at 16:03

















up vote
42
down vote













If it helps, in iOS / tvOS 10, there's a new AVPlayerLooper() that you can use to create seamless looping of video (Swift):



player = AVQueuePlayer()
playerLayer = AVPlayerLayer(player: player)
playerItem = AVPlayerItem(url: videoURL)
playerLooper = AVPlayerLooper(player: player, templateItem: playerItem)
player.play()


This was presented at WWDC 2016 in "Advances in AVFoundation Playback":
https://developer.apple.com/videos/play/wwdc2016/503/



Even using this code, I had a hiccup until I filed a bug report with Apple and got this response:




The movie file having movie duration longer than audio/video tracks is
the problem. FigPlayer_File is disabling gapless transition because
audio track edit is shorter than the movie duration (15.682 vs
15.787).



You need to either fix the movie files to have the movie duration and
track durations to be same length or you can use the time range
parameter of AVPlayerLooper (set time range from 0 to duration of
audio track)




It turns out that Premiere had been exporting files with an audio track of a slightly different length than the video. In my case it was fine to remove the audio entirely, and that fixed the problem.






share|improve this answer


















  • 1




    Nothing else worked for me. I am using an AVPlayerLooper and had this bug and fixing the discrepancy between video/audio lengths solved the issue.
    – Kevin Heap
    Oct 6 '17 at 15:56






  • 2




    This also works in ObjC as well.
    – ObjSal
    Nov 30 '17 at 0:56






  • 1




    Thank you for that information about Premiere. I added a timeRange to the looper and that fixed my "flashing video" issue.
    – Alexander Flenniken
    Aug 29 at 8:31

















up vote
24
down vote













In Swift:



You can get a Notification when the player ends... check AVPlayerItemDidPlayToEndTimeNotification



when setting up the player:



avPlayer.actionAtItemEnd = AVPlayerActionAtItemEnd.None

NSNotificationCenter.defaultCenter().addObserver(self,
selector: "playerItemDidReachEnd:",
name: AVPlayerItemDidPlayToEndTimeNotification,
object: avPlayer.currentItem)


this will prevent the player to pause at the end.



in the notification:



func playerItemDidReachEnd(notification: NSNotification) 
if let playerItem: AVPlayerItem = notification.object as? AVPlayerItem
playerItem.seekToTime(kCMTimeZero)




Swift3



NotificationCenter.default.addObserver(self,
selector: #selector(PlaylistViewController.playerItemDidReachEnd),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: avPlayer?.currentItem)


this will rewind the movie.



Don't forget un unregister the notification when releasing the player.






share|improve this answer


















  • 4




    I'm seeing a small hiccup between loops with this method. I opened my video in Adobe Premier and verified there are no duplicate frames in the video, so the brief hiccup is definitely at playback. Has anybody found a way to make a video loop seamlessly in swift?
    – SpaceManGalaxy
    Oct 11 '15 at 22:45











  • @SpaceManGalaxy I’ve also noticed the hiccup. Have you found a way to fix this glitch?
    – Lance Samaria
    Jan 31 at 6:49

















up vote
14
down vote













Here's what I ended up doing to prevent the pause-hiccup issue:



Swift:



NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime,
object: nil,
queue: nil) [weak self] note in
self?.avPlayer.seek(to: kCMTimeZero)
self?.avPlayer.play()




Objective C:



__weak typeof(self) weakSelf = self; // prevent memory cycle
NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
[noteCenter addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note)
[weakSelf.avPlayer seekToTime:kCMTimeZero];
[weakSelf.avPlayer play];
];



NOTE: I didn't use avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone as it's not needed.






share|improve this answer


















  • 1




    @KostiaDombrovsky did you try on an actual device or different videos?
    – Islam Q.
    Apr 8 '15 at 7:20










  • @IslamQ. I record an MP4 file and then try to play it in a loop kinda like snapchat does.
    – Kostia Dombrovsky
    Apr 8 '15 at 7:22










  • @KostiaDombrovsky did you compare your playback with snapchat side-by-side? I think because the beginning and the ending frames don't match it seems as if it was paused, but it never pauses.
    – Islam Q.
    Apr 8 '15 at 9:25










  • Didn't work for me either. I have a 6 second video with ceaseless audio and I keep hearing a split second of silence with this method
    – Cbas
    Jan 24 '16 at 3:05










  • I am seeing a memory leak when using this approach. It has to do with the [weakSelf.avPlayer seekToTime:kCMTimeZero]; [weakSelf.avPlayer play]; lines - when I comment out these lines there is not longer a memory leak. I've profiled this in instruments.
    – Solsma Dev
    Nov 16 '16 at 16:39

















up vote
3
down vote













I recommend using AVQueuePlayer to loop your videos seamlessly. Add the notification observer



AVPlayerItemDidPlayToEndTimeNotification


and in its selector, loop your video



AVPlayerItem *video = [[AVPlayerItem alloc] initWithURL:videoURL];
[self.player insertItem:video afterItem:nil];
[self.player play];





share|improve this answer




















  • I tried this and it does not show any improvement over the method @Bastian suggested. Did you manage to totally remove the hiccup with this ?
    – amadour
    Sep 29 '14 at 19:32






  • 2




    @amadour what you can do is add 2 of the same videos in the AVQueuePlayer player when initialized and when the player posts the AVPlayerItemDidPlayToEndTimeNotification, add the same video to the player's queue.
    – kevnguy
    Sep 30 '14 at 4:43

















up vote
3
down vote













To avoid the gap when the video is rewound, using multiple copies of the same asset in a composition worked well for me. I found it here: www.developers-life.com/avplayer-looping-video-without-hiccupdelays.html (link now dead).



AVURLAsset *tAsset = [AVURLAsset assetWithURL:tURL];
CMTimeRange tEditRange = CMTimeRangeMake(CMTimeMake(0, 1), CMTimeMake(tAsset.duration.value, tAsset.duration.timescale));
AVMutableComposition *tComposition = [[[AVMutableComposition alloc] init] autorelease];
for (int i = 0; i < 100; i++) // Insert some copies.
[tComposition insertTimeRange:tEditRange ofAsset:tAsset atTime:tComposition.duration error:nil];

AVPlayerItem *tAVPlayerItem = [[AVPlayerItem alloc] initWithAsset:tComposition];
AVPlayer *tAVPlayer = [[AVPlayer alloc] initWithPlayerItem:tAVPlayerItem];





share|improve this answer






















  • I guess you mean this link devbrief.blogspot.se/2011/12/…
    – flame3
    Aug 31 '16 at 9:35

















up vote
2
down vote













For Swift 3 & 4



NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.avPlayer?.currentItem, queue: .main) _ in
self.avPlayer?.seek(to: kCMTimeZero)
self.avPlayer?.play()






share|improve this answer



























    up vote
    1
    down vote













    this worked for me without hiccup issues, point is in pausing the player before calling seekToTime method:




    1. init AVPlayer



      let url = NSBundle.mainBundle().URLForResource("loop", withExtension: "mp4")
      let playerItem = AVPlayerItem(URL: url!)

      self.backgroundPlayer = AVPlayer(playerItem: playerItem)
      let playerLayer = AVPlayerLayer(player: self.backgroundPlayer)

      playerLayer.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)
      self.layer.addSublayer(playerLayer)
      self.backgroundPlayer!.actionAtItemEnd = .None
      self.backgroundPlayer!.play()



    2. registering notification



      NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoLoop", name: AVPlayerItemDidPlayToEndTimeNotification, object: self.backgroundPlayer!.currentItem)



    3. videoLoop function



      func videoLoop() 
      self.backgroundPlayer?.pause()
      self.backgroundPlayer?.currentItem?.seekToTime(kCMTimeZero)
      self.backgroundPlayer?.play()







    share|improve this answer
















    • 3




      Thanks — I tried this, but there is still a pause for me.
      – Nabha
      Feb 8 '16 at 7:04

















    up vote
    0
    down vote













    After loading the video into the AVPlayer (via its AVPlayerItem, of course):



     [self addDidPlayToEndTimeNotificationForPlayerItem:item];


    The addDidPlayToEndTimeNotificationForPlayerItem method:



    - (void)addDidPlayToEndTimeNotificationForPlayerItem:(AVPlayerItem *)item

    if (_notificationToken)
    _notificationToken = nil;

    /*
    Setting actionAtItemEnd to None prevents the movie from getting paused at item end. A very simplistic, and not gapless, looped playback.
    */
    _player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
    _notificationToken = [[NSNotificationCenter defaultCenter] addObserverForName:AVPlayerItemDidPlayToEndTimeNotification object:item queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note)
    // Simple item playback rewind.
    [[_player currentItem] seekToTime:kCMTimeZero];
    ];



    In your viewWillDisappear method:



    if (_notificationToken) 
    [[NSNotificationCenter defaultCenter] removeObserver:_notificationToken name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem];
    _notificationToken = nil;



    In your view controller's interface declaration within the implementation file:



    id _notificationToken;


    Need to see this up-and-running before you try? Download and run this sample app:



    https://developer.apple.com/library/prerelease/ios/samplecode/AVBasicVideoOutput/Listings/AVBasicVideoOutput_APLViewController_m.html#//apple_ref/doc/uid/DTS40013109-AVBasicVideoOutput_APLViewController_m-DontLinkElementID_8



    In my app, which uses this very code, there is no pause whatsoever between the end of the video and the beginning. In fact, depending on the video, there's no way for me to tell the video is at the beginning again, save the timecode display.






    share|improve this answer



























      up vote
      0
      down vote













      you can add a AVPlayerItemDidPlayToEndTimeNotification observer and replay video
      from start in selector, code like below



       //add observer
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification
      object:_aniPlayer.currentItem];

      -(void)playbackFinished:(NSNotification *)notification
      [_aniPlayer seekToTime:CMTimeMake(0, 1)];//replay from start
      [_aniPlayer play];






      share|improve this answer



























        up vote
        0
        down vote













        my solution in objective-c wth AVQueuePlayer - it seems you have to duplicate the AVPlayerItem and upon finishing playback of first element instantly add another copy. "Kind of" makes sense and works for me without any hiccup



        NSURL *videoLoopUrl; 
        // as [[NSBundle mainBundle] URLForResource:@"assets/yourVideo" withExtension:@"mp4"]];
        AVQueuePlayer *_loopVideoPlayer;

        +(void) nextVideoInstance:(NSNotification*)notif

        AVPlayerItem *currItem = [AVPlayerItem playerItemWithURL: videoLoopUrl];

        [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(nextVideoInstance:)
        name:AVPlayerItemDidPlayToEndTimeNotification
        object: currItem];

        [_loopVideoPlayer insertItem:currItem afterItem:nil];
        [_loopVideoPlayer advanceToNextItem];



        +(void) initVideoPlayer
        videoCopy1 = [AVPlayerItem playerItemWithURL: videoLoopUrl];
        videoCopy2 = [AVPlayerItem playerItemWithURL: videoLoopUrl];
        NSArray <AVPlayerItem *> *dummyArray = [NSArray arrayWithObjects: videoCopy1, videoCopy2, nil];
        _loopVideoPlayer = [AVQueuePlayer queuePlayerWithItems: dummyArray];

        [[NSNotificationCenter defaultCenter] addObserver: self
        selector: @selector(nextVideoInstance:)
        name: AVPlayerItemDidPlayToEndTimeNotification
        object: videoCopy1];

        [[NSNotificationCenter defaultCenter] addObserver: self
        selector: @selector(nextVideoInstance:)
        name: AVPlayerItemDidPlayToEndTimeNotification
        object: videoCopy2];



        https://gist.github.com/neonm3/06c3b5c911fdd3ca7c7800dccf7202ad






        share|improve this answer






















        • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
          – pmichna
          Aug 22 '17 at 12:41

















        up vote
        0
        down vote













        The following is working for me in WKWebView in swift 4.1
        The main part of the WKWebView in WKwebviewConfiguration



        wkwebView.navigationDelegate = self
        wkwebView.allowsBackForwardNavigationGestures = true
        self.wkwebView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
        let config = WKWebViewConfiguration()
        config.allowsInlineMediaPlayback = true
        wkwebView = WKWebView(frame: wkwebView.frame, configuration: config)
        self.view.addSubview(wkwebView)
        self.wkwebView.load(NSURLRequest(url: URL(string: self.getUrl())!) as URLRequest)





        share|improve this answer



























          up vote
          0
          down vote













          What I did is to make it loop playing, like my code below:



          [player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0)
          queue:dispatch_get_main_queue() usingBlock:^(CMTime time)
          float current = CMTimeGetSeconds(time);
          float total = CMTimeGetSeconds([playerItem duration]);
          if (current >= total)
          [[self.player currentItem] seekToTime:kCMTimeZero];
          [self.player play];

          ];





          share|improve this answer





























            up vote
            0
            down vote













            Swift 4.2 in Xcode 10.1.



            Yes, there is a relatively easy way of looping a video in AVKit/AVFoundation using AVQueuePlayer(), Key-Value Observation (KVO) technique and a token for it.



            This definitely works for a bunch of H.264/HEVC videos with a minimal burden for CPU.



            Here's a code:



            import UIKit
            import AVFoundation
            import AVKit

            class ViewController: UIViewController

            private let player = AVQueuePlayer()
            let clips = ["01", "02", "03", "04", "05", "06", "07"]
            private var token: NSKeyValueObservation?
            var avPlayerView = AVPlayerViewController()

            override func viewDidAppear(_ animated: Bool)
            super.viewDidAppear(true)

            self.addAllVideosToPlayer()
            present(avPlayerView, animated: true, completion: self.player.play() )


            func addAllVideosToPlayer()
            avPlayerView.player = player

            for clip in clips
            let urlPath = Bundle.main.path(forResource: clip, ofType: "m4v")!
            let url = URL(fileURLWithPath: urlPath)
            let playerItem = AVPlayerItem(url: url)
            player.insert(playerItem, after: player.items().last)

            token = player.observe(.currentItem) [weak self] player, _ in
            if self!.player.items().count == 1 self?.addAllVideosToPlayer()









            share|improve this answer





























              up vote
              -1
              down vote













              use AVPlayerViewController below code, its working for me



               let type : String! = "mp4"
              let targetURL : String? = NSBundle.mainBundle().pathForResource("Official Apple MacBook Air Video YouTube", ofType: "mp4")

              let videoURL = NSURL(fileURLWithPath:targetURL!)


              let player = AVPlayer(URL: videoURL)
              let playerController = AVPlayerViewController()

              playerController.player = player
              self.addChildViewController(playerController)
              self.playView.addSubview(playerController.view)
              playerController.view.frame = playView.bounds

              player.play()


              All controls to be showed, hope its helpful






              share|improve this answer



























                up vote
                -2
                down vote













                /* "numberOfLoops" is the number of times that the sound will return to the beginning upon reaching the end. 
                A value of zero means to play the sound just once.
                A value of one will result in playing the sound twice, and so on..
                Any negative number will loop indefinitely until stopped.
                */
                @property NSInteger numberOfLoops;


                This property is already defined inside AVAudioPlayer. Hope this can help you.
                I'm using Xcode 6.3.






                share|improve this answer


















                • 8




                  that's for audio, not for AVPlayer
                  – Yariv
                  May 21 '15 at 21:23










                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5361145%2flooping-a-video-with-avfoundation-avplayer%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                16 Answers
                16






                active

                oldest

                votes








                16 Answers
                16






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                257
                down vote



                accepted










                You can get a Notification when the player ends. Check AVPlayerItemDidPlayToEndTimeNotification



                When setting up the player:



                ObjC



                 avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

                [[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(playerItemDidReachEnd:)
                name:AVPlayerItemDidPlayToEndTimeNotification
                object:[avPlayer currentItem]];


                this will prevent the player to pause at the end.



                in the notification:



                - (void)playerItemDidReachEnd:(NSNotification *)notification 
                AVPlayerItem *p = [notification object];
                [p seekToTime:kCMTimeZero];



                this will rewind the movie.



                Don't forget un unregister the notification when releasing the player.



                Swift



                NotificationCenter.default.addObserver(self,
                selector: #selector(playerItemDidReachEnd(notification:)),
                name: Notification.Name.AVPlayerItemDidPlayToEndTime,
                object: avPlayer?.currentItem)

                @objc func playerItemDidReachEnd(notification: Notification)
                if let playerItem: AVPlayerItem = notification.object as? AVPlayerItem
                playerItem.seek(to: kCMTimeZero, completionHandler: nil)







                share|improve this answer


















                • 6




                  ...and if you want to play it right after [p seekToTime:kCMTimeZero] (a "rewind" of sorts), simply do [p play] again.
                  – thomax
                  Dec 28 '11 at 11:31






                • 23




                  this should not be necessary... if you do avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; it will not stop, so no need to set it to play again
                  – Bastian
                  Dec 31 '11 at 14:44






                • 2




                  in my case it stops then.
                  – headkit
                  Aug 30 '12 at 15:05






                • 12




                  This solution works, but it is not completely seamless. I have a very small pause. Am I doing something wrong?
                  – Joris van Liempd iDeveloper
                  Dec 21 '12 at 11:20






                • 3




                  @Praxiteles you need to unregister it when the view ist destroyed, or when you remove the videoplayer or whatever you do ,) You can use [[NSNotificationCenter defaultCenter] removeObserver:self]; for example, when self is listening to the notifications.
                  – Bastian
                  Jun 24 '15 at 16:03














                up vote
                257
                down vote



                accepted










                You can get a Notification when the player ends. Check AVPlayerItemDidPlayToEndTimeNotification



                When setting up the player:



                ObjC



                 avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

                [[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(playerItemDidReachEnd:)
                name:AVPlayerItemDidPlayToEndTimeNotification
                object:[avPlayer currentItem]];


                this will prevent the player to pause at the end.



                in the notification:



                - (void)playerItemDidReachEnd:(NSNotification *)notification 
                AVPlayerItem *p = [notification object];
                [p seekToTime:kCMTimeZero];



                this will rewind the movie.



                Don't forget un unregister the notification when releasing the player.



                Swift



                NotificationCenter.default.addObserver(self,
                selector: #selector(playerItemDidReachEnd(notification:)),
                name: Notification.Name.AVPlayerItemDidPlayToEndTime,
                object: avPlayer?.currentItem)

                @objc func playerItemDidReachEnd(notification: Notification)
                if let playerItem: AVPlayerItem = notification.object as? AVPlayerItem
                playerItem.seek(to: kCMTimeZero, completionHandler: nil)







                share|improve this answer


















                • 6




                  ...and if you want to play it right after [p seekToTime:kCMTimeZero] (a "rewind" of sorts), simply do [p play] again.
                  – thomax
                  Dec 28 '11 at 11:31






                • 23




                  this should not be necessary... if you do avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; it will not stop, so no need to set it to play again
                  – Bastian
                  Dec 31 '11 at 14:44






                • 2




                  in my case it stops then.
                  – headkit
                  Aug 30 '12 at 15:05






                • 12




                  This solution works, but it is not completely seamless. I have a very small pause. Am I doing something wrong?
                  – Joris van Liempd iDeveloper
                  Dec 21 '12 at 11:20






                • 3




                  @Praxiteles you need to unregister it when the view ist destroyed, or when you remove the videoplayer or whatever you do ,) You can use [[NSNotificationCenter defaultCenter] removeObserver:self]; for example, when self is listening to the notifications.
                  – Bastian
                  Jun 24 '15 at 16:03












                up vote
                257
                down vote



                accepted







                up vote
                257
                down vote



                accepted






                You can get a Notification when the player ends. Check AVPlayerItemDidPlayToEndTimeNotification



                When setting up the player:



                ObjC



                 avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

                [[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(playerItemDidReachEnd:)
                name:AVPlayerItemDidPlayToEndTimeNotification
                object:[avPlayer currentItem]];


                this will prevent the player to pause at the end.



                in the notification:



                - (void)playerItemDidReachEnd:(NSNotification *)notification 
                AVPlayerItem *p = [notification object];
                [p seekToTime:kCMTimeZero];



                this will rewind the movie.



                Don't forget un unregister the notification when releasing the player.



                Swift



                NotificationCenter.default.addObserver(self,
                selector: #selector(playerItemDidReachEnd(notification:)),
                name: Notification.Name.AVPlayerItemDidPlayToEndTime,
                object: avPlayer?.currentItem)

                @objc func playerItemDidReachEnd(notification: Notification)
                if let playerItem: AVPlayerItem = notification.object as? AVPlayerItem
                playerItem.seek(to: kCMTimeZero, completionHandler: nil)







                share|improve this answer














                You can get a Notification when the player ends. Check AVPlayerItemDidPlayToEndTimeNotification



                When setting up the player:



                ObjC



                 avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

                [[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(playerItemDidReachEnd:)
                name:AVPlayerItemDidPlayToEndTimeNotification
                object:[avPlayer currentItem]];


                this will prevent the player to pause at the end.



                in the notification:



                - (void)playerItemDidReachEnd:(NSNotification *)notification 
                AVPlayerItem *p = [notification object];
                [p seekToTime:kCMTimeZero];



                this will rewind the movie.



                Don't forget un unregister the notification when releasing the player.



                Swift



                NotificationCenter.default.addObserver(self,
                selector: #selector(playerItemDidReachEnd(notification:)),
                name: Notification.Name.AVPlayerItemDidPlayToEndTime,
                object: avPlayer?.currentItem)

                @objc func playerItemDidReachEnd(notification: Notification)
                if let playerItem: AVPlayerItem = notification.object as? AVPlayerItem
                playerItem.seek(to: kCMTimeZero, completionHandler: nil)








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 2 '17 at 8:04









                Jack

                5,19232753




                5,19232753










                answered Mar 19 '11 at 22:29









                Bastian

                9,44612637




                9,44612637







                • 6




                  ...and if you want to play it right after [p seekToTime:kCMTimeZero] (a "rewind" of sorts), simply do [p play] again.
                  – thomax
                  Dec 28 '11 at 11:31






                • 23




                  this should not be necessary... if you do avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; it will not stop, so no need to set it to play again
                  – Bastian
                  Dec 31 '11 at 14:44






                • 2




                  in my case it stops then.
                  – headkit
                  Aug 30 '12 at 15:05






                • 12




                  This solution works, but it is not completely seamless. I have a very small pause. Am I doing something wrong?
                  – Joris van Liempd iDeveloper
                  Dec 21 '12 at 11:20






                • 3




                  @Praxiteles you need to unregister it when the view ist destroyed, or when you remove the videoplayer or whatever you do ,) You can use [[NSNotificationCenter defaultCenter] removeObserver:self]; for example, when self is listening to the notifications.
                  – Bastian
                  Jun 24 '15 at 16:03












                • 6




                  ...and if you want to play it right after [p seekToTime:kCMTimeZero] (a "rewind" of sorts), simply do [p play] again.
                  – thomax
                  Dec 28 '11 at 11:31






                • 23




                  this should not be necessary... if you do avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; it will not stop, so no need to set it to play again
                  – Bastian
                  Dec 31 '11 at 14:44






                • 2




                  in my case it stops then.
                  – headkit
                  Aug 30 '12 at 15:05






                • 12




                  This solution works, but it is not completely seamless. I have a very small pause. Am I doing something wrong?
                  – Joris van Liempd iDeveloper
                  Dec 21 '12 at 11:20






                • 3




                  @Praxiteles you need to unregister it when the view ist destroyed, or when you remove the videoplayer or whatever you do ,) You can use [[NSNotificationCenter defaultCenter] removeObserver:self]; for example, when self is listening to the notifications.
                  – Bastian
                  Jun 24 '15 at 16:03







                6




                6




                ...and if you want to play it right after [p seekToTime:kCMTimeZero] (a "rewind" of sorts), simply do [p play] again.
                – thomax
                Dec 28 '11 at 11:31




                ...and if you want to play it right after [p seekToTime:kCMTimeZero] (a "rewind" of sorts), simply do [p play] again.
                – thomax
                Dec 28 '11 at 11:31




                23




                23




                this should not be necessary... if you do avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; it will not stop, so no need to set it to play again
                – Bastian
                Dec 31 '11 at 14:44




                this should not be necessary... if you do avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; it will not stop, so no need to set it to play again
                – Bastian
                Dec 31 '11 at 14:44




                2




                2




                in my case it stops then.
                – headkit
                Aug 30 '12 at 15:05




                in my case it stops then.
                – headkit
                Aug 30 '12 at 15:05




                12




                12




                This solution works, but it is not completely seamless. I have a very small pause. Am I doing something wrong?
                – Joris van Liempd iDeveloper
                Dec 21 '12 at 11:20




                This solution works, but it is not completely seamless. I have a very small pause. Am I doing something wrong?
                – Joris van Liempd iDeveloper
                Dec 21 '12 at 11:20




                3




                3




                @Praxiteles you need to unregister it when the view ist destroyed, or when you remove the videoplayer or whatever you do ,) You can use [[NSNotificationCenter defaultCenter] removeObserver:self]; for example, when self is listening to the notifications.
                – Bastian
                Jun 24 '15 at 16:03




                @Praxiteles you need to unregister it when the view ist destroyed, or when you remove the videoplayer or whatever you do ,) You can use [[NSNotificationCenter defaultCenter] removeObserver:self]; for example, when self is listening to the notifications.
                – Bastian
                Jun 24 '15 at 16:03












                up vote
                42
                down vote













                If it helps, in iOS / tvOS 10, there's a new AVPlayerLooper() that you can use to create seamless looping of video (Swift):



                player = AVQueuePlayer()
                playerLayer = AVPlayerLayer(player: player)
                playerItem = AVPlayerItem(url: videoURL)
                playerLooper = AVPlayerLooper(player: player, templateItem: playerItem)
                player.play()


                This was presented at WWDC 2016 in "Advances in AVFoundation Playback":
                https://developer.apple.com/videos/play/wwdc2016/503/



                Even using this code, I had a hiccup until I filed a bug report with Apple and got this response:




                The movie file having movie duration longer than audio/video tracks is
                the problem. FigPlayer_File is disabling gapless transition because
                audio track edit is shorter than the movie duration (15.682 vs
                15.787).



                You need to either fix the movie files to have the movie duration and
                track durations to be same length or you can use the time range
                parameter of AVPlayerLooper (set time range from 0 to duration of
                audio track)




                It turns out that Premiere had been exporting files with an audio track of a slightly different length than the video. In my case it was fine to remove the audio entirely, and that fixed the problem.






                share|improve this answer


















                • 1




                  Nothing else worked for me. I am using an AVPlayerLooper and had this bug and fixing the discrepancy between video/audio lengths solved the issue.
                  – Kevin Heap
                  Oct 6 '17 at 15:56






                • 2




                  This also works in ObjC as well.
                  – ObjSal
                  Nov 30 '17 at 0:56






                • 1




                  Thank you for that information about Premiere. I added a timeRange to the looper and that fixed my "flashing video" issue.
                  – Alexander Flenniken
                  Aug 29 at 8:31














                up vote
                42
                down vote













                If it helps, in iOS / tvOS 10, there's a new AVPlayerLooper() that you can use to create seamless looping of video (Swift):



                player = AVQueuePlayer()
                playerLayer = AVPlayerLayer(player: player)
                playerItem = AVPlayerItem(url: videoURL)
                playerLooper = AVPlayerLooper(player: player, templateItem: playerItem)
                player.play()


                This was presented at WWDC 2016 in "Advances in AVFoundation Playback":
                https://developer.apple.com/videos/play/wwdc2016/503/



                Even using this code, I had a hiccup until I filed a bug report with Apple and got this response:




                The movie file having movie duration longer than audio/video tracks is
                the problem. FigPlayer_File is disabling gapless transition because
                audio track edit is shorter than the movie duration (15.682 vs
                15.787).



                You need to either fix the movie files to have the movie duration and
                track durations to be same length or you can use the time range
                parameter of AVPlayerLooper (set time range from 0 to duration of
                audio track)




                It turns out that Premiere had been exporting files with an audio track of a slightly different length than the video. In my case it was fine to remove the audio entirely, and that fixed the problem.






                share|improve this answer


















                • 1




                  Nothing else worked for me. I am using an AVPlayerLooper and had this bug and fixing the discrepancy between video/audio lengths solved the issue.
                  – Kevin Heap
                  Oct 6 '17 at 15:56






                • 2




                  This also works in ObjC as well.
                  – ObjSal
                  Nov 30 '17 at 0:56






                • 1




                  Thank you for that information about Premiere. I added a timeRange to the looper and that fixed my "flashing video" issue.
                  – Alexander Flenniken
                  Aug 29 at 8:31












                up vote
                42
                down vote










                up vote
                42
                down vote









                If it helps, in iOS / tvOS 10, there's a new AVPlayerLooper() that you can use to create seamless looping of video (Swift):



                player = AVQueuePlayer()
                playerLayer = AVPlayerLayer(player: player)
                playerItem = AVPlayerItem(url: videoURL)
                playerLooper = AVPlayerLooper(player: player, templateItem: playerItem)
                player.play()


                This was presented at WWDC 2016 in "Advances in AVFoundation Playback":
                https://developer.apple.com/videos/play/wwdc2016/503/



                Even using this code, I had a hiccup until I filed a bug report with Apple and got this response:




                The movie file having movie duration longer than audio/video tracks is
                the problem. FigPlayer_File is disabling gapless transition because
                audio track edit is shorter than the movie duration (15.682 vs
                15.787).



                You need to either fix the movie files to have the movie duration and
                track durations to be same length or you can use the time range
                parameter of AVPlayerLooper (set time range from 0 to duration of
                audio track)




                It turns out that Premiere had been exporting files with an audio track of a slightly different length than the video. In my case it was fine to remove the audio entirely, and that fixed the problem.






                share|improve this answer














                If it helps, in iOS / tvOS 10, there's a new AVPlayerLooper() that you can use to create seamless looping of video (Swift):



                player = AVQueuePlayer()
                playerLayer = AVPlayerLayer(player: player)
                playerItem = AVPlayerItem(url: videoURL)
                playerLooper = AVPlayerLooper(player: player, templateItem: playerItem)
                player.play()


                This was presented at WWDC 2016 in "Advances in AVFoundation Playback":
                https://developer.apple.com/videos/play/wwdc2016/503/



                Even using this code, I had a hiccup until I filed a bug report with Apple and got this response:




                The movie file having movie duration longer than audio/video tracks is
                the problem. FigPlayer_File is disabling gapless transition because
                audio track edit is shorter than the movie duration (15.682 vs
                15.787).



                You need to either fix the movie files to have the movie duration and
                track durations to be same length or you can use the time range
                parameter of AVPlayerLooper (set time range from 0 to duration of
                audio track)




                It turns out that Premiere had been exporting files with an audio track of a slightly different length than the video. In my case it was fine to remove the audio entirely, and that fixed the problem.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 17 at 17:15









                Mike

                3,63241320




                3,63241320










                answered Jul 8 '16 at 16:12









                Nabha

                603613




                603613







                • 1




                  Nothing else worked for me. I am using an AVPlayerLooper and had this bug and fixing the discrepancy between video/audio lengths solved the issue.
                  – Kevin Heap
                  Oct 6 '17 at 15:56






                • 2




                  This also works in ObjC as well.
                  – ObjSal
                  Nov 30 '17 at 0:56






                • 1




                  Thank you for that information about Premiere. I added a timeRange to the looper and that fixed my "flashing video" issue.
                  – Alexander Flenniken
                  Aug 29 at 8:31












                • 1




                  Nothing else worked for me. I am using an AVPlayerLooper and had this bug and fixing the discrepancy between video/audio lengths solved the issue.
                  – Kevin Heap
                  Oct 6 '17 at 15:56






                • 2




                  This also works in ObjC as well.
                  – ObjSal
                  Nov 30 '17 at 0:56






                • 1




                  Thank you for that information about Premiere. I added a timeRange to the looper and that fixed my "flashing video" issue.
                  – Alexander Flenniken
                  Aug 29 at 8:31







                1




                1




                Nothing else worked for me. I am using an AVPlayerLooper and had this bug and fixing the discrepancy between video/audio lengths solved the issue.
                – Kevin Heap
                Oct 6 '17 at 15:56




                Nothing else worked for me. I am using an AVPlayerLooper and had this bug and fixing the discrepancy between video/audio lengths solved the issue.
                – Kevin Heap
                Oct 6 '17 at 15:56




                2




                2




                This also works in ObjC as well.
                – ObjSal
                Nov 30 '17 at 0:56




                This also works in ObjC as well.
                – ObjSal
                Nov 30 '17 at 0:56




                1




                1




                Thank you for that information about Premiere. I added a timeRange to the looper and that fixed my "flashing video" issue.
                – Alexander Flenniken
                Aug 29 at 8:31




                Thank you for that information about Premiere. I added a timeRange to the looper and that fixed my "flashing video" issue.
                – Alexander Flenniken
                Aug 29 at 8:31










                up vote
                24
                down vote













                In Swift:



                You can get a Notification when the player ends... check AVPlayerItemDidPlayToEndTimeNotification



                when setting up the player:



                avPlayer.actionAtItemEnd = AVPlayerActionAtItemEnd.None

                NSNotificationCenter.defaultCenter().addObserver(self,
                selector: "playerItemDidReachEnd:",
                name: AVPlayerItemDidPlayToEndTimeNotification,
                object: avPlayer.currentItem)


                this will prevent the player to pause at the end.



                in the notification:



                func playerItemDidReachEnd(notification: NSNotification) 
                if let playerItem: AVPlayerItem = notification.object as? AVPlayerItem
                playerItem.seekToTime(kCMTimeZero)




                Swift3



                NotificationCenter.default.addObserver(self,
                selector: #selector(PlaylistViewController.playerItemDidReachEnd),
                name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                object: avPlayer?.currentItem)


                this will rewind the movie.



                Don't forget un unregister the notification when releasing the player.






                share|improve this answer


















                • 4




                  I'm seeing a small hiccup between loops with this method. I opened my video in Adobe Premier and verified there are no duplicate frames in the video, so the brief hiccup is definitely at playback. Has anybody found a way to make a video loop seamlessly in swift?
                  – SpaceManGalaxy
                  Oct 11 '15 at 22:45











                • @SpaceManGalaxy I’ve also noticed the hiccup. Have you found a way to fix this glitch?
                  – Lance Samaria
                  Jan 31 at 6:49














                up vote
                24
                down vote













                In Swift:



                You can get a Notification when the player ends... check AVPlayerItemDidPlayToEndTimeNotification



                when setting up the player:



                avPlayer.actionAtItemEnd = AVPlayerActionAtItemEnd.None

                NSNotificationCenter.defaultCenter().addObserver(self,
                selector: "playerItemDidReachEnd:",
                name: AVPlayerItemDidPlayToEndTimeNotification,
                object: avPlayer.currentItem)


                this will prevent the player to pause at the end.



                in the notification:



                func playerItemDidReachEnd(notification: NSNotification) 
                if let playerItem: AVPlayerItem = notification.object as? AVPlayerItem
                playerItem.seekToTime(kCMTimeZero)




                Swift3



                NotificationCenter.default.addObserver(self,
                selector: #selector(PlaylistViewController.playerItemDidReachEnd),
                name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                object: avPlayer?.currentItem)


                this will rewind the movie.



                Don't forget un unregister the notification when releasing the player.






                share|improve this answer


















                • 4




                  I'm seeing a small hiccup between loops with this method. I opened my video in Adobe Premier and verified there are no duplicate frames in the video, so the brief hiccup is definitely at playback. Has anybody found a way to make a video loop seamlessly in swift?
                  – SpaceManGalaxy
                  Oct 11 '15 at 22:45











                • @SpaceManGalaxy I’ve also noticed the hiccup. Have you found a way to fix this glitch?
                  – Lance Samaria
                  Jan 31 at 6:49












                up vote
                24
                down vote










                up vote
                24
                down vote









                In Swift:



                You can get a Notification when the player ends... check AVPlayerItemDidPlayToEndTimeNotification



                when setting up the player:



                avPlayer.actionAtItemEnd = AVPlayerActionAtItemEnd.None

                NSNotificationCenter.defaultCenter().addObserver(self,
                selector: "playerItemDidReachEnd:",
                name: AVPlayerItemDidPlayToEndTimeNotification,
                object: avPlayer.currentItem)


                this will prevent the player to pause at the end.



                in the notification:



                func playerItemDidReachEnd(notification: NSNotification) 
                if let playerItem: AVPlayerItem = notification.object as? AVPlayerItem
                playerItem.seekToTime(kCMTimeZero)




                Swift3



                NotificationCenter.default.addObserver(self,
                selector: #selector(PlaylistViewController.playerItemDidReachEnd),
                name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                object: avPlayer?.currentItem)


                this will rewind the movie.



                Don't forget un unregister the notification when releasing the player.






                share|improve this answer














                In Swift:



                You can get a Notification when the player ends... check AVPlayerItemDidPlayToEndTimeNotification



                when setting up the player:



                avPlayer.actionAtItemEnd = AVPlayerActionAtItemEnd.None

                NSNotificationCenter.defaultCenter().addObserver(self,
                selector: "playerItemDidReachEnd:",
                name: AVPlayerItemDidPlayToEndTimeNotification,
                object: avPlayer.currentItem)


                this will prevent the player to pause at the end.



                in the notification:



                func playerItemDidReachEnd(notification: NSNotification) 
                if let playerItem: AVPlayerItem = notification.object as? AVPlayerItem
                playerItem.seekToTime(kCMTimeZero)




                Swift3



                NotificationCenter.default.addObserver(self,
                selector: #selector(PlaylistViewController.playerItemDidReachEnd),
                name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                object: avPlayer?.currentItem)


                this will rewind the movie.



                Don't forget un unregister the notification when releasing the player.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 29 '17 at 17:23









                vishal dharankar

                4,57643566




                4,57643566










                answered May 1 '15 at 20:45









                King-Wizard

                13.1k27169




                13.1k27169







                • 4




                  I'm seeing a small hiccup between loops with this method. I opened my video in Adobe Premier and verified there are no duplicate frames in the video, so the brief hiccup is definitely at playback. Has anybody found a way to make a video loop seamlessly in swift?
                  – SpaceManGalaxy
                  Oct 11 '15 at 22:45











                • @SpaceManGalaxy I’ve also noticed the hiccup. Have you found a way to fix this glitch?
                  – Lance Samaria
                  Jan 31 at 6:49












                • 4




                  I'm seeing a small hiccup between loops with this method. I opened my video in Adobe Premier and verified there are no duplicate frames in the video, so the brief hiccup is definitely at playback. Has anybody found a way to make a video loop seamlessly in swift?
                  – SpaceManGalaxy
                  Oct 11 '15 at 22:45











                • @SpaceManGalaxy I’ve also noticed the hiccup. Have you found a way to fix this glitch?
                  – Lance Samaria
                  Jan 31 at 6:49







                4




                4




                I'm seeing a small hiccup between loops with this method. I opened my video in Adobe Premier and verified there are no duplicate frames in the video, so the brief hiccup is definitely at playback. Has anybody found a way to make a video loop seamlessly in swift?
                – SpaceManGalaxy
                Oct 11 '15 at 22:45





                I'm seeing a small hiccup between loops with this method. I opened my video in Adobe Premier and verified there are no duplicate frames in the video, so the brief hiccup is definitely at playback. Has anybody found a way to make a video loop seamlessly in swift?
                – SpaceManGalaxy
                Oct 11 '15 at 22:45













                @SpaceManGalaxy I’ve also noticed the hiccup. Have you found a way to fix this glitch?
                – Lance Samaria
                Jan 31 at 6:49




                @SpaceManGalaxy I’ve also noticed the hiccup. Have you found a way to fix this glitch?
                – Lance Samaria
                Jan 31 at 6:49










                up vote
                14
                down vote













                Here's what I ended up doing to prevent the pause-hiccup issue:



                Swift:



                NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime,
                object: nil,
                queue: nil) [weak self] note in
                self?.avPlayer.seek(to: kCMTimeZero)
                self?.avPlayer.play()




                Objective C:



                __weak typeof(self) weakSelf = self; // prevent memory cycle
                NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
                [noteCenter addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
                object:nil
                queue:nil
                usingBlock:^(NSNotification *note)
                [weakSelf.avPlayer seekToTime:kCMTimeZero];
                [weakSelf.avPlayer play];
                ];



                NOTE: I didn't use avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone as it's not needed.






                share|improve this answer


















                • 1




                  @KostiaDombrovsky did you try on an actual device or different videos?
                  – Islam Q.
                  Apr 8 '15 at 7:20










                • @IslamQ. I record an MP4 file and then try to play it in a loop kinda like snapchat does.
                  – Kostia Dombrovsky
                  Apr 8 '15 at 7:22










                • @KostiaDombrovsky did you compare your playback with snapchat side-by-side? I think because the beginning and the ending frames don't match it seems as if it was paused, but it never pauses.
                  – Islam Q.
                  Apr 8 '15 at 9:25










                • Didn't work for me either. I have a 6 second video with ceaseless audio and I keep hearing a split second of silence with this method
                  – Cbas
                  Jan 24 '16 at 3:05










                • I am seeing a memory leak when using this approach. It has to do with the [weakSelf.avPlayer seekToTime:kCMTimeZero]; [weakSelf.avPlayer play]; lines - when I comment out these lines there is not longer a memory leak. I've profiled this in instruments.
                  – Solsma Dev
                  Nov 16 '16 at 16:39














                up vote
                14
                down vote













                Here's what I ended up doing to prevent the pause-hiccup issue:



                Swift:



                NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime,
                object: nil,
                queue: nil) [weak self] note in
                self?.avPlayer.seek(to: kCMTimeZero)
                self?.avPlayer.play()




                Objective C:



                __weak typeof(self) weakSelf = self; // prevent memory cycle
                NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
                [noteCenter addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
                object:nil
                queue:nil
                usingBlock:^(NSNotification *note)
                [weakSelf.avPlayer seekToTime:kCMTimeZero];
                [weakSelf.avPlayer play];
                ];



                NOTE: I didn't use avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone as it's not needed.






                share|improve this answer


















                • 1




                  @KostiaDombrovsky did you try on an actual device or different videos?
                  – Islam Q.
                  Apr 8 '15 at 7:20










                • @IslamQ. I record an MP4 file and then try to play it in a loop kinda like snapchat does.
                  – Kostia Dombrovsky
                  Apr 8 '15 at 7:22










                • @KostiaDombrovsky did you compare your playback with snapchat side-by-side? I think because the beginning and the ending frames don't match it seems as if it was paused, but it never pauses.
                  – Islam Q.
                  Apr 8 '15 at 9:25










                • Didn't work for me either. I have a 6 second video with ceaseless audio and I keep hearing a split second of silence with this method
                  – Cbas
                  Jan 24 '16 at 3:05










                • I am seeing a memory leak when using this approach. It has to do with the [weakSelf.avPlayer seekToTime:kCMTimeZero]; [weakSelf.avPlayer play]; lines - when I comment out these lines there is not longer a memory leak. I've profiled this in instruments.
                  – Solsma Dev
                  Nov 16 '16 at 16:39












                up vote
                14
                down vote










                up vote
                14
                down vote









                Here's what I ended up doing to prevent the pause-hiccup issue:



                Swift:



                NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime,
                object: nil,
                queue: nil) [weak self] note in
                self?.avPlayer.seek(to: kCMTimeZero)
                self?.avPlayer.play()




                Objective C:



                __weak typeof(self) weakSelf = self; // prevent memory cycle
                NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
                [noteCenter addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
                object:nil
                queue:nil
                usingBlock:^(NSNotification *note)
                [weakSelf.avPlayer seekToTime:kCMTimeZero];
                [weakSelf.avPlayer play];
                ];



                NOTE: I didn't use avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone as it's not needed.






                share|improve this answer














                Here's what I ended up doing to prevent the pause-hiccup issue:



                Swift:



                NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime,
                object: nil,
                queue: nil) [weak self] note in
                self?.avPlayer.seek(to: kCMTimeZero)
                self?.avPlayer.play()




                Objective C:



                __weak typeof(self) weakSelf = self; // prevent memory cycle
                NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
                [noteCenter addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
                object:nil
                queue:nil
                usingBlock:^(NSNotification *note)
                [weakSelf.avPlayer seekToTime:kCMTimeZero];
                [weakSelf.avPlayer play];
                ];



                NOTE: I didn't use avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone as it's not needed.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 1 at 5:29

























                answered Oct 16 '14 at 10:08









                Islam Q.

                2,49721832




                2,49721832







                • 1




                  @KostiaDombrovsky did you try on an actual device or different videos?
                  – Islam Q.
                  Apr 8 '15 at 7:20










                • @IslamQ. I record an MP4 file and then try to play it in a loop kinda like snapchat does.
                  – Kostia Dombrovsky
                  Apr 8 '15 at 7:22










                • @KostiaDombrovsky did you compare your playback with snapchat side-by-side? I think because the beginning and the ending frames don't match it seems as if it was paused, but it never pauses.
                  – Islam Q.
                  Apr 8 '15 at 9:25










                • Didn't work for me either. I have a 6 second video with ceaseless audio and I keep hearing a split second of silence with this method
                  – Cbas
                  Jan 24 '16 at 3:05










                • I am seeing a memory leak when using this approach. It has to do with the [weakSelf.avPlayer seekToTime:kCMTimeZero]; [weakSelf.avPlayer play]; lines - when I comment out these lines there is not longer a memory leak. I've profiled this in instruments.
                  – Solsma Dev
                  Nov 16 '16 at 16:39












                • 1




                  @KostiaDombrovsky did you try on an actual device or different videos?
                  – Islam Q.
                  Apr 8 '15 at 7:20










                • @IslamQ. I record an MP4 file and then try to play it in a loop kinda like snapchat does.
                  – Kostia Dombrovsky
                  Apr 8 '15 at 7:22










                • @KostiaDombrovsky did you compare your playback with snapchat side-by-side? I think because the beginning and the ending frames don't match it seems as if it was paused, but it never pauses.
                  – Islam Q.
                  Apr 8 '15 at 9:25










                • Didn't work for me either. I have a 6 second video with ceaseless audio and I keep hearing a split second of silence with this method
                  – Cbas
                  Jan 24 '16 at 3:05










                • I am seeing a memory leak when using this approach. It has to do with the [weakSelf.avPlayer seekToTime:kCMTimeZero]; [weakSelf.avPlayer play]; lines - when I comment out these lines there is not longer a memory leak. I've profiled this in instruments.
                  – Solsma Dev
                  Nov 16 '16 at 16:39







                1




                1




                @KostiaDombrovsky did you try on an actual device or different videos?
                – Islam Q.
                Apr 8 '15 at 7:20




                @KostiaDombrovsky did you try on an actual device or different videos?
                – Islam Q.
                Apr 8 '15 at 7:20












                @IslamQ. I record an MP4 file and then try to play it in a loop kinda like snapchat does.
                – Kostia Dombrovsky
                Apr 8 '15 at 7:22




                @IslamQ. I record an MP4 file and then try to play it in a loop kinda like snapchat does.
                – Kostia Dombrovsky
                Apr 8 '15 at 7:22












                @KostiaDombrovsky did you compare your playback with snapchat side-by-side? I think because the beginning and the ending frames don't match it seems as if it was paused, but it never pauses.
                – Islam Q.
                Apr 8 '15 at 9:25




                @KostiaDombrovsky did you compare your playback with snapchat side-by-side? I think because the beginning and the ending frames don't match it seems as if it was paused, but it never pauses.
                – Islam Q.
                Apr 8 '15 at 9:25












                Didn't work for me either. I have a 6 second video with ceaseless audio and I keep hearing a split second of silence with this method
                – Cbas
                Jan 24 '16 at 3:05




                Didn't work for me either. I have a 6 second video with ceaseless audio and I keep hearing a split second of silence with this method
                – Cbas
                Jan 24 '16 at 3:05












                I am seeing a memory leak when using this approach. It has to do with the [weakSelf.avPlayer seekToTime:kCMTimeZero]; [weakSelf.avPlayer play]; lines - when I comment out these lines there is not longer a memory leak. I've profiled this in instruments.
                – Solsma Dev
                Nov 16 '16 at 16:39




                I am seeing a memory leak when using this approach. It has to do with the [weakSelf.avPlayer seekToTime:kCMTimeZero]; [weakSelf.avPlayer play]; lines - when I comment out these lines there is not longer a memory leak. I've profiled this in instruments.
                – Solsma Dev
                Nov 16 '16 at 16:39










                up vote
                3
                down vote













                I recommend using AVQueuePlayer to loop your videos seamlessly. Add the notification observer



                AVPlayerItemDidPlayToEndTimeNotification


                and in its selector, loop your video



                AVPlayerItem *video = [[AVPlayerItem alloc] initWithURL:videoURL];
                [self.player insertItem:video afterItem:nil];
                [self.player play];





                share|improve this answer




















                • I tried this and it does not show any improvement over the method @Bastian suggested. Did you manage to totally remove the hiccup with this ?
                  – amadour
                  Sep 29 '14 at 19:32






                • 2




                  @amadour what you can do is add 2 of the same videos in the AVQueuePlayer player when initialized and when the player posts the AVPlayerItemDidPlayToEndTimeNotification, add the same video to the player's queue.
                  – kevnguy
                  Sep 30 '14 at 4:43














                up vote
                3
                down vote













                I recommend using AVQueuePlayer to loop your videos seamlessly. Add the notification observer



                AVPlayerItemDidPlayToEndTimeNotification


                and in its selector, loop your video



                AVPlayerItem *video = [[AVPlayerItem alloc] initWithURL:videoURL];
                [self.player insertItem:video afterItem:nil];
                [self.player play];





                share|improve this answer




















                • I tried this and it does not show any improvement over the method @Bastian suggested. Did you manage to totally remove the hiccup with this ?
                  – amadour
                  Sep 29 '14 at 19:32






                • 2




                  @amadour what you can do is add 2 of the same videos in the AVQueuePlayer player when initialized and when the player posts the AVPlayerItemDidPlayToEndTimeNotification, add the same video to the player's queue.
                  – kevnguy
                  Sep 30 '14 at 4:43












                up vote
                3
                down vote










                up vote
                3
                down vote









                I recommend using AVQueuePlayer to loop your videos seamlessly. Add the notification observer



                AVPlayerItemDidPlayToEndTimeNotification


                and in its selector, loop your video



                AVPlayerItem *video = [[AVPlayerItem alloc] initWithURL:videoURL];
                [self.player insertItem:video afterItem:nil];
                [self.player play];





                share|improve this answer












                I recommend using AVQueuePlayer to loop your videos seamlessly. Add the notification observer



                AVPlayerItemDidPlayToEndTimeNotification


                and in its selector, loop your video



                AVPlayerItem *video = [[AVPlayerItem alloc] initWithURL:videoURL];
                [self.player insertItem:video afterItem:nil];
                [self.player play];






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 3 '14 at 17:35









                kevnguy

                322313




                322313











                • I tried this and it does not show any improvement over the method @Bastian suggested. Did you manage to totally remove the hiccup with this ?
                  – amadour
                  Sep 29 '14 at 19:32






                • 2




                  @amadour what you can do is add 2 of the same videos in the AVQueuePlayer player when initialized and when the player posts the AVPlayerItemDidPlayToEndTimeNotification, add the same video to the player's queue.
                  – kevnguy
                  Sep 30 '14 at 4:43
















                • I tried this and it does not show any improvement over the method @Bastian suggested. Did you manage to totally remove the hiccup with this ?
                  – amadour
                  Sep 29 '14 at 19:32






                • 2




                  @amadour what you can do is add 2 of the same videos in the AVQueuePlayer player when initialized and when the player posts the AVPlayerItemDidPlayToEndTimeNotification, add the same video to the player's queue.
                  – kevnguy
                  Sep 30 '14 at 4:43















                I tried this and it does not show any improvement over the method @Bastian suggested. Did you manage to totally remove the hiccup with this ?
                – amadour
                Sep 29 '14 at 19:32




                I tried this and it does not show any improvement over the method @Bastian suggested. Did you manage to totally remove the hiccup with this ?
                – amadour
                Sep 29 '14 at 19:32




                2




                2




                @amadour what you can do is add 2 of the same videos in the AVQueuePlayer player when initialized and when the player posts the AVPlayerItemDidPlayToEndTimeNotification, add the same video to the player's queue.
                – kevnguy
                Sep 30 '14 at 4:43




                @amadour what you can do is add 2 of the same videos in the AVQueuePlayer player when initialized and when the player posts the AVPlayerItemDidPlayToEndTimeNotification, add the same video to the player's queue.
                – kevnguy
                Sep 30 '14 at 4:43










                up vote
                3
                down vote













                To avoid the gap when the video is rewound, using multiple copies of the same asset in a composition worked well for me. I found it here: www.developers-life.com/avplayer-looping-video-without-hiccupdelays.html (link now dead).



                AVURLAsset *tAsset = [AVURLAsset assetWithURL:tURL];
                CMTimeRange tEditRange = CMTimeRangeMake(CMTimeMake(0, 1), CMTimeMake(tAsset.duration.value, tAsset.duration.timescale));
                AVMutableComposition *tComposition = [[[AVMutableComposition alloc] init] autorelease];
                for (int i = 0; i < 100; i++) // Insert some copies.
                [tComposition insertTimeRange:tEditRange ofAsset:tAsset atTime:tComposition.duration error:nil];

                AVPlayerItem *tAVPlayerItem = [[AVPlayerItem alloc] initWithAsset:tComposition];
                AVPlayer *tAVPlayer = [[AVPlayer alloc] initWithPlayerItem:tAVPlayerItem];





                share|improve this answer






















                • I guess you mean this link devbrief.blogspot.se/2011/12/…
                  – flame3
                  Aug 31 '16 at 9:35














                up vote
                3
                down vote













                To avoid the gap when the video is rewound, using multiple copies of the same asset in a composition worked well for me. I found it here: www.developers-life.com/avplayer-looping-video-without-hiccupdelays.html (link now dead).



                AVURLAsset *tAsset = [AVURLAsset assetWithURL:tURL];
                CMTimeRange tEditRange = CMTimeRangeMake(CMTimeMake(0, 1), CMTimeMake(tAsset.duration.value, tAsset.duration.timescale));
                AVMutableComposition *tComposition = [[[AVMutableComposition alloc] init] autorelease];
                for (int i = 0; i < 100; i++) // Insert some copies.
                [tComposition insertTimeRange:tEditRange ofAsset:tAsset atTime:tComposition.duration error:nil];

                AVPlayerItem *tAVPlayerItem = [[AVPlayerItem alloc] initWithAsset:tComposition];
                AVPlayer *tAVPlayer = [[AVPlayer alloc] initWithPlayerItem:tAVPlayerItem];





                share|improve this answer






















                • I guess you mean this link devbrief.blogspot.se/2011/12/…
                  – flame3
                  Aug 31 '16 at 9:35












                up vote
                3
                down vote










                up vote
                3
                down vote









                To avoid the gap when the video is rewound, using multiple copies of the same asset in a composition worked well for me. I found it here: www.developers-life.com/avplayer-looping-video-without-hiccupdelays.html (link now dead).



                AVURLAsset *tAsset = [AVURLAsset assetWithURL:tURL];
                CMTimeRange tEditRange = CMTimeRangeMake(CMTimeMake(0, 1), CMTimeMake(tAsset.duration.value, tAsset.duration.timescale));
                AVMutableComposition *tComposition = [[[AVMutableComposition alloc] init] autorelease];
                for (int i = 0; i < 100; i++) // Insert some copies.
                [tComposition insertTimeRange:tEditRange ofAsset:tAsset atTime:tComposition.duration error:nil];

                AVPlayerItem *tAVPlayerItem = [[AVPlayerItem alloc] initWithAsset:tComposition];
                AVPlayer *tAVPlayer = [[AVPlayer alloc] initWithPlayerItem:tAVPlayerItem];





                share|improve this answer














                To avoid the gap when the video is rewound, using multiple copies of the same asset in a composition worked well for me. I found it here: www.developers-life.com/avplayer-looping-video-without-hiccupdelays.html (link now dead).



                AVURLAsset *tAsset = [AVURLAsset assetWithURL:tURL];
                CMTimeRange tEditRange = CMTimeRangeMake(CMTimeMake(0, 1), CMTimeMake(tAsset.duration.value, tAsset.duration.timescale));
                AVMutableComposition *tComposition = [[[AVMutableComposition alloc] init] autorelease];
                for (int i = 0; i < 100; i++) // Insert some copies.
                [tComposition insertTimeRange:tEditRange ofAsset:tAsset atTime:tComposition.duration error:nil];

                AVPlayerItem *tAVPlayerItem = [[AVPlayerItem alloc] initWithAsset:tComposition];
                AVPlayer *tAVPlayer = [[AVPlayer alloc] initWithPlayerItem:tAVPlayerItem];






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 9 '15 at 18:54









                Matt

                61.7k18118159




                61.7k18118159










                answered Dec 17 '14 at 12:24









                user2581875

                11913




                11913











                • I guess you mean this link devbrief.blogspot.se/2011/12/…
                  – flame3
                  Aug 31 '16 at 9:35
















                • I guess you mean this link devbrief.blogspot.se/2011/12/…
                  – flame3
                  Aug 31 '16 at 9:35















                I guess you mean this link devbrief.blogspot.se/2011/12/…
                – flame3
                Aug 31 '16 at 9:35




                I guess you mean this link devbrief.blogspot.se/2011/12/…
                – flame3
                Aug 31 '16 at 9:35










                up vote
                2
                down vote













                For Swift 3 & 4



                NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.avPlayer?.currentItem, queue: .main) _ in
                self.avPlayer?.seek(to: kCMTimeZero)
                self.avPlayer?.play()






                share|improve this answer
























                  up vote
                  2
                  down vote













                  For Swift 3 & 4



                  NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.avPlayer?.currentItem, queue: .main) _ in
                  self.avPlayer?.seek(to: kCMTimeZero)
                  self.avPlayer?.play()






                  share|improve this answer






















                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    For Swift 3 & 4



                    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.avPlayer?.currentItem, queue: .main) _ in
                    self.avPlayer?.seek(to: kCMTimeZero)
                    self.avPlayer?.play()






                    share|improve this answer












                    For Swift 3 & 4



                    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.avPlayer?.currentItem, queue: .main) _ in
                    self.avPlayer?.seek(to: kCMTimeZero)
                    self.avPlayer?.play()







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 21 at 12:43









                    vp2698

                    9961419




                    9961419




















                        up vote
                        1
                        down vote













                        this worked for me without hiccup issues, point is in pausing the player before calling seekToTime method:




                        1. init AVPlayer



                          let url = NSBundle.mainBundle().URLForResource("loop", withExtension: "mp4")
                          let playerItem = AVPlayerItem(URL: url!)

                          self.backgroundPlayer = AVPlayer(playerItem: playerItem)
                          let playerLayer = AVPlayerLayer(player: self.backgroundPlayer)

                          playerLayer.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)
                          self.layer.addSublayer(playerLayer)
                          self.backgroundPlayer!.actionAtItemEnd = .None
                          self.backgroundPlayer!.play()



                        2. registering notification



                          NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoLoop", name: AVPlayerItemDidPlayToEndTimeNotification, object: self.backgroundPlayer!.currentItem)



                        3. videoLoop function



                          func videoLoop() 
                          self.backgroundPlayer?.pause()
                          self.backgroundPlayer?.currentItem?.seekToTime(kCMTimeZero)
                          self.backgroundPlayer?.play()







                        share|improve this answer
















                        • 3




                          Thanks — I tried this, but there is still a pause for me.
                          – Nabha
                          Feb 8 '16 at 7:04














                        up vote
                        1
                        down vote













                        this worked for me without hiccup issues, point is in pausing the player before calling seekToTime method:




                        1. init AVPlayer



                          let url = NSBundle.mainBundle().URLForResource("loop", withExtension: "mp4")
                          let playerItem = AVPlayerItem(URL: url!)

                          self.backgroundPlayer = AVPlayer(playerItem: playerItem)
                          let playerLayer = AVPlayerLayer(player: self.backgroundPlayer)

                          playerLayer.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)
                          self.layer.addSublayer(playerLayer)
                          self.backgroundPlayer!.actionAtItemEnd = .None
                          self.backgroundPlayer!.play()



                        2. registering notification



                          NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoLoop", name: AVPlayerItemDidPlayToEndTimeNotification, object: self.backgroundPlayer!.currentItem)



                        3. videoLoop function



                          func videoLoop() 
                          self.backgroundPlayer?.pause()
                          self.backgroundPlayer?.currentItem?.seekToTime(kCMTimeZero)
                          self.backgroundPlayer?.play()







                        share|improve this answer
















                        • 3




                          Thanks — I tried this, but there is still a pause for me.
                          – Nabha
                          Feb 8 '16 at 7:04












                        up vote
                        1
                        down vote










                        up vote
                        1
                        down vote









                        this worked for me without hiccup issues, point is in pausing the player before calling seekToTime method:




                        1. init AVPlayer



                          let url = NSBundle.mainBundle().URLForResource("loop", withExtension: "mp4")
                          let playerItem = AVPlayerItem(URL: url!)

                          self.backgroundPlayer = AVPlayer(playerItem: playerItem)
                          let playerLayer = AVPlayerLayer(player: self.backgroundPlayer)

                          playerLayer.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)
                          self.layer.addSublayer(playerLayer)
                          self.backgroundPlayer!.actionAtItemEnd = .None
                          self.backgroundPlayer!.play()



                        2. registering notification



                          NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoLoop", name: AVPlayerItemDidPlayToEndTimeNotification, object: self.backgroundPlayer!.currentItem)



                        3. videoLoop function



                          func videoLoop() 
                          self.backgroundPlayer?.pause()
                          self.backgroundPlayer?.currentItem?.seekToTime(kCMTimeZero)
                          self.backgroundPlayer?.play()







                        share|improve this answer












                        this worked for me without hiccup issues, point is in pausing the player before calling seekToTime method:




                        1. init AVPlayer



                          let url = NSBundle.mainBundle().URLForResource("loop", withExtension: "mp4")
                          let playerItem = AVPlayerItem(URL: url!)

                          self.backgroundPlayer = AVPlayer(playerItem: playerItem)
                          let playerLayer = AVPlayerLayer(player: self.backgroundPlayer)

                          playerLayer.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)
                          self.layer.addSublayer(playerLayer)
                          self.backgroundPlayer!.actionAtItemEnd = .None
                          self.backgroundPlayer!.play()



                        2. registering notification



                          NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoLoop", name: AVPlayerItemDidPlayToEndTimeNotification, object: self.backgroundPlayer!.currentItem)



                        3. videoLoop function



                          func videoLoop() 
                          self.backgroundPlayer?.pause()
                          self.backgroundPlayer?.currentItem?.seekToTime(kCMTimeZero)
                          self.backgroundPlayer?.play()








                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 11 '15 at 0:43









                        Vojta

                        397412




                        397412







                        • 3




                          Thanks — I tried this, but there is still a pause for me.
                          – Nabha
                          Feb 8 '16 at 7:04












                        • 3




                          Thanks — I tried this, but there is still a pause for me.
                          – Nabha
                          Feb 8 '16 at 7:04







                        3




                        3




                        Thanks — I tried this, but there is still a pause for me.
                        – Nabha
                        Feb 8 '16 at 7:04




                        Thanks — I tried this, but there is still a pause for me.
                        – Nabha
                        Feb 8 '16 at 7:04










                        up vote
                        0
                        down vote













                        After loading the video into the AVPlayer (via its AVPlayerItem, of course):



                         [self addDidPlayToEndTimeNotificationForPlayerItem:item];


                        The addDidPlayToEndTimeNotificationForPlayerItem method:



                        - (void)addDidPlayToEndTimeNotificationForPlayerItem:(AVPlayerItem *)item

                        if (_notificationToken)
                        _notificationToken = nil;

                        /*
                        Setting actionAtItemEnd to None prevents the movie from getting paused at item end. A very simplistic, and not gapless, looped playback.
                        */
                        _player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
                        _notificationToken = [[NSNotificationCenter defaultCenter] addObserverForName:AVPlayerItemDidPlayToEndTimeNotification object:item queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note)
                        // Simple item playback rewind.
                        [[_player currentItem] seekToTime:kCMTimeZero];
                        ];



                        In your viewWillDisappear method:



                        if (_notificationToken) 
                        [[NSNotificationCenter defaultCenter] removeObserver:_notificationToken name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem];
                        _notificationToken = nil;



                        In your view controller's interface declaration within the implementation file:



                        id _notificationToken;


                        Need to see this up-and-running before you try? Download and run this sample app:



                        https://developer.apple.com/library/prerelease/ios/samplecode/AVBasicVideoOutput/Listings/AVBasicVideoOutput_APLViewController_m.html#//apple_ref/doc/uid/DTS40013109-AVBasicVideoOutput_APLViewController_m-DontLinkElementID_8



                        In my app, which uses this very code, there is no pause whatsoever between the end of the video and the beginning. In fact, depending on the video, there's no way for me to tell the video is at the beginning again, save the timecode display.






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          After loading the video into the AVPlayer (via its AVPlayerItem, of course):



                           [self addDidPlayToEndTimeNotificationForPlayerItem:item];


                          The addDidPlayToEndTimeNotificationForPlayerItem method:



                          - (void)addDidPlayToEndTimeNotificationForPlayerItem:(AVPlayerItem *)item

                          if (_notificationToken)
                          _notificationToken = nil;

                          /*
                          Setting actionAtItemEnd to None prevents the movie from getting paused at item end. A very simplistic, and not gapless, looped playback.
                          */
                          _player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
                          _notificationToken = [[NSNotificationCenter defaultCenter] addObserverForName:AVPlayerItemDidPlayToEndTimeNotification object:item queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note)
                          // Simple item playback rewind.
                          [[_player currentItem] seekToTime:kCMTimeZero];
                          ];



                          In your viewWillDisappear method:



                          if (_notificationToken) 
                          [[NSNotificationCenter defaultCenter] removeObserver:_notificationToken name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem];
                          _notificationToken = nil;



                          In your view controller's interface declaration within the implementation file:



                          id _notificationToken;


                          Need to see this up-and-running before you try? Download and run this sample app:



                          https://developer.apple.com/library/prerelease/ios/samplecode/AVBasicVideoOutput/Listings/AVBasicVideoOutput_APLViewController_m.html#//apple_ref/doc/uid/DTS40013109-AVBasicVideoOutput_APLViewController_m-DontLinkElementID_8



                          In my app, which uses this very code, there is no pause whatsoever between the end of the video and the beginning. In fact, depending on the video, there's no way for me to tell the video is at the beginning again, save the timecode display.






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            After loading the video into the AVPlayer (via its AVPlayerItem, of course):



                             [self addDidPlayToEndTimeNotificationForPlayerItem:item];


                            The addDidPlayToEndTimeNotificationForPlayerItem method:



                            - (void)addDidPlayToEndTimeNotificationForPlayerItem:(AVPlayerItem *)item

                            if (_notificationToken)
                            _notificationToken = nil;

                            /*
                            Setting actionAtItemEnd to None prevents the movie from getting paused at item end. A very simplistic, and not gapless, looped playback.
                            */
                            _player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
                            _notificationToken = [[NSNotificationCenter defaultCenter] addObserverForName:AVPlayerItemDidPlayToEndTimeNotification object:item queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note)
                            // Simple item playback rewind.
                            [[_player currentItem] seekToTime:kCMTimeZero];
                            ];



                            In your viewWillDisappear method:



                            if (_notificationToken) 
                            [[NSNotificationCenter defaultCenter] removeObserver:_notificationToken name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem];
                            _notificationToken = nil;



                            In your view controller's interface declaration within the implementation file:



                            id _notificationToken;


                            Need to see this up-and-running before you try? Download and run this sample app:



                            https://developer.apple.com/library/prerelease/ios/samplecode/AVBasicVideoOutput/Listings/AVBasicVideoOutput_APLViewController_m.html#//apple_ref/doc/uid/DTS40013109-AVBasicVideoOutput_APLViewController_m-DontLinkElementID_8



                            In my app, which uses this very code, there is no pause whatsoever between the end of the video and the beginning. In fact, depending on the video, there's no way for me to tell the video is at the beginning again, save the timecode display.






                            share|improve this answer












                            After loading the video into the AVPlayer (via its AVPlayerItem, of course):



                             [self addDidPlayToEndTimeNotificationForPlayerItem:item];


                            The addDidPlayToEndTimeNotificationForPlayerItem method:



                            - (void)addDidPlayToEndTimeNotificationForPlayerItem:(AVPlayerItem *)item

                            if (_notificationToken)
                            _notificationToken = nil;

                            /*
                            Setting actionAtItemEnd to None prevents the movie from getting paused at item end. A very simplistic, and not gapless, looped playback.
                            */
                            _player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
                            _notificationToken = [[NSNotificationCenter defaultCenter] addObserverForName:AVPlayerItemDidPlayToEndTimeNotification object:item queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note)
                            // Simple item playback rewind.
                            [[_player currentItem] seekToTime:kCMTimeZero];
                            ];



                            In your viewWillDisappear method:



                            if (_notificationToken) 
                            [[NSNotificationCenter defaultCenter] removeObserver:_notificationToken name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem];
                            _notificationToken = nil;



                            In your view controller's interface declaration within the implementation file:



                            id _notificationToken;


                            Need to see this up-and-running before you try? Download and run this sample app:



                            https://developer.apple.com/library/prerelease/ios/samplecode/AVBasicVideoOutput/Listings/AVBasicVideoOutput_APLViewController_m.html#//apple_ref/doc/uid/DTS40013109-AVBasicVideoOutput_APLViewController_m-DontLinkElementID_8



                            In my app, which uses this very code, there is no pause whatsoever between the end of the video and the beginning. In fact, depending on the video, there's no way for me to tell the video is at the beginning again, save the timecode display.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 26 '16 at 1:16









                            James Bush

                            1,063516




                            1,063516




















                                up vote
                                0
                                down vote













                                you can add a AVPlayerItemDidPlayToEndTimeNotification observer and replay video
                                from start in selector, code like below



                                 //add observer
                                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification
                                object:_aniPlayer.currentItem];

                                -(void)playbackFinished:(NSNotification *)notification
                                [_aniPlayer seekToTime:CMTimeMake(0, 1)];//replay from start
                                [_aniPlayer play];






                                share|improve this answer
























                                  up vote
                                  0
                                  down vote













                                  you can add a AVPlayerItemDidPlayToEndTimeNotification observer and replay video
                                  from start in selector, code like below



                                   //add observer
                                  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification
                                  object:_aniPlayer.currentItem];

                                  -(void)playbackFinished:(NSNotification *)notification
                                  [_aniPlayer seekToTime:CMTimeMake(0, 1)];//replay from start
                                  [_aniPlayer play];






                                  share|improve this answer






















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    you can add a AVPlayerItemDidPlayToEndTimeNotification observer and replay video
                                    from start in selector, code like below



                                     //add observer
                                    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification
                                    object:_aniPlayer.currentItem];

                                    -(void)playbackFinished:(NSNotification *)notification
                                    [_aniPlayer seekToTime:CMTimeMake(0, 1)];//replay from start
                                    [_aniPlayer play];






                                    share|improve this answer












                                    you can add a AVPlayerItemDidPlayToEndTimeNotification observer and replay video
                                    from start in selector, code like below



                                     //add observer
                                    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification
                                    object:_aniPlayer.currentItem];

                                    -(void)playbackFinished:(NSNotification *)notification
                                    [_aniPlayer seekToTime:CMTimeMake(0, 1)];//replay from start
                                    [_aniPlayer play];







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jun 29 '17 at 3:19









                                    shujucn

                                    915




                                    915




















                                        up vote
                                        0
                                        down vote













                                        my solution in objective-c wth AVQueuePlayer - it seems you have to duplicate the AVPlayerItem and upon finishing playback of first element instantly add another copy. "Kind of" makes sense and works for me without any hiccup



                                        NSURL *videoLoopUrl; 
                                        // as [[NSBundle mainBundle] URLForResource:@"assets/yourVideo" withExtension:@"mp4"]];
                                        AVQueuePlayer *_loopVideoPlayer;

                                        +(void) nextVideoInstance:(NSNotification*)notif

                                        AVPlayerItem *currItem = [AVPlayerItem playerItemWithURL: videoLoopUrl];

                                        [[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(nextVideoInstance:)
                                        name:AVPlayerItemDidPlayToEndTimeNotification
                                        object: currItem];

                                        [_loopVideoPlayer insertItem:currItem afterItem:nil];
                                        [_loopVideoPlayer advanceToNextItem];



                                        +(void) initVideoPlayer
                                        videoCopy1 = [AVPlayerItem playerItemWithURL: videoLoopUrl];
                                        videoCopy2 = [AVPlayerItem playerItemWithURL: videoLoopUrl];
                                        NSArray <AVPlayerItem *> *dummyArray = [NSArray arrayWithObjects: videoCopy1, videoCopy2, nil];
                                        _loopVideoPlayer = [AVQueuePlayer queuePlayerWithItems: dummyArray];

                                        [[NSNotificationCenter defaultCenter] addObserver: self
                                        selector: @selector(nextVideoInstance:)
                                        name: AVPlayerItemDidPlayToEndTimeNotification
                                        object: videoCopy1];

                                        [[NSNotificationCenter defaultCenter] addObserver: self
                                        selector: @selector(nextVideoInstance:)
                                        name: AVPlayerItemDidPlayToEndTimeNotification
                                        object: videoCopy2];



                                        https://gist.github.com/neonm3/06c3b5c911fdd3ca7c7800dccf7202ad






                                        share|improve this answer






















                                        • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
                                          – pmichna
                                          Aug 22 '17 at 12:41














                                        up vote
                                        0
                                        down vote













                                        my solution in objective-c wth AVQueuePlayer - it seems you have to duplicate the AVPlayerItem and upon finishing playback of first element instantly add another copy. "Kind of" makes sense and works for me without any hiccup



                                        NSURL *videoLoopUrl; 
                                        // as [[NSBundle mainBundle] URLForResource:@"assets/yourVideo" withExtension:@"mp4"]];
                                        AVQueuePlayer *_loopVideoPlayer;

                                        +(void) nextVideoInstance:(NSNotification*)notif

                                        AVPlayerItem *currItem = [AVPlayerItem playerItemWithURL: videoLoopUrl];

                                        [[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(nextVideoInstance:)
                                        name:AVPlayerItemDidPlayToEndTimeNotification
                                        object: currItem];

                                        [_loopVideoPlayer insertItem:currItem afterItem:nil];
                                        [_loopVideoPlayer advanceToNextItem];



                                        +(void) initVideoPlayer
                                        videoCopy1 = [AVPlayerItem playerItemWithURL: videoLoopUrl];
                                        videoCopy2 = [AVPlayerItem playerItemWithURL: videoLoopUrl];
                                        NSArray <AVPlayerItem *> *dummyArray = [NSArray arrayWithObjects: videoCopy1, videoCopy2, nil];
                                        _loopVideoPlayer = [AVQueuePlayer queuePlayerWithItems: dummyArray];

                                        [[NSNotificationCenter defaultCenter] addObserver: self
                                        selector: @selector(nextVideoInstance:)
                                        name: AVPlayerItemDidPlayToEndTimeNotification
                                        object: videoCopy1];

                                        [[NSNotificationCenter defaultCenter] addObserver: self
                                        selector: @selector(nextVideoInstance:)
                                        name: AVPlayerItemDidPlayToEndTimeNotification
                                        object: videoCopy2];



                                        https://gist.github.com/neonm3/06c3b5c911fdd3ca7c7800dccf7202ad






                                        share|improve this answer






















                                        • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
                                          – pmichna
                                          Aug 22 '17 at 12:41












                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        my solution in objective-c wth AVQueuePlayer - it seems you have to duplicate the AVPlayerItem and upon finishing playback of first element instantly add another copy. "Kind of" makes sense and works for me without any hiccup



                                        NSURL *videoLoopUrl; 
                                        // as [[NSBundle mainBundle] URLForResource:@"assets/yourVideo" withExtension:@"mp4"]];
                                        AVQueuePlayer *_loopVideoPlayer;

                                        +(void) nextVideoInstance:(NSNotification*)notif

                                        AVPlayerItem *currItem = [AVPlayerItem playerItemWithURL: videoLoopUrl];

                                        [[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(nextVideoInstance:)
                                        name:AVPlayerItemDidPlayToEndTimeNotification
                                        object: currItem];

                                        [_loopVideoPlayer insertItem:currItem afterItem:nil];
                                        [_loopVideoPlayer advanceToNextItem];



                                        +(void) initVideoPlayer
                                        videoCopy1 = [AVPlayerItem playerItemWithURL: videoLoopUrl];
                                        videoCopy2 = [AVPlayerItem playerItemWithURL: videoLoopUrl];
                                        NSArray <AVPlayerItem *> *dummyArray = [NSArray arrayWithObjects: videoCopy1, videoCopy2, nil];
                                        _loopVideoPlayer = [AVQueuePlayer queuePlayerWithItems: dummyArray];

                                        [[NSNotificationCenter defaultCenter] addObserver: self
                                        selector: @selector(nextVideoInstance:)
                                        name: AVPlayerItemDidPlayToEndTimeNotification
                                        object: videoCopy1];

                                        [[NSNotificationCenter defaultCenter] addObserver: self
                                        selector: @selector(nextVideoInstance:)
                                        name: AVPlayerItemDidPlayToEndTimeNotification
                                        object: videoCopy2];



                                        https://gist.github.com/neonm3/06c3b5c911fdd3ca7c7800dccf7202ad






                                        share|improve this answer














                                        my solution in objective-c wth AVQueuePlayer - it seems you have to duplicate the AVPlayerItem and upon finishing playback of first element instantly add another copy. "Kind of" makes sense and works for me without any hiccup



                                        NSURL *videoLoopUrl; 
                                        // as [[NSBundle mainBundle] URLForResource:@"assets/yourVideo" withExtension:@"mp4"]];
                                        AVQueuePlayer *_loopVideoPlayer;

                                        +(void) nextVideoInstance:(NSNotification*)notif

                                        AVPlayerItem *currItem = [AVPlayerItem playerItemWithURL: videoLoopUrl];

                                        [[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(nextVideoInstance:)
                                        name:AVPlayerItemDidPlayToEndTimeNotification
                                        object: currItem];

                                        [_loopVideoPlayer insertItem:currItem afterItem:nil];
                                        [_loopVideoPlayer advanceToNextItem];



                                        +(void) initVideoPlayer
                                        videoCopy1 = [AVPlayerItem playerItemWithURL: videoLoopUrl];
                                        videoCopy2 = [AVPlayerItem playerItemWithURL: videoLoopUrl];
                                        NSArray <AVPlayerItem *> *dummyArray = [NSArray arrayWithObjects: videoCopy1, videoCopy2, nil];
                                        _loopVideoPlayer = [AVQueuePlayer queuePlayerWithItems: dummyArray];

                                        [[NSNotificationCenter defaultCenter] addObserver: self
                                        selector: @selector(nextVideoInstance:)
                                        name: AVPlayerItemDidPlayToEndTimeNotification
                                        object: videoCopy1];

                                        [[NSNotificationCenter defaultCenter] addObserver: self
                                        selector: @selector(nextVideoInstance:)
                                        name: AVPlayerItemDidPlayToEndTimeNotification
                                        object: videoCopy2];



                                        https://gist.github.com/neonm3/06c3b5c911fdd3ca7c7800dccf7202ad







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Aug 22 '17 at 13:06

























                                        answered Aug 22 '17 at 12:15









                                        neon M3

                                        12




                                        12











                                        • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
                                          – pmichna
                                          Aug 22 '17 at 12:41
















                                        • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
                                          – pmichna
                                          Aug 22 '17 at 12:41















                                        While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
                                        – pmichna
                                        Aug 22 '17 at 12:41




                                        While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
                                        – pmichna
                                        Aug 22 '17 at 12:41










                                        up vote
                                        0
                                        down vote













                                        The following is working for me in WKWebView in swift 4.1
                                        The main part of the WKWebView in WKwebviewConfiguration



                                        wkwebView.navigationDelegate = self
                                        wkwebView.allowsBackForwardNavigationGestures = true
                                        self.wkwebView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
                                        let config = WKWebViewConfiguration()
                                        config.allowsInlineMediaPlayback = true
                                        wkwebView = WKWebView(frame: wkwebView.frame, configuration: config)
                                        self.view.addSubview(wkwebView)
                                        self.wkwebView.load(NSURLRequest(url: URL(string: self.getUrl())!) as URLRequest)





                                        share|improve this answer
























                                          up vote
                                          0
                                          down vote













                                          The following is working for me in WKWebView in swift 4.1
                                          The main part of the WKWebView in WKwebviewConfiguration



                                          wkwebView.navigationDelegate = self
                                          wkwebView.allowsBackForwardNavigationGestures = true
                                          self.wkwebView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
                                          let config = WKWebViewConfiguration()
                                          config.allowsInlineMediaPlayback = true
                                          wkwebView = WKWebView(frame: wkwebView.frame, configuration: config)
                                          self.view.addSubview(wkwebView)
                                          self.wkwebView.load(NSURLRequest(url: URL(string: self.getUrl())!) as URLRequest)





                                          share|improve this answer






















                                            up vote
                                            0
                                            down vote










                                            up vote
                                            0
                                            down vote









                                            The following is working for me in WKWebView in swift 4.1
                                            The main part of the WKWebView in WKwebviewConfiguration



                                            wkwebView.navigationDelegate = self
                                            wkwebView.allowsBackForwardNavigationGestures = true
                                            self.wkwebView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
                                            let config = WKWebViewConfiguration()
                                            config.allowsInlineMediaPlayback = true
                                            wkwebView = WKWebView(frame: wkwebView.frame, configuration: config)
                                            self.view.addSubview(wkwebView)
                                            self.wkwebView.load(NSURLRequest(url: URL(string: self.getUrl())!) as URLRequest)





                                            share|improve this answer












                                            The following is working for me in WKWebView in swift 4.1
                                            The main part of the WKWebView in WKwebviewConfiguration



                                            wkwebView.navigationDelegate = self
                                            wkwebView.allowsBackForwardNavigationGestures = true
                                            self.wkwebView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
                                            let config = WKWebViewConfiguration()
                                            config.allowsInlineMediaPlayback = true
                                            wkwebView = WKWebView(frame: wkwebView.frame, configuration: config)
                                            self.view.addSubview(wkwebView)
                                            self.wkwebView.load(NSURLRequest(url: URL(string: self.getUrl())!) as URLRequest)






                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Jul 19 at 13:46









                                            Nrv

                                            1255




                                            1255




















                                                up vote
                                                0
                                                down vote













                                                What I did is to make it loop playing, like my code below:



                                                [player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0)
                                                queue:dispatch_get_main_queue() usingBlock:^(CMTime time)
                                                float current = CMTimeGetSeconds(time);
                                                float total = CMTimeGetSeconds([playerItem duration]);
                                                if (current >= total)
                                                [[self.player currentItem] seekToTime:kCMTimeZero];
                                                [self.player play];

                                                ];





                                                share|improve this answer


























                                                  up vote
                                                  0
                                                  down vote













                                                  What I did is to make it loop playing, like my code below:



                                                  [player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0)
                                                  queue:dispatch_get_main_queue() usingBlock:^(CMTime time)
                                                  float current = CMTimeGetSeconds(time);
                                                  float total = CMTimeGetSeconds([playerItem duration]);
                                                  if (current >= total)
                                                  [[self.player currentItem] seekToTime:kCMTimeZero];
                                                  [self.player play];

                                                  ];





                                                  share|improve this answer
























                                                    up vote
                                                    0
                                                    down vote










                                                    up vote
                                                    0
                                                    down vote









                                                    What I did is to make it loop playing, like my code below:



                                                    [player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0)
                                                    queue:dispatch_get_main_queue() usingBlock:^(CMTime time)
                                                    float current = CMTimeGetSeconds(time);
                                                    float total = CMTimeGetSeconds([playerItem duration]);
                                                    if (current >= total)
                                                    [[self.player currentItem] seekToTime:kCMTimeZero];
                                                    [self.player play];

                                                    ];





                                                    share|improve this answer














                                                    What I did is to make it loop playing, like my code below:



                                                    [player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0)
                                                    queue:dispatch_get_main_queue() usingBlock:^(CMTime time)
                                                    float current = CMTimeGetSeconds(time);
                                                    float total = CMTimeGetSeconds([playerItem duration]);
                                                    if (current >= total)
                                                    [[self.player currentItem] seekToTime:kCMTimeZero];
                                                    [self.player play];

                                                    ];






                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Sep 14 at 13:14









                                                    Sid Singh

                                                    34




                                                    34










                                                    answered May 2 at 12:37









                                                    Victor John

                                                    341312




                                                    341312




















                                                        up vote
                                                        0
                                                        down vote













                                                        Swift 4.2 in Xcode 10.1.



                                                        Yes, there is a relatively easy way of looping a video in AVKit/AVFoundation using AVQueuePlayer(), Key-Value Observation (KVO) technique and a token for it.



                                                        This definitely works for a bunch of H.264/HEVC videos with a minimal burden for CPU.



                                                        Here's a code:



                                                        import UIKit
                                                        import AVFoundation
                                                        import AVKit

                                                        class ViewController: UIViewController

                                                        private let player = AVQueuePlayer()
                                                        let clips = ["01", "02", "03", "04", "05", "06", "07"]
                                                        private var token: NSKeyValueObservation?
                                                        var avPlayerView = AVPlayerViewController()

                                                        override func viewDidAppear(_ animated: Bool)
                                                        super.viewDidAppear(true)

                                                        self.addAllVideosToPlayer()
                                                        present(avPlayerView, animated: true, completion: self.player.play() )


                                                        func addAllVideosToPlayer()
                                                        avPlayerView.player = player

                                                        for clip in clips
                                                        let urlPath = Bundle.main.path(forResource: clip, ofType: "m4v")!
                                                        let url = URL(fileURLWithPath: urlPath)
                                                        let playerItem = AVPlayerItem(url: url)
                                                        player.insert(playerItem, after: player.items().last)

                                                        token = player.observe(.currentItem) [weak self] player, _ in
                                                        if self!.player.items().count == 1 self?.addAllVideosToPlayer()









                                                        share|improve this answer


























                                                          up vote
                                                          0
                                                          down vote













                                                          Swift 4.2 in Xcode 10.1.



                                                          Yes, there is a relatively easy way of looping a video in AVKit/AVFoundation using AVQueuePlayer(), Key-Value Observation (KVO) technique and a token for it.



                                                          This definitely works for a bunch of H.264/HEVC videos with a minimal burden for CPU.



                                                          Here's a code:



                                                          import UIKit
                                                          import AVFoundation
                                                          import AVKit

                                                          class ViewController: UIViewController

                                                          private let player = AVQueuePlayer()
                                                          let clips = ["01", "02", "03", "04", "05", "06", "07"]
                                                          private var token: NSKeyValueObservation?
                                                          var avPlayerView = AVPlayerViewController()

                                                          override func viewDidAppear(_ animated: Bool)
                                                          super.viewDidAppear(true)

                                                          self.addAllVideosToPlayer()
                                                          present(avPlayerView, animated: true, completion: self.player.play() )


                                                          func addAllVideosToPlayer()
                                                          avPlayerView.player = player

                                                          for clip in clips
                                                          let urlPath = Bundle.main.path(forResource: clip, ofType: "m4v")!
                                                          let url = URL(fileURLWithPath: urlPath)
                                                          let playerItem = AVPlayerItem(url: url)
                                                          player.insert(playerItem, after: player.items().last)

                                                          token = player.observe(.currentItem) [weak self] player, _ in
                                                          if self!.player.items().count == 1 self?.addAllVideosToPlayer()









                                                          share|improve this answer
























                                                            up vote
                                                            0
                                                            down vote










                                                            up vote
                                                            0
                                                            down vote









                                                            Swift 4.2 in Xcode 10.1.



                                                            Yes, there is a relatively easy way of looping a video in AVKit/AVFoundation using AVQueuePlayer(), Key-Value Observation (KVO) technique and a token for it.



                                                            This definitely works for a bunch of H.264/HEVC videos with a minimal burden for CPU.



                                                            Here's a code:



                                                            import UIKit
                                                            import AVFoundation
                                                            import AVKit

                                                            class ViewController: UIViewController

                                                            private let player = AVQueuePlayer()
                                                            let clips = ["01", "02", "03", "04", "05", "06", "07"]
                                                            private var token: NSKeyValueObservation?
                                                            var avPlayerView = AVPlayerViewController()

                                                            override func viewDidAppear(_ animated: Bool)
                                                            super.viewDidAppear(true)

                                                            self.addAllVideosToPlayer()
                                                            present(avPlayerView, animated: true, completion: self.player.play() )


                                                            func addAllVideosToPlayer()
                                                            avPlayerView.player = player

                                                            for clip in clips
                                                            let urlPath = Bundle.main.path(forResource: clip, ofType: "m4v")!
                                                            let url = URL(fileURLWithPath: urlPath)
                                                            let playerItem = AVPlayerItem(url: url)
                                                            player.insert(playerItem, after: player.items().last)

                                                            token = player.observe(.currentItem) [weak self] player, _ in
                                                            if self!.player.items().count == 1 self?.addAllVideosToPlayer()









                                                            share|improve this answer














                                                            Swift 4.2 in Xcode 10.1.



                                                            Yes, there is a relatively easy way of looping a video in AVKit/AVFoundation using AVQueuePlayer(), Key-Value Observation (KVO) technique and a token for it.



                                                            This definitely works for a bunch of H.264/HEVC videos with a minimal burden for CPU.



                                                            Here's a code:



                                                            import UIKit
                                                            import AVFoundation
                                                            import AVKit

                                                            class ViewController: UIViewController

                                                            private let player = AVQueuePlayer()
                                                            let clips = ["01", "02", "03", "04", "05", "06", "07"]
                                                            private var token: NSKeyValueObservation?
                                                            var avPlayerView = AVPlayerViewController()

                                                            override func viewDidAppear(_ animated: Bool)
                                                            super.viewDidAppear(true)

                                                            self.addAllVideosToPlayer()
                                                            present(avPlayerView, animated: true, completion: self.player.play() )


                                                            func addAllVideosToPlayer()
                                                            avPlayerView.player = player

                                                            for clip in clips
                                                            let urlPath = Bundle.main.path(forResource: clip, ofType: "m4v")!
                                                            let url = URL(fileURLWithPath: urlPath)
                                                            let playerItem = AVPlayerItem(url: url)
                                                            player.insert(playerItem, after: player.items().last)

                                                            token = player.observe(.currentItem) [weak self] player, _ in
                                                            if self!.player.items().count == 1 self?.addAllVideosToPlayer()










                                                            share|improve this answer














                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited Nov 29 at 14:15

























                                                            answered Nov 11 at 12:18









                                                            ARGeo

                                                            5,29052047




                                                            5,29052047




















                                                                up vote
                                                                -1
                                                                down vote













                                                                use AVPlayerViewController below code, its working for me



                                                                 let type : String! = "mp4"
                                                                let targetURL : String? = NSBundle.mainBundle().pathForResource("Official Apple MacBook Air Video YouTube", ofType: "mp4")

                                                                let videoURL = NSURL(fileURLWithPath:targetURL!)


                                                                let player = AVPlayer(URL: videoURL)
                                                                let playerController = AVPlayerViewController()

                                                                playerController.player = player
                                                                self.addChildViewController(playerController)
                                                                self.playView.addSubview(playerController.view)
                                                                playerController.view.frame = playView.bounds

                                                                player.play()


                                                                All controls to be showed, hope its helpful






                                                                share|improve this answer
























                                                                  up vote
                                                                  -1
                                                                  down vote













                                                                  use AVPlayerViewController below code, its working for me



                                                                   let type : String! = "mp4"
                                                                  let targetURL : String? = NSBundle.mainBundle().pathForResource("Official Apple MacBook Air Video YouTube", ofType: "mp4")

                                                                  let videoURL = NSURL(fileURLWithPath:targetURL!)


                                                                  let player = AVPlayer(URL: videoURL)
                                                                  let playerController = AVPlayerViewController()

                                                                  playerController.player = player
                                                                  self.addChildViewController(playerController)
                                                                  self.playView.addSubview(playerController.view)
                                                                  playerController.view.frame = playView.bounds

                                                                  player.play()


                                                                  All controls to be showed, hope its helpful






                                                                  share|improve this answer






















                                                                    up vote
                                                                    -1
                                                                    down vote










                                                                    up vote
                                                                    -1
                                                                    down vote









                                                                    use AVPlayerViewController below code, its working for me



                                                                     let type : String! = "mp4"
                                                                    let targetURL : String? = NSBundle.mainBundle().pathForResource("Official Apple MacBook Air Video YouTube", ofType: "mp4")

                                                                    let videoURL = NSURL(fileURLWithPath:targetURL!)


                                                                    let player = AVPlayer(URL: videoURL)
                                                                    let playerController = AVPlayerViewController()

                                                                    playerController.player = player
                                                                    self.addChildViewController(playerController)
                                                                    self.playView.addSubview(playerController.view)
                                                                    playerController.view.frame = playView.bounds

                                                                    player.play()


                                                                    All controls to be showed, hope its helpful






                                                                    share|improve this answer












                                                                    use AVPlayerViewController below code, its working for me



                                                                     let type : String! = "mp4"
                                                                    let targetURL : String? = NSBundle.mainBundle().pathForResource("Official Apple MacBook Air Video YouTube", ofType: "mp4")

                                                                    let videoURL = NSURL(fileURLWithPath:targetURL!)


                                                                    let player = AVPlayer(URL: videoURL)
                                                                    let playerController = AVPlayerViewController()

                                                                    playerController.player = player
                                                                    self.addChildViewController(playerController)
                                                                    self.playView.addSubview(playerController.view)
                                                                    playerController.view.frame = playView.bounds

                                                                    player.play()


                                                                    All controls to be showed, hope its helpful







                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered May 3 '16 at 10:00









                                                                    Iyyappan Ravi

                                                                    2,003724




                                                                    2,003724




















                                                                        up vote
                                                                        -2
                                                                        down vote













                                                                        /* "numberOfLoops" is the number of times that the sound will return to the beginning upon reaching the end. 
                                                                        A value of zero means to play the sound just once.
                                                                        A value of one will result in playing the sound twice, and so on..
                                                                        Any negative number will loop indefinitely until stopped.
                                                                        */
                                                                        @property NSInteger numberOfLoops;


                                                                        This property is already defined inside AVAudioPlayer. Hope this can help you.
                                                                        I'm using Xcode 6.3.






                                                                        share|improve this answer


















                                                                        • 8




                                                                          that's for audio, not for AVPlayer
                                                                          – Yariv
                                                                          May 21 '15 at 21:23














                                                                        up vote
                                                                        -2
                                                                        down vote













                                                                        /* "numberOfLoops" is the number of times that the sound will return to the beginning upon reaching the end. 
                                                                        A value of zero means to play the sound just once.
                                                                        A value of one will result in playing the sound twice, and so on..
                                                                        Any negative number will loop indefinitely until stopped.
                                                                        */
                                                                        @property NSInteger numberOfLoops;


                                                                        This property is already defined inside AVAudioPlayer. Hope this can help you.
                                                                        I'm using Xcode 6.3.






                                                                        share|improve this answer


















                                                                        • 8




                                                                          that's for audio, not for AVPlayer
                                                                          – Yariv
                                                                          May 21 '15 at 21:23












                                                                        up vote
                                                                        -2
                                                                        down vote










                                                                        up vote
                                                                        -2
                                                                        down vote









                                                                        /* "numberOfLoops" is the number of times that the sound will return to the beginning upon reaching the end. 
                                                                        A value of zero means to play the sound just once.
                                                                        A value of one will result in playing the sound twice, and so on..
                                                                        Any negative number will loop indefinitely until stopped.
                                                                        */
                                                                        @property NSInteger numberOfLoops;


                                                                        This property is already defined inside AVAudioPlayer. Hope this can help you.
                                                                        I'm using Xcode 6.3.






                                                                        share|improve this answer














                                                                        /* "numberOfLoops" is the number of times that the sound will return to the beginning upon reaching the end. 
                                                                        A value of zero means to play the sound just once.
                                                                        A value of one will result in playing the sound twice, and so on..
                                                                        Any negative number will loop indefinitely until stopped.
                                                                        */
                                                                        @property NSInteger numberOfLoops;


                                                                        This property is already defined inside AVAudioPlayer. Hope this can help you.
                                                                        I'm using Xcode 6.3.







                                                                        share|improve this answer














                                                                        share|improve this answer



                                                                        share|improve this answer








                                                                        edited Apr 23 '15 at 10:31









                                                                        Ajith R Nayak

                                                                        2,66832534




                                                                        2,66832534










                                                                        answered Apr 23 '15 at 9:52









                                                                        Julz

                                                                        12




                                                                        12







                                                                        • 8




                                                                          that's for audio, not for AVPlayer
                                                                          – Yariv
                                                                          May 21 '15 at 21:23












                                                                        • 8




                                                                          that's for audio, not for AVPlayer
                                                                          – Yariv
                                                                          May 21 '15 at 21:23







                                                                        8




                                                                        8




                                                                        that's for audio, not for AVPlayer
                                                                        – Yariv
                                                                        May 21 '15 at 21:23




                                                                        that's for audio, not for AVPlayer
                                                                        – Yariv
                                                                        May 21 '15 at 21:23

















                                                                        draft saved

                                                                        draft discarded
















































                                                                        Thanks for contributing an answer to Stack Overflow!


                                                                        • Please be sure to answer the question. Provide details and share your research!

                                                                        But avoid


                                                                        • Asking for help, clarification, or responding to other answers.

                                                                        • Making statements based on opinion; back them up with references or personal experience.

                                                                        To learn more, see our tips on writing great answers.





                                                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                        Please pay close attention to the following guidance:


                                                                        • Please be sure to answer the question. Provide details and share your research!

                                                                        But avoid


                                                                        • Asking for help, clarification, or responding to other answers.

                                                                        • Making statements based on opinion; back them up with references or personal experience.

                                                                        To learn more, see our tips on writing great answers.




                                                                        draft saved


                                                                        draft discarded














                                                                        StackExchange.ready(
                                                                        function ()
                                                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5361145%2flooping-a-video-with-avfoundation-avplayer%23new-answer', 'question_page');

                                                                        );

                                                                        Post as a guest















                                                                        Required, but never shown





















































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown

































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown







                                                                        Popular posts from this blog

                                                                        Top Tejano songwriter Luis Silva dead of heart attack at 64

                                                                        政党

                                                                        天津地下鉄3号線