如何在iphone中连续播放视频?

时间:2013-04-08 12:30:40

标签: iphone objective-c ipad cocoa-touch

我正在使用MPMoviePlayerController,但我得到的问题是它只重复两次,但我必须不断重复它如何做到这一点请任何人建议我更好的方式。

-(void)viewDidLoad
{
    player = [[MPMoviePlayerController alloc] initWithContentURL:outPutUrl];

    player.view.frame = CGRectMake(0, 0, 320, 480);
    player.repeatMode = MPMovieRepeatModeOne;

    [player setShouldAutoplay:YES];
    [player prepareToPlay];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayerDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:player];
    [player setControlStyle:MPMovieControlStyleNone];

    [self.imgView addSubview:player.view];

    [player play];
}
-(void)moviePlayerDidFinish:(NSNotification *)note
{
    if (note.object == player)
    {
        NSLog(@"this is note%@",note.object);

        NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];

        if (reason == MPMovieFinishReasonPlaybackEnded)
        {
            [player play];
        }
    }
}

1 个答案:

答案 0 :(得分:1)

- (void)viewDidLoad
{
    [super viewDidLoad];
    player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"drvo_hd_final-1" ofType:@"mp4"]]];
    player.view.frame = CGRectMake(20, 20, 280, 200);
    player.repeatMode = MPMovieRepeatModeOne;
    [player setShouldAutoplay:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerDidFinish:)  name:MPMoviePlayerPlaybackDidFinishNotification object:player];
    [player setControlStyle:MPMovieControlModeDefault];
    [self.view addSubview:player.view];
    [player prepareToPlay];
}

- (void)moviePlayerDidFinish:(NSNotification *)note
{
    if (note.object == player)
    {
        NSLog(@"this is note%@",note.object);
        NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
        if (reason == MPMovieFinishReasonPlaybackEnded)
        {
            [player prepareToPlay];
        }
    }
}

这对我有用并且不断播放.....