iOS AVPlayer 在快退/快进后无法暂停

Posted

技术标签:

【中文标题】iOS AVPlayer 在快退/快进后无法暂停【英文标题】:iOS AVPlayer not able to pause after rewind/fastforward 【发布时间】:2020-03-10 07:05:07 【问题描述】:

场景

如果 AVPlayer 正在播放视频,请倒退 15 秒并继续播放。 如果 AVPlayer 暂停,则倒退 15 秒并保持暂停状态。

问题

在调用seekToTime: 后,我无法暂停视频。

问题

调用 seekToTime 后如何暂停播放器?

我已经尝试了[Avplayer pause] 以及将Avplayer.rate 设置为0。致电seekToTime 后,没有任何东西可以停止视频。

我的代码

@property (strong, nonatomic) AVPlayer *player;
.
.
.
@synthesize player
.
.
.
- (IBAction)rewind:(id)sender 
    NSLog(@"rewind called");

    int secToRewind = DEFAULT_REWIND_SEC;
    int time = CMTimeGetSeconds(player.currentTime) - secToRewind;
    CMTime newTime = CMTimeMakeWithSeconds(time, 1);
    CMTime tolerance = CMTimeMakeWithSeconds(1, 1);

    __weak __typeof__(self) weakSelf = self;
    int playerRate = player.rate;
    [player seekToTime:newTime toleranceBefore: tolerance toleranceAfter: tolerance completionHandler:^(BOOL finished) 

        NSLog(@"seekToTime called");

        __strong __typeof__(self) strongSelf = weakSelf;
        if (playerRate == 0) 
            [player pause];
        

    ];



【问题讨论】:

欢迎来到 ***。我可以推荐一个很棒的图书馆吗? pod 'FreeStreamer'. 【参考方案1】:

我猜的问题在于内容的缓冲,因为你要往后 X(应该是 15)秒,这是很多,它需要一点时间来缓冲内容。

试试下面的代码...

@property (strong, nonatomic) AVPlayer *player;
.
.
.
@synthesize player
.
.
.
- (IBAction)rewind:(id)sender 
    NSLog(@"rewind called");

    int secToRewind = DEFAULT_REWIND_SEC;
    int time = CMTimeGetSeconds(player.currentTime) - secToRewind;
    CMTime newTime = CMTimeMakeWithSeconds(time, 1);
    CMTime tolerance = CMTimeMakeWithSeconds(1, 1);

    __weak __typeof__(self) weakSelf = self;
    int playerRate = player.rate;
    [player seekToTime:newTime toleranceBefore: tolerance toleranceAfter: tolerance completionHandler:^(BOOL finished) 



        do 

          NSLog(@"Successful rewind");

         __strong __typeof__(self) strongSelf = weakSelf;
         if (playerRate == 0) 
             [player playAt:[player currentTime]];
         

        while (completed == true);

    ];



【讨论】:

感谢您的回答。我的问题不清楚,所以我更新了问题。致电seekToTime: 后,我无法暂停播放器。如果播放器暂停并且用户单击倒带按钮,我需要暂停它。它应该向后移动并保持暂停状态。【参考方案2】:

我找到了导致自动播放的代码。有一个观察者附在玩家身上。每当播放器项目处于AVPlayerItemStatusReadyToPlay 时,无论播放/暂停状态如何,播放器都会被设置为播放该项目。

带有修复的代码

case AVPlayerItemStatusReadyToPlay: 

            if ([playButton isSelected]) 
                 [self.player play];
            


        

【讨论】:

以上是关于iOS AVPlayer 在快退/快进后无法暂停的主要内容,如果未能解决你的问题,请参考以下文章

iOS:使用 AudioQueue 时快进音频

寻找 iOS 风格的快进和快退按钮图像

我在谷歌存储桶上的视频无法快进或快退

我可以隐藏 iOS 锁屏音乐控件中的快退按钮吗?

QAudioOutput 快进、快退

利用MediaPlayer实现在线音频的播放(支持进度条拖动快进快退)