Воспроизведение видео пропадает через несколько секунд - PullRequest
3 голосов
/ 24 февраля 2012

Я хочу воспроизвести видео в формате UIView с помощью MPMoviePlayerController. После нажатия кнопки UIB видео появляется и начинает воспроизводиться. К сожалению, через 3-4 секунды видео становится черным, звук все еще воспроизводится. Есть идеи? Спасибо за ваше время.

(с использованием Xcode 4.2.1)

    -(void) playMovieButtonPressed:(UIButton*) button{

        NSString* video = @"testmovie.m4v";

        NSString *filepath   =   [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]];
        NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];  
        MPMoviePlayerController* player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 


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

        player.movieSourceType = MPMovieSourceTypeFile;
        [player.view setFrame:CGRectMake(20, 400, 300, 200)];
        [self.view addSubview:player.view];
        [player prepareToPlay];
        [player play];  
    }

- (void) moviePlaybackComplete:(NSNotification*) notification {
    NSLog(@"videoviewcontroller complete:  %@", notification);

    MPMoviePlayerController *mymoviePlayerController = [notification object];  
    [[NSNotificationCenter defaultCenter] removeObserver:self  
                                                    name:MPMoviePlayerPlaybackDidFinishNotification  
                                                  object:mymoviePlayerController];  
}

1 Ответ

7 голосов
/ 11 апреля 2012

Я разговаривал с инженером Apple.Решение состоит в том, что проигрыватель должен быть переменной экземпляра или свойством контроллера представления .Таким образом, он все еще доступен, когда вид был разрушен.

-(void) playMovieButtonPressed:(UIButton*) button{

        NSString* video = @"testmovie.m4v";

        NSString *filepath   =   [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]];
        NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];  
        self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 

// .....
}
...