4

我正在开发的 iOS 应用程序中播放直播电视流。我正在使用 MPMoviePlayerController 并且需要创建自定义控件(播放、暂停、进度条等)。

在尝试获取 currentPlaybackTime 时,我注意到一些非常奇怪的行为:它像没有明天一样跳来跳去。

持续时间属性稳定在 7170 秒,它永远不会改变。playableDuration 属性随着电影的播放而增长,并且 currentPlaybackTime 不断地跳来跳去:

playableDuration: 7179.875398
currentPlaybackTime: 7170.181204
--------------
playableDuration: 7187.655396
currentPlaybackTime: 7171.079373
--------------
playableDuration: 7197.208128
currentPlaybackTime: 7172.131998
--------------
playableDuration: 7197.208128
currentPlaybackTime: 7173.139118
--------------
playableDuration: 7197.208128
currentPlaybackTime: 7174.139770
--------------
playableDuration: 7197.208128
currentPlaybackTime: 7175.139666
--------------
playableDuration: 7197.208128
currentPlaybackTime: 7176.140805
--------------
playableDuration: 7197.208128
currentPlaybackTime: 7177.139057
--------------
playableDuration: 7197.208128
currentPlaybackTime: 7178.139128
--------------
playableDuration: 7197.208128
currentPlaybackTime: 7179.139911
--------------
playableDuration: 7199.822063
currentPlaybackTime: 7170.095921
--------------
playableDuration: 7209.959997
currentPlaybackTime: 7171.140078
--------------
playableDuration: 7209.959997
currentPlaybackTime: 7172.140715
--------------
playableDuration: 7209.959997
currentPlaybackTime: 7173.076899
--------------
playableDuration: 7209.959997
currentPlaybackTime: 7174.139497
--------------
playableDuration: 7209.959997
currentPlaybackTime: 7175.139907
--------------
playableDuration: 7209.959997
currentPlaybackTime: 7176.138233
--------------
playableDuration: 7209.959997
currentPlaybackTime: 7177.139523
--------------
playableDuration: 7209.959997
currentPlaybackTime: 7178.135736
--------------
playableDuration: 7209.959997
currentPlaybackTime: 7179.138545
--------------
playableDuration: 7209.959997
currentPlaybackTime: 7180.111102
--------------
playableDuration: 7219.939398
currentPlaybackTime: 7171.132859
--------------
playableDuration: 7219.939398
currentPlaybackTime: 7172.128591
--------------
playableDuration: 7219.939398
currentPlaybackTime: 7173.136137
--------------
playableDuration: 7219.939398
currentPlaybackTime: 7174.135416
--------------
playableDuration: 7219.939398
currentPlaybackTime: 7175.137593
--------------
playableDuration: 7219.939398
currentPlaybackTime: 7176.131573
--------------
playableDuration: 7219.939398
currentPlaybackTime: 7177.137812
--------------
playableDuration: 7219.939398
currentPlaybackTime: 7178.131751
--------------
playableDuration: 7219.939398
currentPlaybackTime: 7179.135922
--------------
playableDuration: 7219.939398
currentPlaybackTime: 7180.139828
--------------
playableDuration: 7229.752731
currentPlaybackTime: 7171.129731
--------------
playableDuration: 7229.752731
currentPlaybackTime: 7172.136576
--------------
playableDuration: 7229.752731
currentPlaybackTime: 7173.132362
--------------
playableDuration: 7229.752731
currentPlaybackTime: 7174.139998

如您所见,它从 7179 跳到 7170,然后从 7180 跳回 7171,一次又一次……换句话说,似乎没有办法获得稳定递增的 currentPlaybackTime。这导致我的进度条跳来跳去,进度标签(0:04:30 / 2:00:00)也表现得非常奇怪。

有没有人见过这种行为?有解决方法吗?

4

1 回答 1

0

最后我通过使用我自己的内部属性来跟踪播放时间解决了这个问题:

@property (nonatomic) NSTimeInterval previousPlaybackTime;
@property (nonatomic) NSTimeInterval currentPlaybackTime;
@property (strong, nonatomic) NSTimer *timer;

- (void)setCurrentPlaybackTime:(NSTimeInterval)currentPlaybackTime {
    BOOL force = (self.moviePlayer.playbackState == MPMoviePlaybackStatePaused);
    [self setCurrentPlaybackTime:currentPlaybackTime force:force];
}

- (void)setCurrentPlaybackTime:(NSTimeInterval)currentPlaybackTime force:(BOOL)force {
    if (currentPlaybackTime > self.moviePlayer.duration) {
        currentPlaybackTime = self.moviePlayer.duration;
    }

    if (!force && currentPlaybackTime < _previousPlaybackTime) {
        // Don't go backwards in time if it's less then 50 seconds (solves the 10 seconds going back problem)
        if (_previousPlaybackTime - currentPlaybackTime < 50) {
            return;
        }
    }

    if (!force && _previousPlaybackTime && currentPlaybackTime > _previousPlaybackTime) {
        // Don't go forward in time if it's more then 10 seconds (solves the huge jumps when using the progress bar)
        if (currentPlaybackTime - _previousPlaybackTime > 10) {
            return;
        }
    }

    _previousPlaybackTime = currentPlaybackTime;
    _currentPlaybackTime = currentPlaybackTime;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timedJob) userInfo:nil repeats:YES];
    [self.timer fire];
}

- (void)timedJob {
    if (self.moviePlayer.playbackState == MPMoviePlaybackStateSeekingBackward) return;
    if (self.moviePlayer.playbackState == MPMoviePlaybackStateSeekingForward) return;

    self.currentPlaybackTime = self.moviePlayer.currentPlaybackTime;
}

所以在我的界面中,我现在使用 self.currentPlaybackTime,它内置了跳跃保护,可以这么说。当用户使用滑块更改播放位置时,您使用 force:YES 参数。

于 2014-01-07T10:02:16.697 回答