iphone - AVPlayer 视频 SeekToTime

标签 iphone objective-c ipad ipod

我正在使用 AVPlayer 来使用 slider 和一些按钮播放我的视频。 这是我使用按钮向前和向后移动的方法。

-(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 的时间不能正常工作。基于秒导航到下一帧。我需要每分钟移动下一帧。帮帮我……

最佳答案

我不太理解你的代码,你真的不需要单独的方法来向前和向后移动,你可以对两者使用相同的方法。我有一个可用的 AVPlayer 电影播放器​​,我将向您展示我是如何制作 slider 部分的。

-(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 变量,您必须将其转换为 float ,此外,这将返回原始秒数,您必须将其转换为h:mm:ss 如果你想显示时间标签。其次,updateSlider 方法每秒由计时器触发。 祝你好运。

关于iphone - AVPlayer 视频 SeekToTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6329706/

相关文章:

iphone - 如何将变量参数传递给另一个方法?

iphone - 如何调整表格 View 布局?

ios - 接收器没有带标识符的 segue ...以编程方式创建的 segue

iOS 通过 UISplitView 呈现 View 可能吗?

iphone - ipad锁定: launch app on boot?

ios - iPad:两个导航 Controller 同时管理两个 View Controller

iphone - aurioTouch2录制问题。我需要将数据从一个AudioBufferList添加到另一个

ios - stringByReplacingOccurrencesOfString没有给我想要的结果

iphone - 在 iOS 上保存 ID3 标签

ios - CATextLayer 上来自 iOS 6 的不需要的垂直填充