iOS App: playing mp4 file from mainBundle crashes - ios

I am trying to play test.mp4 from inside my App, but it crashes with the following error:
2013-06-28 15:02:40.931 framing[9617:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
*** First throw call stack:
(0x1a5f012 0x176ce7e 0x1a5edeb 0x11839b1 0x118393b 0x28c2 0x1780705 0x6b42c0 0x6b4258 0x775021 0x77557f 0x7746e8 0x6e3cef 0x6e3f02 0x6c1d4a 0x6b3698 0x2ac1df9 0x2ac1ad0 0x19d4bf5 0x19d4962 0x1a05bb6 0x1a04f44 0x1a04e1b 0x2ac07e3 0x2ac0668 0x6b0ffc 0x223d 0x2165)
libc++abi.dylib: terminate called throwing an exception
(lldb)
The following are the codes:
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#implementation ViewController
#synthesize moviePlayer,movieScreen,viewVideoTitleBg;
-(IBAction)playTheMovie {
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp4"];
moviePlayer = [[MPMoviePlayerController
alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
//[self presentMoviePlayerViewControllerAnimated:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
moviePlayer.view.frame = CGRectMake(64,192,895,415);
[self.view addSubview:moviePlayer.view];
//[moviePlayer setFullscreen:NO animated:YES];
[moviePlayer play];
}
I have got the Frameworks installed too.
What is wrong with my code?

I had faced this issue recently. My work around was to delete the video file from the bundle and drag-drop it again.
Clean and build and then run it again.
But for instance: You should check the spelling and capitalisation of the name of the video file and the one used in your code.

Try using [NSURL URLWithString : path]; instead of [NSURL fileURLWithPath:path];

check if this
[[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp4"]
return nil.
if this return nil, you need to add the mp4 file to xcode project and SELECT >ADD TO TARGET< checkbox.

Related

How to access file using pathForResource:ofType:inDirectory

I have an mp3 file in my Supporting Files folder which I would like to play in my app on a loop. I am using the following code to attempt this:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"smoothjazz" ofType:#"mp3" inDirectory:#"Supporting Files"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //infinite
[player play];
However, this returns an error saying:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
How can I access my file in supporting files, and play it as intended? All help appreciated.
EDIT: SOLVED! See my answer for how I did it :)
If Supporting Files folder is the Supporting Files group you see in every Xcode project by default than the inFolder: argument is not needed as Supporting Files is a Xcode grouping and not a real folder. So you should do:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"smoothjazz"
ofType:#"mp3"];
This should work.
Hope it helps.
May be the path you getting through this will be nil,
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"smoothjazz" ofType:#"mp3" inDirectory:#"Supporting Files"];.
Make sure you properly added the files to your bundle.
And add the proper checks before committing operations, its the best practice. Like follows, Hope this helps you..
// Define NSFileManager and add the proper checks for file existance like as follows.
NSFileManager *filemgr = [NSFileManager defaultManager];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"smoothjazz" ofType:#"mp3" inDirectory:#"Supporting Files"];
if (filemgr fileExistsAtPath:imageURL.absoluteString])
{
// Your code to play the sound file
}
Dan Spag was right - I didn't need to use 'inDirectory:', as it was in the supporting files. But the main problem was that I declared the AVAudioPlayer in the viewDidLoad method, meaning that it's lifespan was only as long as the method (or something along those lines). So I declared the variable as a global variable, and the problem was solved! The music plays.

Error with a button performing multiple actions

I am trying to play audio and switch views with the same button..
- (IBAction)yourbuttonClicked:(UIButton *)sender
{
//start a background sound
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"tap2" ofType: #"caf"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
myAudioPlayer.numberOfLoops = -1; //infinite loop
[myAudioPlayer play];
//for Switch view
ViewController *nextView = [[ViewController alloc] initWithNibName:#"ViewController2"
bundle: Nil];
[self.navigationController pushViewController:nextView animated:YES];
}
2014-03-02 19:07:46.817 Balloon Tap[847:70b] * Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string
parameter'
When I launch my simulator and click on the play button that performs the action it crashes and in my log it comes up with this error above..
How do I go about fixing this and editing my code?
It appears soundFilePath is Nil
Try this
NSURL *url = [NSURL fileURLWithPath:stringPath];
This removes the alloc, which may cause you some issues here. Also I'm sure you've checked, but is there an issue with the filename? Does it actually exist in your bundle? I.e is it being compiled with the rest of your code? Make sure that you add that file to your project, and it appears in a "Copy Bundle Resources" build phase in your executable's target in Xcode.

How to activate a sound in an if statement (objective-c)

I am writing a code for a game and I am almost done. I am at the point where I am trying to input sounds. What I did was insert the audiotoolbox framework and:
.h
#interface GameViewController : UIViewController <ADBannerViewDelegate> {
SystemSoundID JumpSoundID;
}
-(void)jumpSound;
#end
and .m
#implementation GameViewController
-(void)jumpSound {
AudioServicesPlaySystemSound(JumpSoundID);
NSURL *jumpURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"jump2" ofType:#"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)jumpURL, &JumpSoundID);
}
-(void)fishSound {
AudioServicesPlaySystemSound(FishSoundID);
NSURL *fishURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"pickupFish" ofType:#"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)fishURL, &FishSoundID);
}
-(void)gameOverSound {
AudioServicesPlaySystemSound(GameOverSoundID);
NSURL *gameOverURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"gameover" ofType:#"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)gameOverURL, &GameOverSoundID);
}
Then I thought I could simply call on the methods in the if statement like so:
if (CGRectIntersectsRect(ball.frame, platform.frame) && (upMovement <= -1 )){
[self bounce];
[self iceblockfall];
[self jumpSound]; //this is what I thought I could call <<----------------
if (iceblock6Used == NO) {
addedScore = 1;
iceblock6Used = YES;
}
}
But it is not working. Whenever I run it I get this error:
2014-02-28 18:03:52.383 BounceIt[528:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
* First throw call stack..
What is the problem here. I am confused and can't find anything to help me. Am I making the wrong move by trying to call it as a method?
You need to change this:
AudioServicesPlaySystemSound(FishSoundID);
NSURL *fishURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"pickupFish" ofType:#"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)fishURL, &FishSoundID);
for this (move first line to the end)
NSURL *fishURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"pickupFish" ofType:#"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)fishURL, &FishSoundID);
AudioServicesPlaySystemSound(FishSoundID);
But I would recommend moving the first two lines to your initialization code, it doesn't make sense loading the wav file every time.

Creating a new class in iOS

Hi, I'm new to the iOS app development I have created an app with two ViewController for the the second controller have created on class in called video controll view. Everything is fine but when I click the play I'm getting warning like this I don't what is this can anyone please help me on that I'm stuck here for long time.
2013-12-25 10:10:12.890 politicalapp[346:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
This is the code I have put for the video player:
- (IBAction)play:(UIButton *)sender {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"d" ofType:#"mp4"]];
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc]initWithContentURL: url];
[self presentMoviePlayerViewControllerAnimated:player];
player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;[player.moviePlayer play];
player = Nil;
}
#end
Your problem is that pathForResource is returning nil. There can be a number of causes of this. Try changing it to:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"d.mp4" ofType:nil]];
If that doesn't solve it, just search the multiple of questions on stackoverflow named "Path for resource returns nil" and trouble shoot based on the various issues already discussed.

Unable to play MP4 video file from mainBundle

So I'm trying to play a simple intro animation video file that I've dragged into my project in XCode and therefore should be able to play from my mainBundle, right?
With this code:
NSURL *urlString = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"introvideo" ofType:#"mp4"]];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:urlString];
[player play];
I get this error message:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
Any help would be great!
This means your code can't find your introvideo.mp4 file. Make sure you have successfully add that file to your bundle. You can check in your project's setting: Copy Bundle Resource.
Your code isn't correct. Please change accordingly to the following. Still copy your video into your bundle resources.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"MacBook Pro with Retina Display" ofType:#"m4v"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[playercontroller release];
playercontroller = nil;
NSLog(#"Video is Playing");
Xcode 9
Here is an updated image.
Go to Build Phases > Copy Bundle Resources and see if your file is there. If it isn't you can add it by pressing the "+" button.
It seems that pathForResource:ofType: returns nil. Check that
this file is indeed added to "copy resources" build phase;
you didn't make mistake in the name of file - paths on device are case-sensitive.
You need To Check whether That Video Available in your Application's Resource Bundle.
As You mentioned
you getting this error message: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter.
It simply indicate that problem is in the resourcePath,means there is no such file exist there in Resource Bundle.that's Why that pathForResource returns nil path.
You need to put That Video File Again and Make Sure that file Exist in In Resource Bundle.
Then You should Go AHead with Code as Rehan Also posted.
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:#"video" ofType:#"mov" inDirectory:#""]];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[player view] setFrame:[self.view bounds]]; // Frame must match parent view
[self.view addSubview:[player view]];
[player play];
[player release];
I hope It may worth full to you.
Thanks
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:#"video" ofType:#"mov" inDirectory:#""]];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[player view] setFrame:[self.view bounds]]; // Frame must match parent view
[self.view addSubview:[player view]];
[player play];
[player release];
I had put an entire "Sounds" directory into the project, and added it to the "Copy bundle resources" section. It was working fine, but then it started crashing.
The fix was to prepend the directory name to the filenames. Eg, this didn't work:
SKAction.playSoundFileNamed("crash.caf", waitForCompletion: false)
... but this did work:
SKAction.playSoundFileNamed("Sounds/crash.caf", waitForCompletion: false)

Resources