iOS play video with MPMoviePlayerController

2019-03-27 12:53发布

I got this piece of code:

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:@"/Resources/disc.mp4"]];
    theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
    theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
    UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
    [theMoviPlayer.view setFrame:backgroundWindow.frame];
    [backgroundWindow addSubview:theMoviPlayer.view];
    [theMoviPlayer play];

But i really dont know how to add the video to my project. In which folder do i have to put the video file!? Or do i have to do something else to add it to my project?

EDIT:

It looks like this in xcode, is it correct? Because i do get a playback error right now. Previously i used an url to play this video and this worked quite well, but with this file locally not :(

enter image description here

4条回答
相关推荐>>
2楼-- · 2019-03-27 13:14

Ok your bundle path looks jacked, below should work.

NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"disc" ofType:@"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[theMoviPlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:theMoviPlayer.view];
[theMoviPlayer play];
查看更多
一夜七次
3楼-- · 2019-03-27 13:20

Add MediaPlayer Framework

import it to your file

#import <MediaPlayer/MediaPlayer.h>

Create an object of MPMoviePlayerController

MPMoviePlayerController * moviePlayer;

write this code where you wants to play video

NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"spacetest.mp4" ofType:nil];
NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:moviePlayer.view];
moviePlayer.fullscreen = YES;
[moviePlayer play];
查看更多
Emotional °昔
4楼-- · 2019-03-27 13:32

Using HTML5 as I promised above:

    NSString *videoTitle = @"disc.mp4";
    NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSString *playPath = [NSString stringWithFormat:@"<center><video  width=\"640\" height=\"480\" controls><source src=\"%@\" media=\"all and (max-width:1024px)\"></video></center>",videoTitle];


    [webView loadHTMLString:playPath baseURL:baseURL];

This will play in 640x480, but if you are familiar with HTML5 video tags, you can customize pretty heavily.

查看更多
三岁会撩人
5楼-- · 2019-03-27 13:33

Since you are using MPMoviePlayerController and not UIWebView you can place your mp4 or file in your Resources and XCode/iOS will find it. Be sure that the directory/group that the file is under is yellow not blue. You don't want it to be a relative path.

Just drag the resource into your project. Copy items into destination is selected, First option of Folders is selected, and most importantly, add to target!

Ok try the code below:

NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"disc" ofType:@"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[theMoviPlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:theMoviPlayer.view];
[theMoviPlayer play];
查看更多
登录 后发表回答