Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS play video with MPMoviePlayerController

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

like image 813
krackmoe Avatar asked Jul 10 '13 19:07

krackmoe


2 Answers

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.

like image 157
apollosoftware.org Avatar answered Nov 14 '22 15:11

apollosoftware.org


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];
like image 35
Mohit Avatar answered Nov 14 '22 13:11

Mohit