Can I test when IOS4 AVPlayer seekToTime completes?

I use AVFoundationfor implementation AVPlayer. I want to continuously contact a video clip, so I register AVPlayerItemDidPlayToEndTimeNotificationto call this method:

- (void)player1ItemDidReachEnd:(NSNotification *)notification
{ 
 dispatch_async(dispatch_get_main_queue(),
       ^{
        [player1 seekToTime:kCMTimeZero]; 
        [player1 play];
       });
}

It works for a while, but ultimately loses stops, presumably due to asynchronous termination seekToTime. How can I make this code bulletproof?

+3
source share
4 answers

, AVPlayerActionAtItemEnd AVPlayer AVPlayerActionAtItemEndNone - AVPlayerActionAtItemEndPause. AVPlayer , . , seekToTime .

+5
player.actionAtItemEnd = AVPlayerActionAtItemEndNone; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification object:[player currentItem]];

(void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
 [p seekToTime:kCMTimeZero];
}   
+5

in Controller.h add

AVPlayer *myplayer;

in Controller.m add

#import <CoreMedia/CoreMedia.h>

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

and add function from selector

- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *playerItem = [notification object];
[playerItem seekToTime:kCMTimeZero];
[myplayer play];

}

This helps me iterate over the video, but you need to cache it somehow (for a smooth loop)

0
source
- (void)playLogo {
NSString *path = [[NSBundle mainBundle] pathForResource:@"logo" 
                                                 ofType:@"m4v" 
                                            inDirectory:@"../Documents"];

self.myplayerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:path]];
self.myplayer = [AVPlayer playerWithPlayerItem:self.myplayerItem];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[self.myplayer currentItem]];
[videoView setPlayer:self.myplayer];
[videoView setAlpha:0.5f];
[self.myplayer play]; }

- (void)playerItemDidReachEnd:(NSNotification *)notification {
[videoView setAlpha:0.0f];
[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:AVPlayerItemDidPlayToEndTimeNotification 
                                              object:[self.myplayer currentItem]];
[self playLogo];    }
0
source

Source: https://habr.com/ru/post/1772419/


All Articles