IOS [NSURL initFileURLWithPath:]: nil string parameter on loading video - iphone

I have a problem with my video content. I want to load a video from my viewcontroller and I get this error:
[NSURL initFileURLWithPath:]: nil string parameter
When I try to load the video from a url the video wont show up.
I am sure that my variable targetURN is not nil.
From here I load my viewcontroller:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
[self openVideoView:#"Video"];
break;
case 1:
[self openWebview:#"About"];
break;
case 2:
[self openWebview:#"http://www.url.com"];
break;
}
}
- (void)openVideoView:(NSString *)url{
IDPVideoView *vv = [[IDPVideoView alloc] initWithURN:url];
[self presentModalViewController:vv animated:NO];
}
In my viewcontroller:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *videoURL = nil;
if ([self.targetURN hasPrefix:#"http"])
{
videoURL = [NSURL URLWithString:targetURN];
}
else
{
videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:targetURN ofType:#"mp4"]];
}
if(videoURL != nil) {
MPMoviePlayerController *moviePlayer =
[[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[moviePlayer prepareToPlay];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
}

have you tried allocating the url first?
NSURL *vidURL = [[NSURL alloc] initWithString:self.targetURN];
as a general advice, try using self.targetURN all the time, or synchronize it to a different name like
#synchronize targetURN = _targetURN
and then use _targetURN in your code.this makes sure that you (in any case) use the right property.
(of course this goes for all your properties)

I solved the error part by adding this line
NSString *urlString = [NSString stringWithFormat:url];
- (void)openVideoView:(NSString *)url{
NSString *urlString = [NSString stringWithFormat:url];
IDPVideoView *vv = [[IDPVideoView alloc] initWithURN:url];
[self presentModalViewController:vv animated:NO];
}
But the video is still not showing up.

Related

MPMoviePlayerController not working on iOS8

I was trying out a project I worked on during iOS6. Tried running the same on iOS8 and the video is playing. Is there anything else I need to do?
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"Mcdonald" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayerController];
}
- (void)playMovie:(NSNotification *)notification {
MPMoviePlayerController *player = notification.object;
if (player.loadState & MPMovieLoadStatePlayable)
{
NSLog(#"Movie is Ready to Play");
[player play];
}
}
Thanks.
Finally. Adding it to the UIWindow solved the problem for me.
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];
Init your MPMoviePlayerController in .h file like below :
#property(strong,nonatomic)MPMoviePlayerController *moviePlayerController;
After above changes it will work .
Sample Code:
#interface ViewController : UIViewController
#property(strong,nonatomic)MPMoviePlayerController *moviePlayerController;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"Mcdonald" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
_moviePlayerController.allowsAirPlay = NO;
_moviePlayerController.shouldAutoplay = YES;
[_moviePlayerController.view setFrame:[UIScreen mainScreen].bounds];
_moviePlayerController.controlStyle=MPMovieControlStyleEmbedded;
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayerController];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)playMovie:(NSNotification *)notification {
MPMoviePlayerController *player = notification.object;
if (player.loadState & MPMovieLoadStatePlayable)
{
NSLog(#"Movie is Ready to Play");
[player play];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

repeatmode doesn't work in MPMoviePlayerViewController?

How to display a recorded video as repeat as an'n' number of times like in Vine Application.
Here I use the MPMoviePlayerViewController, and works great display the recorded video. But the problem is, it doesn't repeating.
Here the currently using code is,
NSURL *url = [NSURL fileURLWithPath:videoPath];
playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
[playerController.moviePlayer prepareToPlay];
playerController.view.frame = CGRectMake(200, 402, 300, 200);
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
playerController.moviePlayer.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview: playerController.view];
[playerController.moviePlayer play];
NSLog(#"repeatMode: %d",playerController.moviePlayer.repeatMode);
[playerController.view addSubview:customview];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerController moviePlayer]];
And the NSNotification code,
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
NSLog( #"myMovieFinishedCallback: %#", aNotification );
MPMoviePlayerController *movieController = aNotification.object;
NSLog( #"player.playbackState = %d", movieController.playbackState );
}
Can anyone please give the solution..
Note:
I'm using the XCode 4.5.2 tool and ios simulater 6.0
Try this code. It is working.
NSURL *fileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Video" ofType:#"mp4"]];
MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc]initWithContentURL:fileUrl];
[moviePlayerController.moviePlayer prepareToPlay];
[moviePlayerController.moviePlayer setRepeatMode:MPMovieRepeatModeOne];
[moviePlayerController.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
[self.view addSubview:moviePlayerController.view];
you can try this code
-(void) movieFinishedCallback:(NSNotification *) aNotification
{
if (aNotification.object == self.moviePlayer) {
NSInteger reason = [[aNotification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded)
{
[playerController play];
}
}
}
You have to keep the MPMoviePlayerController object as a member, otherwise the app would lose context to it and the movie will not loop.

i have videos list when i select row video will play on another view how to stop and come back for table view

if (indexPath.row==0)
{
NSString *urlAsString = [[NSBundle mainBundle] pathForResource: [self.array objectAtIndex:indexPath.row] ofType:#"mov"];
NSURL *url = [NSURL fileURLWithPath:urlAsString];
MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
myPlayer.shouldAutoplay = YES;
myPlayer.repeatMode = MPMovieRepeatModeOne;
myPlayer.fullscreen = YES;
myPlayer.movieSourceType = MPMovieSourceTypeFile;
myPlayer.scalingMode = MPMovieScalingModeAspectFit;
myPlayer.contentURL =[NSURL fileURLWithPath:urlAsString];
[self.view addSubview:myPlayer.view];
[myPlayer.view setFrame:CGRectMake(0, 0, 320, 400)];
[myPlayer setFullscreen:YES animated:YES];
[myPlayer play];
}
i have one button in MPMoviePlayerController called done when i click on done button i need to come back to my tableview plz help me any one
MPMoviePlayerController *myPlayer declare global and init . and
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doneButtonClick:)
name:MPMoviePlayerWillExitFullscreenNotification
object:nil];
And selector method:
-(void)doneButtonClick:(NSNotification*)aNotification{
[myPlayer stop];
}

MPMoviePlayerController is not playing video

I am using image matcher sdk with MPMoviePlayerController to play video after matched.
In NSLog video is process but not play.
Here .m file`
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize moviePlayer;
- (void)viewDidLoad
{
[super viewDidLoad];
_cvView = [[ImageMatcher alloc] initWithAppKey:#"apikey" useDefaultCamera:TRUE];
_cvView.matcherDelegate = self;
[_cvView start];
[_cvView setEnableMedianFilter:YES];
[_cvView setImagePoolMinimumRating:0];
[_cvView setMatchMode:matcher_mode_All];
bool value = [_cvView addImage:[UIImage imageNamed:#"pic2.jpeg"] withUniqeID:[NSNumber numberWithInt:20]];
}
-(void) viewDidAppear:(BOOL)animated
{
[self presentViewController:_cvView animated:YES completion:nil];
}
-(void) imageRecognitionResult:(int)uId
{
if (uId == 20) {
[self startPlayingVideo2:nil];
NSLog(#"ID = %d",uId);
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void) startPlayingVideo1:(id)paramSender{
NSLog(#"function startPlayingVideo make");
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *urlAsString = [mainBundle pathForResource:#"test1" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:urlAsString];
if(self.moviePlayer != nil){
NSLog(#"if 1 make");
[self stopPlayingVideo:nil];
}
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
if(self.moviePlayer != nil){
NSLog(#"if 2 make");
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(videoHasFinishedPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
moviePlayer.view.frame = CGRectMake(0, 0, 1024, 760);
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
[self.moviePlayer play];
}else{
NSLog(#"fail");
}
}
-(void) startPlayingVideo2:(id)paramSender{
NSLog(#"function startPlayingVideo make");
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *urlAsString = [mainBundle pathForResource:#"sample_mpeg4" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:urlAsString];
if(self.moviePlayer != nil){
NSLog(#"if 1 make");
[self stopPlayingVideo:nil];
}
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
if(self.moviePlayer != nil){
NSLog(#"if 2 make");
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(videoHasFinishedPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
moviePlayer.view.frame = CGRectMake(0, 0, 1024, 760);
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
[self.moviePlayer play];
}else{
NSLog(#"fail");
}
}
-(void) stopPlayingVideo:(id)paramSender{
if (self.moviePlayer != nil) {
NSLog(#"stopPlayingVideo on");
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
[self.moviePlayer stop];
if([self.moviePlayer.view.superview isEqual:self.view]){
[self.moviePlayer.view removeFromSuperview];
}
}
}
-(void) videoHasFinishedPlaying:(NSNotification *)paramNotification{
NSNumber *reason = [paramNotification.userInfo valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if (reason != nil) {
NSInteger reasonAsInteger = [reason integerValue];
switch (reasonAsInteger){
case MPMovieFinishReasonPlaybackEnded:{
NSLog(#"MPMovieFinishReasonPlaybackEnded");
break;
}
case MPMovieFinishReasonPlaybackError:{
NSLog(#"MPMovieFinishReasonPlaybackError");
break;
}
case MPMovieFinishReasonUserExited:{
NSLog(#"MPMovieFinishReasonUserExited");
break;
}
}
NSLog(#"finish reason = %ld", (long)reasonAsInteger);
[self stopPlayingVideo:nil];
}
}
#end
`
Add prepareToPlay in your code (Prepares a movie player for playback. (required))'
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
To prepare a new movie player for playback, call the prepareToPlay method, described in MPMediaPlayback Protocol Reference.

Play Video Url in iphone

How To play Server URL Video in Iphone ?
how to play video from url using MPMoviePlayer ?
(void)loadVideo {
videoUrl = [NSURL URLWithString:[NSString stringWithFormat:#"http://movies.apple.com/media/us/mac/getamac/2009/apple-mvp-biohazard_suit-us-20090419_480x272.mov"]];
if(videoUrl) {
if([videoUrl scheme]) {
[self playVideoStream:videoUrl];
}
}
}
-(void)playVideoStream:(NSURL *)movieFileURL {
MPMovieSourceType movieSourceType = MPMovieSourceTypeUnknown;
/* If we have a streaming url then specify the movie source type. */
if ([[movieFileURL pathExtension] compare:#"mov" options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
movieSourceType = MPMovieSourceTypeStreaming;
}
[self createAndPlayMovieForURL:movieFileURL sourceType:movieSourceType];
}
-(void)createAndPlayMovieForURL:(NSURL *)movieURL sourceType:(MPMovieSourceType)sourceType {
/* Play the video! */
[moviePlayer setMovieSourceType:sourceType];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];;
moviePlayer.view.frame = CGRectMake(10, 20, 280, 210);
[customVideoView addSubview:moviePlayer.view];
[moviePlayer play];
}
You need to set contentURL property at fisrt
moviePlayer.contentURL = urlVideo;
[moviePlayer prepareToPlay];
[moviePlayer play];
try this:
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"//name of your video here" ofType:#"//format e.g. mp4, m4v"]];
MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playerController.moviePlayer play];
// When movie finished loading you can have a notification and do extra code
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MovieDidLoad:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[playerController release];
playerController = nil;
}
-(void)MovieDidLoad:(NSNotification *)notification {
NSLog(#"logged and notification is %#", notification);
// extra code if needed
}
One of the easy way is to load that video url into a UIWebview.