How to play video locally in objective-c for iphone?
I开发者_JAVA百科 want to play the video on iphone locally by storing the video in the app. how can i do?
NSString *path = [[NSBundle mainBundle] pathForResource:@"myVideo" ofType:@"mp4"];
MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
myPlayer.shouldAutoplay = YES;
myPlayer.repeatMode = MPMovieRepeatModeOne;
myPlayer.fullscreen = YES;
myPlayer.movieSourceType = MPMovieSourceTypeFile;
myPlayer.scalingMode = MPMovieScalingModeAspectFit;
myPlayer.contentURL =[NSURL fileURLWithPath:path];
[self.view addSubview:myPlayer.view];
[myPlayer play];
To store video in the app, you can just add it with a copy files phase in the build. For an example of how to play a movie, check out the Media Player docs and especially the MoviePlayer sample code.
Try the following code:
-(IBAction)Videoplay_btn:(id)sender
{
NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
NSURL *streamURL = [NSURL fileURLWithPath:videoPath];
MPMoviePlayerController *moviplayer =[[MPMoviePlayerController alloc] initWithContentURL: streamURL];
[moviplayer prepareToPlay];
[moviplayer.view setFrame: self.view.bounds];
[self.view addSubview: moviplayer.view];
moviplayer.fullscreen = YES;
moviplayer.shouldAutoplay = YES;
moviplayer.repeatMode = MPMovieRepeatModeNone;
moviplayer.movieSourceType = MPMovieSourceTypeFile;
[moviplayer play];
}
FYI
The MPMoviePlayerController class is formally deprecated in iOS 9. (The MPMoviePlayerViewController class is also formally deprecated.) To play video content in iOS 9 and later, instead use the AVPictureInPictureController or AVPlayerViewController class from the AVKit framework, or the WKWebView class from WebKit.
精彩评论