iPhone: initWithContentURL not playing the video - iphone

I have a .mp4 video url in my server and trying to play by using the following code. But it doesn't do any actions when click on play button. I'm trying to test on iPhone 4.0 simulator or iPod 4.1 device. Is there anything wrong here (or) can't it work on iOS 4.0 or 4.1?
NSURL *url = [NSURL fileURLWithPath:encodedURL];
MPMoviePlayerController *videoPlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
[self.view addSubview:videoPlayer.view];
videoPlayer.view.frame = CGRectMake(0, 0, 320, 480);
[videoPlayer play];
Please advise!
Encode function:
NSString *newString = [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR("?#[]#!$ &'()*+,;=\"<>%{}|^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)) autorelease];

If the file is on a server than the line:
NSURL *url = [NSURL fileURLWithPath:encodedURL];
should be:
NSURL *url = [NSURL URLWithString:encodedURL];
You should also double check that the string encodedURL is correct by NSlog to your console.

Related

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 :(
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];
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];
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.
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];

play video using url ios 6

I am struggling to play a video which is a url of the video file on a server. I have a view in which I display url from the web service. When I click the url(contained in a table cell) I want that a new view should appear with movie player on it playing the video. I have tried MPMoviePlayerViewController and also MPMoviePlayerController and various combinations of the two but I could not play the video on simulator. Currently I don't have a device so please consider simulator as well as device while answering. Currently i am using:
NSURL *url = [NSURL fileURLWithPath:filePath];
self.player= [[ MPMoviePlayerViewController alloc] initWithContentURL:url];
//self.player.navigationController.navigationBar.hidden = YES;
[self.player.moviePlayer prepareToPlay];
//self.player.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
self.player.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.player.moviePlayer.fullscreen = NO;
[self presentModalViewController:self.player animated:NO];
[self.player.moviePlayer play];
filepath is a nsstring containing the video url.
Replace your this line : NSURL *url = [NSURL fileURLWithPath:filePath];
with this: NSURL *url=[NSURL URLWithString:filePath]; & then try.
My Code I am using MPMOVIEPLAYERVIEWCONTROLLER:
NSData *geturl = [[videoparsing objectAtIndex:btntag]objectForKey:#"iurl"];
myString = [[NSString alloc] initWithData:geturl encoding:NSASCIIStringEncoding];
NSLog(#"myString..%#",myString);
NSURL *fileURL=[NSURL URLWithString:myString];
NSLog(#"fileURL..%#",fileURL);
moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayerController];
[moviePlayerController.moviePlayer prepareToPlay];
moviePlayerController.moviePlayer.shouldAutoplay=YES;
[moviePlayerController.moviePlayer play];
If video file is on server then
NSURL *url=[NSURL URLWithString:filePath];
other should be change movieSourceType to MPMovieSourceTypeStreaming :
self.player.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
Also
self.player.moviePlayer.fullscreen = YES;
EDIT : Add :
self.player.moviePlayer.shouldAutoplay=YES;
remove :
[self.player.moviePlayer play];

NSInvalidArgumentException [NSURL length] url video record iphone

I save a video via iphone database application,the url where i'm recording the video is:
file://localhost/private/var/mobile/Applications/0DB3ABFC-1630-44FF-8506-4B08E3CB2C85/tmp/capture-T0x2591d0.tmp.00fEdr/capturedvideo.MOV
However, when I try to read the video via MediaPlayer it always crashes...
sample code:
NSString *frameName = [info objectForKey:#"UIImagePickerControllerMediaURL"];
NSURL *url = [NSURL URLWithString:frameName];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:videoPlayer.view];
videoPlayer.view.frame = CGRectMake(0, 0, 320, 419);
[videoPlayer play];
Thank you in advance.
Looks like the contents of [info objectForKey:#"UIImagePickerControllerMediaURL"] is not valid.
If it is valid, try fileURLWithPath rather then urlWithString.

MPMoviePlayerController "Loading Movie..."

I'm trying to play a video file from the resource folder on iPhone with iOS 4.1. The MPMoviePlayerViewController displays the view but it only says "Loading Movie..." forever with the activity indicator rotating. Anyone knows the cause of this? I've tried with various video formats that are supposed to work on iPhone, same result every time.
The movie player responds to actions like showing and hiding the controls.
My code:
-(void) playVideoNamed:(NSString *)videoName
{
// I get the path like this so the user can enter extension in the filename
NSString *path = [NSString stringWithFormat:#"%#/%#",
[[NSBundle mainBundle] resourcePath], videoName];
NSLog(#"Path of video file: %#", path);
RootViewController *rootController = [(DerpAppDelegate *)[[UIApplication sharedApplication] delegate] viewController];
NSURL *url = [NSURL URLWithString:path];
MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
vc.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[rootController presentMoviePlayerViewControllerAnimated:vc];
[vc.moviePlayer prepareToPlay]; // Not sure about this... I've copied this code from various others that claims this works
[vc.moviePlayer play];
}
Found out the issue:
NSURL *url = [NSURL URLWithString:path];
Must be replaced with:
NSURL *url = [NSURL fileURLWithPath:path];
Hard to spot that one...
I was performing the lines of code on a different thread, first dispatching to the main thread fixed it:
dispatch_async(dispatch_get_main_queue(), ^(void){
MPMoviePlayerViewController* theMovie = [[MPMoviePlayerViewController alloc] initWithContentURL: videoURL];
[self presentMoviePlayerViewControllerAnimated:theMovie];
});

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

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?
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];
}