iOS AVPlayer 在 rewind/fastforward 后无法暂停
iOS AVPlayer not able to pause after rewind/fastforward
场景
- 如果 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];
}
}];
}
我猜问题出在内容的缓冲范围内,因为您要倒退 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);
}];
}
我找到了导致自动播放的代码。有一个观察员附在玩家身上。每当播放器项目处于 AVPlayerItemStatusReadyToPlay
时,无论 play/pause 状态如何,播放器都被设置为播放该项目。
有修复的代码
case AVPlayerItemStatusReadyToPlay: {
if ([playButton isSelected]) {
[self.player play];
}
}
场景
- 如果 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];
}
}];
}
我猜问题出在内容的缓冲范围内,因为您要倒退 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);
}];
}
我找到了导致自动播放的代码。有一个观察员附在玩家身上。每当播放器项目处于 AVPlayerItemStatusReadyToPlay
时,无论 play/pause 状态如何,播放器都被设置为播放该项目。
有修复的代码
case AVPlayerItemStatusReadyToPlay: {
if ([playButton isSelected]) {
[self.player play];
}
}