AVPlayer视频SeekToTime

时间:2011-06-13 11:18:58

标签: iphone objective-c ipad ipod

我正在使用AVPlayer使用滑块和部分按钮播放我的视频。 这是我使用按钮前进和后退的方法。

-(IBAction)MoveForward
{
    //int value = timeSlider.value*36000 + 10;
    //CMTime newTime = CMTimeMakeWithSeconds(value, playspeed);
    //CMTime newTime = CMTimeMake(value,(playspeed*timeSlider.maximumValue));

    CMTime newTime = CMTimeMakeWithSeconds(timeSlider.value, playspeed);
    newTime.value += 60;
    [player seekToTime: newTime];
}

-(IBAction)MoveBackward
{
    CMTime newTime = CMTimeMakeWithSeconds(timeSlider.value-1, playspeed);
    [player seekToTime: newTime];
}

我的问题是,Seek的工作时间不正常。根据秒数导航到下一帧。我需要小心地移动下一帧。帮帮我...

2 个答案:

答案 0 :(得分:25)

我真的不了解你的代码,你真的不需要单独的方法来向前和向后移动,你可以使用相同的方法。我有一个有效的AVPlayer电影播放器​​,我会告诉你我是如何做滑块的。

    -(IBAction)sliding:(id)sender{

        CMTime newTime = CMTimeMakeWithSeconds(seeker.value, 1);
        [self.player seekToTime:newTime];
     }

    -(void)setSlider{

        sliderTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self    selector:@selector(updateSlider) userInfo:nil repeats:YES]retain];
        self.seeker.maximumValue = [self durationInSeconds];
        [seeker addTarget:self action:@selector(sliding:) forControlEvents:UIControlEventValueChanged];
        seeker.minimumValue = 0.0;
        seeker.continuous = YES;  
    }

    - (void)updateSlider {

        self.seeker.maximumValue = [self durationInSeconds];
        self.seeker.value = [self currentTimeInSeconds];
    }

    - (Float64)durationInSeconds {
            Float64 dur = CMTimeGetSeconds(duration);
        return dur;
    }


    - (Float64)currentTimeInSeconds {
            Float64 dur = CMTimeGetSeconds([self.player currentTime]);
        return dur;
      }

就是这样,这段代码中有两个陷阱,首先,duration属性返回一个CMTime变量,你必须将它转换为一个浮点数,同样,这将返回原始的秒数,你必须将其转换为h:mm:如果要显示时间标签,则为ss。其次,updateSlider方法每秒由一个计时器触发。 祝你好运。

答案 1 :(得分:4)

以下代码片段对我有用:

CMTime videoLength = self.mPlayer.currentItem.asset.duration;  // Gets the video duration
float videoLengthInSeconds = videoLength.value/videoLength.timescale; // Transfers the CMTime duration into seconds

[self.mPlayer seekToTime:CMTimeMakeWithSeconds(videoLengthInSeconds * [slider value], 1) 
                   completionHandler:^(BOOL finished) 
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                    isSeeking = NO;
                    // Do some stuff
                });
            }];