开发者

I have a video saved in xcode: how can I play it?

开发者 https://www.devze.com 2023-03-08 05:15 出处:网络
I am already using the MediaPlayer framework but the media player can only be initialized through a URL. I have my video saved in my project, so how can I play it?开发者_StackOverflow社区URLs can be f

I am already using the MediaPlayer framework but the media player can only be initialized through a URL. I have my video saved in my project, so how can I play it?开发者_StackOverflow社区


URLs can be files paths too (file://):

NSURL *movieURL = [[NSBundle mainBundle] URLForResource:@"movie" withExtension:@"mp4"];


You can also play your video in UIWebview.

CGRect webFrame = CGRectMake(0.0, 0.0, 1, 1);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
webView.scalesPageToFit =YES;
webView.autoresizingMask =YES;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test123" ofType:@"mp4"]isDirectory:NO]]];
[self.view insertSubview:webView atIndex:0];


I am trying to play video inside a UIView, so my first step was to add a class for that view and start playing a movie in it using this code:

- (IBAction)movie:(id)sender{
    NSBundle *bundle = [NSBundle mainBundle];
        NSString *moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"];
    NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
    MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    theMovie.scalingMode = MPMovieScalingModeAspectFill;
    [theMovie play];
}
0

精彩评论

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