How to play video in ios6

2019-03-21 16:16发布

问题:

I'm confused about:

MPMoviePlayerViewController and MPMoviePlayerController

what is the best way to play a video locally in ios6?

this is my code

NSURL * url = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource: @ "17" OfType: @ "mov"]];

     MoviePlayer = [[MPMoviePlayerViewController alloc]
                     initWithContentURL: url];
     [self presentMoviePlayerViewControllerAnimated: MoviePlayer];
     [moviePlayer.moviePlayer play];
     [[NSNotificationCenter defaultCenter] addObserver: self selector: @ selector (moviePlayerPlaybackStateChanged :) name: MPMoviePlayerPlaybackStateDidChangeNotification object: nil];
}


-(void)moviePlayerPlaybackStateChanged:(NSNotification *)notification {


}

回答1:

MPMoviePlayerViewController is for playing fullscreen video and is used mostly on the phone.

MPMoviePlayerController can be used for embedded video, ie not full screen on any of the iPads. You need to pull an empty view onto your scene in storyboard and give it the desired size. Then, in code, place the movieplayer in that subview. The first part of your code should be in viewDidLoad;

//movieplayer initialization
NSString *path = [[NSBundle mainBundle] pathForResource:@"videoName" ofType:@"m4v"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer setControlStyle:MPMovieControlStyleNone]; // for custom controls, for default controls you can leave this line out.

This part goes in viewWillAppear;

moviePlayer.repeatMode = MPMovieRepeatModeOne; // for looping
[moviePlayer.view setFrame: self.videoSuper.bounds]; 
[self.videoSuper addSubview: moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];

where videoSuper is the subview added in storyboard. Be sure to hook it up correctly;

//in .h
@property (weak, nonatomic) IBOutlet UIView *videoSuper;