如何在给定时间暂停看电影?IOS

发布于 2024-12-12 08:16:12 字数 991 浏览 0 评论 0原文

我想在给定时间暂停一部电影,然后将一个动作继续多次。 我已经尝试过这样的 NSTimer:

#define Timer 0.05
#define Accuracy 0.025

pauseTimeArray = [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.00],[NSNumber numberWithFloat:10.00],[NSNumber numberWithFloat:15.00] , nil];


    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];

- (void)timeAction{

if (_moviePlayer.playbackState == MPMoviePlaybackStatePlaying) {

    NSNumber * positionNum = [NSNumber numberWithFloat:_moviePlayer.currentPlaybackTime];

    float positionFloat = [positionNum floatValue];

    for (NSNumber * pauseNum in _pauseTimeArray) {

        float pauseFloat = [pauseNum floatValue];

        float differentFloat = fabsf(pauseFloat - positionFloat);

        if (differentFloat < Accuracy) {
            [_moviePlayer pause];
        }
    }
}

}

当我暂停并再次播放时,它偶尔会暂停。和不同的Float <准确性。

有人可以在没有 NStimer 或其他东西的情况下提供另一个想法吗?谢谢!

I wanna pause a movie at a given time,and then an action to continue for many times.
i have try NSTimer like this:

#define Timer 0.05
#define Accuracy 0.025

pauseTimeArray = [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.00],[NSNumber numberWithFloat:10.00],[NSNumber numberWithFloat:15.00] , nil];


    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];

- (void)timeAction{

if (_moviePlayer.playbackState == MPMoviePlaybackStatePlaying) {

    NSNumber * positionNum = [NSNumber numberWithFloat:_moviePlayer.currentPlaybackTime];

    float positionFloat = [positionNum floatValue];

    for (NSNumber * pauseNum in _pauseTimeArray) {

        float pauseFloat = [pauseNum floatValue];

        float differentFloat = fabsf(pauseFloat - positionFloat);

        if (differentFloat < Accuracy) {
            [_moviePlayer pause];
        }
    }
}

}

And when i paused and play again , it occasionally paused. and the differentFloat < Accuracy.

could someone give another idear without NStimer,or something else.Thanks!

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

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

发布评论

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

评论(1

玩物 2024-12-19 08:16:12

您的方法很好,但需要一些改进才能正常工作。
不要在 pauseFloat +/-Accuracy 范围内检查 positionFloat,而是保留上次暂停时间作为检查点。然后,如果 positionFloat 大于此检查点 - 暂停影片并设置下一个检查点。

pauseTimeArray = [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.00],[NSNumber numberWithFloat:10.00],[NSNumber numberWithFloat:15.00] , nil];
uint currentCheckpoint = 0;

[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];

- (void)timeAction{

if (_moviePlayer.playbackState == MPMoviePlaybackStatePlaying) {

    NSNumber * positionNum = [NSNumber numberWithFloat:_moviePlayer.currentPlaybackTime];

    float positionFloat = [positionNum floatValue];

        float pauseFloat = [[pauseTimeArray objectAtIndex: currentCheckpoint] floatValue];

        if (positionFloat > pauseFloat) {
            [_moviePlayer pause];
            currentCheckpoint++;
        }
    }
}

当然,检查 currentCheckpoint 是否在您的pauseTimeArray范围内。

Your approach is good but it needs some improvements to work correctly.
Instead of checking positionFloat in range of pauseFloat +/-Accuracy keep your last pause time, as checkpoint. Then if positionFloat is greater than this checkpoint - pause movie and set next checkpoint.

pauseTimeArray = [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.00],[NSNumber numberWithFloat:10.00],[NSNumber numberWithFloat:15.00] , nil];
uint currentCheckpoint = 0;

[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];

- (void)timeAction{

if (_moviePlayer.playbackState == MPMoviePlaybackStatePlaying) {

    NSNumber * positionNum = [NSNumber numberWithFloat:_moviePlayer.currentPlaybackTime];

    float positionFloat = [positionNum floatValue];

        float pauseFloat = [[pauseTimeArray objectAtIndex: currentCheckpoint] floatValue];

        if (positionFloat > pauseFloat) {
            [_moviePlayer pause];
            currentCheckpoint++;
        }
    }
}

And of course, check if currentCheckpoint is inside your pauseTimeArray bounds.

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