AVPlayer 视频 SeekToTime

发布于 2024-11-15 01:47:20 字数 625 浏览 2 评论 0原文

我正在使用 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 的时间无法正常工作。基于秒导航到下一帧。我需要仔细移动下一帧。帮我...

I'm using AVPlayer for play my video using slider and Some Buttons.
Here is my methods for moving forward and backward using buttons.

-(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];
}

My Problem on this is the time to Seek is not working properly. That navigate to next frame based on seconds. I need to move next frame minutely. Help me...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椒妓 2024-11-22 01:47:20

我不太理解你的代码,你真的不需要单独的方法来向前和向后移动,你可以对两者使用相同的方法。我有一个可用的 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 方法每秒由计时器触发。
祝你好运。

I don't really understand your code, you don't really need separate methods to move forwards and backwards, you can use the same one for both. I've got a working AVPlayer Movie Player, I'll show you how I did the slider part.

-(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;
}

And that's it, there are two gotchas in this code, first, the duration property returns a CMTime variable, you have to convert it to a float, also, this returns the raw number of seconds, you have to convert it to h:mm:ss if you want to display time labels. Second, the updateSlider method is triggered by a timer every second.
Good Luck.

飘落散花 2024-11-22 01:47:20

以下代码片段对我有用:

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
    });
}];

The following snippet of code worked for me:

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
    });
}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文