iOS play video with MPMoviePlayerController
Asked Answered
G

4

7

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

Gyp answered 10/7, 2013 at 19:7 Comment(0)
O
13

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];
Osset answered 10/7, 2013 at 19:33 Comment(3)
Works! Big Thanks. I am quite new to iOS development. Whats the reason that my approach didnt work as expected?Gyp
If you want to use UIWebView you can use HTML5 to play videos if you are more comfortable with it. I'll attach code below.Osset
attached it as an answer if you want to play around with it.Osset
H
8

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];
Huck answered 1/4, 2014 at 12:28 Comment(0)
O
1

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.

Osset answered 10/7, 2013 at 19:41 Comment(1)
No doubt. I use HTML5 for playback most of the time. Something about it makes it feel cleaner. Plus with WebKit it allows more customization than iOS's Multimedia Frameworks.Osset
O
0

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];
Osset answered 10/7, 2013 at 19:11 Comment(1)
Should also be getting the path using NSBundleTechnical

© 2022 - 2024 — McMap. All rights reserved.