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

标签 iphone ios video nstimer movie

我想在给定的时间暂停一部电影,然后一个 Action 继续多次。 我试过这样的 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];
        }
    }
}

当我暂停并再次播放时,它偶尔会暂停。和 differentFloat

有人可以在没有 NStimer 或其他东西的情况下给出另一个想法。谢谢!

最佳答案

您的方法很好,但需要一些改进才能正常工作。 不要检查 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 范围内。

关于iphone - 如何在给定时间暂停电影?IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7925094/

相关文章:

ios - 目标 Action 被调用两次

iPhone键盘返回键

ios - 相同类别的 SpriteKit/SKPhysics radialGravityField 总是相互吸引?

ios - 您可以将数组类型传递给用 Metal 编写的 CIFilter 内核吗?

ios - 如何使 IOS 应用程序使用其 Web-API 播放 Spotify 播放列表?

javascript - 在上传到远程网站期间缩小/调整视频大小

api - YouTube 数据 API 视频插入/更新 - 设置 'Game title'

python - Django 通过 iOS 上传图片时图像旋转不正确(EXIF 问题)

iphone - 如何在加载 View 后在运行时更新 UITableView

iOS 5及以上版本: enable landscape mode in MPMoviePlayerController