“MPMoviePlayerController”的“initialPlaybackTime”无法正常工作

时间:2014-08-07 16:02:46

标签: ios mpmovieplayercontroller mpmoviewcontroller

我正在尝试用MPMovieplayercontroller播放电影。一切都很好,除了“ initialPlaybackTime ”。玩家不是从给定的秒数开始玩,而是从头开始玩。

如何使“initialPlaybackTime”起作用?

-(void)viewDidLoad
{
    [super viewDidLoad];
    NSString *moviePath= [[NSBundle mainBundle] pathForResource:@"02_Skater" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:moviePath] ;
    // video player
    playerViewController = [[MPMoviePlayerController alloc] init];
    playerViewController.contentURL = url;
    playerViewController.view.frame = CGRectMake(0, 200, 300, 300);

    playerViewController.scalingMode = MPMovieScalingModeAspectFit;
    playerViewController.initialPlaybackTime = 3;
    playerViewController.endPlaybackTime = 4;
    playerViewController.controlStyle=MPMovieControlStyleNone;
    [playerViewController prepareToPlay];


    [playerViewController play];
    [[self view] addSubview: [playerViewController view]];

    // Do any additional setup after loading the view, typically from a nib.
}

2 个答案:

答案 0 :(得分:0)

如果您查看Apple文档,则可以阅读:

"对于视频点播内容,播放从距提供时间最近的片段边界开始。"

答案 1 :(得分:0)

iOS 8.4之后的新更新存在这些问题,并且这些错误在新的iOS 9.0中仍然存在

目前我找到的解决方法仍然不完整,因为您仍然无法设置视频的结束时间。一些解决方案如何能够设置" initialPlaybackTime"

首先将观察者添加到您的mediaPLayer

//Add Observer
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayerPlaybackStateChanged:)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:mediaPlayerController];

并将此方法写为通知

BOOL playbackDurationSet=NO;
-(void)moviePlayerPlaybackStateChanged:(NSNotification*)notification{
    MPMoviePlayerController* player = (MPMoviePlayerController*)notification.object;

    switch ( player.playbackState ) {
        case MPMoviePlaybackStatePlaying:

            if(!playbackDurationSet){
                [mediaPlayerController setCurrentPlaybackTime:player.initialPlaybackTime];
                playbackDurationSet=YES;
            }
            break;

         default:
            break;
    }
}

-(void)resetPlayerDurationVar{
    playbackDurationSet=NO;
}