1

我的代码:

    playerController = [[AVPlayerViewController alloc]init];


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

    playerController.player = [AVPlayer playerWithURL:videoURL];

    playerController.view.frame = self.view.frame;

    [playerController.player seekToTime:kCMTimeZero];

    playerController.player.actionAtItemEnd = AVPlayerActionAtItemEndPause;

    [playerController.player play];

    [self presentViewController:playerController animated:YES completion:nil];

在这里,我在特定时间停止视频。

4

2 回答 2

4

你要做的是:

在 .h 文件中创建以下属性:

@property (strong) id playerObserver;

在 .m 文件中添加此代码:

AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]];

[player play];

self.playerObserver = [player addBoundaryTimeObserverForTimes:@[[NSValue valueWithCMTime:CMTimeMake(1, 1000)]] queue:NULL usingBlock:^{
    NSLog(@"Video started playing");

    [player performSelector:@selector(pause) withObject:nil afterDelay:3.0]; //will pause player after 3.0 seconds
    [player removeTimeObserver:self.playerObserver];
}];

此代码将跟踪视频何时开始播放。然后,在您想要的任何时间(此处为 3 秒)之后,它将暂停播放器。至少,我们移除了玩家身上的观察者。

希望这可以帮助 !

于 2016-06-30T08:58:52.120 回答
2

SWFIT 3 版本来自@Anthonin C 的回答。

player = AVPlayer(url: tmpURL)
player?.perform(#selector(player?.pause), with: nil, afterDelay: 3)
于 2017-03-02T02:16:19.610 回答