开发者

How to play video in Objective-C - Causes Jerk when loads

I am developing an application which loads video while the application launches. I observe a huge jerk while the video plays and removes from screen.

My code is as follow:

VideoPlayer.m

-(id)initWithVideo: (CGRect)frame file:(NSString *)videoFile 
{
    if (self = [super initWithFrame:frame]) 
    {
        NSArray *file = [videoFile componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];

        NSString *moviePath = [[NSBundle mainBundle] pathForResource:[file objectAtIndex:0] ofType:[file objectAtIndex:1]];

        if (nil != moviePath) 
        {
            if (nil !开发者_如何学C= theMovie) 
            {
                [theMovie.view removeFromSuperview];
            }

            theMovie = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];

            theMovie.view.frame = self.bounds;
            theMovie.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
            theMovie.moviePlayer.controlStyle = MPMovieControlStyleNone;
            theMovie.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
            theMovie.moviePlayer.fullscreen = NO;           

            [self addSubview:theMovie.view];            
        }
    }

    return self;
}

Please guide me where I am missing it.


-(void)prepareMoviePlayer:(NSURL *)moviePath {
    self.moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:moviePath];
    if ([[self.moviePlayerViewController moviePlayer] respondsToSelector:@selector(loadState)]) {
      [[self.moviePlayerViewController moviePlayer] setControlStyle:MPMovieControlStyleEmbedded];
      [[self.moviePlayerViewController moviePlayer] setFullscreen:NO];      
      [[self.moviePlayerViewController moviePlayer] prepareToPlay];
      [[NSNotificationCenter defaultCenter] addObserver:self 
                           selector:@selector(moviePlayerLoadStateDidChange:) 
                             name:MPMoviePlayerLoadStateDidChangeNotification 
                             object:nil];
}


- (void)moviePlayerLoadStateDidChange:(NSNotification *)notification {
  if ([[self.moviePlayerViewController moviePlayer] loadState] == MPMovieLoadStateStalled) {
    NSLog(@"Movie Playback Stalled");
  } else if([[self.moviePlayerViewController moviePlayer] loadState] != MPMovieLoadStateUnknown) {

    [[NSNotificationCenter defaultCenter] removeObserver:self
                            name:MPMoviePlayerLoadStateDidChangeNotification
                            object:nil];

    [[[self.moviePlayerViewController moviePlayer] view] setFrame:self.view.bounds];
    [[self view] addSubview:[[self.moviePlayerViewController moviePlayer] view]];     
    [[self.moviePlayerViewController moviePlayer] play];
  }
}

The NSMoviePlayerViewController prepareToPlay method seems to be the key here. I also suggest adding other notifications to handle playback completion and playback stopping. Also some of this code is designed to work from a subview of the window.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜