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.
Related
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.
I've setup an AVQueuePlayer named player in my viewDidLoad method. I also have a UIbutton method which plays that sequence.
here is my view did load method
AVPlayerItem *firstSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"first" ofType:#"mp3"]]];
AVPlayerItem *secondSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"second" ofType:#"mp3"]]];
AVPlayerItem *thirdSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"third" ofType:#"mp3"]]];
player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstSound, secondSound, thirdSound, nil]];
and here is my button's method
-(IBAction)sequencePlay:(id)sender {[player play];}
pressing the button only produces the sequence of sounds once, when the view is first loaded. when i click the button again nothing happens. however if i navigate to another view controller and back to this view controller, it will work the first time i press the button again but as before, it only works for the first click.
update:
doing the setup in the button instead of the viewDidLoad method was the key as according to the first answer to solve my problem.
My mp3 files are small, each file is just one word being spoken. I have a UIPickerView setup with 3 columns, and depending on which rows are selected, it will play those specific words in sequence when the button is pressed. Any good advice on how i may accomplish that?
i'm currently trying to do this but get an error
-(IBAction)sequencePlay:(id)sender
{
NSString *sayFirst = _firstRowWord.description;
AVPlayerItem *firstSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:sayFirst.description ofType:#"mp3"]]];
i get the following error
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
the description of the firstRowWord is the same exact name as the mp3 file i want to play
You could try using the sequencePlay: method to setup the AVQueuePlayer there.
So:
-(IBAction)sequencePlay:(id)sender {
AVPlayerItem *firstSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:#"first" ofType:#"mp3"]]];
AVPlayerItem *secondSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"second" ofType:#"mp3"]]];
AVPlayerItem *thirdSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"third" ofType:#"mp3"]]];
player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstSound, secondSound, thirdSound, nil]];
[player play];
}
If you are concerned about the performance of this you could use the actionAtItemEnd property to repopulate the Player.
Hope this helped!
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.
This has driven me nuts for a long time now. I am using Xcode 4.6 but this has been happening for several versions. When I turn on breakpoints and add the Exception Breakpoint, it always pauses on a line of code where I'm setting up an audio player. I set some up before and after the same way, but it only pauses on that one.
Here is the code I'm using:
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
self.playedSongs = [NSMutableSet setWithCapacity:9];
[self loadSettingsFromFile];
NSURL *sfxPath = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"Snap.aiff" ofType:nil]];
self.snapSfx = [[[AVAudioPlayer alloc]initWithContentsOfURL:sfxPath error:nil]autorelease];
self.snapSfx.volume = 1.f;
NSURL *fireworksPath;
if ([OriginalIPadChecker isNotiPadOriginal]) {
fireworksPath = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"FireworksSFX.mp3" ofType:nil]];
}
else{
fireworksPath = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"NoFireworksSFX.mp3" ofType:nil]];
}
self.fireWorksSfx = [[[AVAudioPlayer alloc]initWithContentsOfURL:fireworksPath error:nil]autorelease];
self.fireWorksSfx.volume = 1.f;
NSURL *poofPath = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"Remove Poof.mp3" ofType:nil]];
//The line below is the one that it pauses on
self.removePoof = [[[AVAudioPlayer alloc]initWithContentsOfURL:poofPath error:nil]autorelease];
self.removePoof.volume = 1.f;
NSURL *newHighScorePath = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"New High Score.mp3" ofType:nil]];
self.theNewHighScore = [[[AVAudioPlayer alloc]initWithContentsOfURL:newHighScorePath error:nil]autorelease];
self.theNewHighScore.volume = 1.f;
NSURL *badgeInTrophyPath = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"Badge In Trophy.aiff" ofType:nil]];
self.badgeInTrophy = [[[AVAudioPlayer alloc]initWithContentsOfURL:badgeInTrophyPath error:nil]autorelease];
self.badgeInTrophy.volume = 1.f;
NSURL *dingPath = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"Ding.aif" ofType:nil]];
self.ding = [[[AVAudioPlayer alloc]initWithContentsOfURL:dingPath error:nil]autorelease];
self.ding.volume = 1.f;
The app doesn't crash and the sound plays fine, but the debugger always pauses on that one lineāeven if I move it somewhere else, it still pauses. I can continue the execution and it works just fine, but this really bugs me.
I don't get why it pauses. Any ideas?
As explained in the comment of Xcode stops on prepareToPlay, the reason for the breakpoint is that you were setting a generic breakpoint of type 'exception on All' which responds to a signal which is fired by the c++ library. It is better to use an "Objective-c" exception breakpoint, which will protect for such cases.
Try this and update the question on where it stops (if it stops):
AVAudioPlayer *foo;
NSError *error = nil;
assert(poofPath);
foo = [AVAudioPlayer alloc];
foo = [foo initWithContentsOfURL:poofPath error:&error];
assert(foo);
[self setRemovePoof:foo];
[foo release];
If the assert kicks in, that's probably an Apple issue. Unfortunately there are cases of Apple internal frameworks using try/catch, and they will trigger the breakpoint. Its not common but does happen.
If in the end that foo is not nil and the error is also nil, the best you can do is enter a bug report at bugreporter.apple.com (which would be great). Also, did you look at poofPath - is there any chance that the URL returns anything other than pristine audio?
My guess is that Apple tries to open and process the file, its gets an internal exception because there is something "odd" or abnormal about the file. Then the framework does additional work, and manages to read the file. So you get your sound but the exception too. Take some common type of mp3 and put that in your app bundle, and try to open it. See if it (and maybe a few other files) gives the same error. Or create a demo project with the sound and upload it (in the end you may need to submit the demo project to Apple in a bug report). These kinds of things should be bug reported.
EDIT: Unfortunately there are cases of Apple internal frameworks using try/catch, and they will trigger the breakpoint. Then, as mentioned earlier, some later code finally figures out how to decode the file and does.
The original poster has responded in a comment that this particular file was created with Audacity, and has heard that such files often have decode issues.
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)