How to play video in ios6

若如初见. 提交于 2020-01-01 03:41:50

问题


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;


来源:https://stackoverflow.com/questions/13040862/how-to-play-video-in-ios6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!