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.
Related
I want to add a sound effect to a jump action when my character jumps. It sounds simple to do but i just cannot do it, this is what I've tried so far..
In my header file for my character I have:
SystemSoundID jump;
- (void)playSound:(NSString *)jump1 :(NSString *) mp3;
In my implementation file I have:
- (void)playSound :(NSString *)jump1 :(NSString *) mp3{
SystemSoundID audioEffect;
NSString *path = [[NSBundle mainBundle] pathForResource : jump1 ofType :mp3];
if ([[NSFileManager defaultManager] fileExistsAtPath : path]) {
NSURL *pathURL = [NSURL fileURLWithPath: path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
}
else {
NSLog(#"error , file not found aye: %#", path);
}
}
Also inside the function jump I have:
[self playSound:#"jump" :#"mp3"];
Please excuse my inexperience I just had a go at it as it would be a great feature for my game, if someone could tell me what I am doing wrong or a different and better way to do it that works that would be great thanks!
Add this method in a class and whenever you want to play the sound just call this method using [self playSound], jump.mp3 should be in your bundle, and remember one thing more don't forget to add AudioToolbox framework.
-(void) playSound {
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"jump" ofType:#"mp3"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
}
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 am an absolute beginner in both objective-c and other environments so please be patient. Here is my problem: I need to play sound files so that each of them stops when the other is started, right now they overlap; here is a snippet of my code.
Thank you in advance and let me know if you need other details.
- (IBAction)soundfirst {
NSString *path =
[[NSBundle mainBundle] pathForResource:#"hi2" ofType:#"mp3"];
player1 = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:NULL];
player1.delegate = self;
[player1 play];
}
- (IBAction)soundsecond {
[player1 stop];
NSString *path =
[[NSBundle mainBundle] pathForResource:#"hi2" ofType:#"mp3"];
player2 = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:NULL];
player2.delegate = self;
[player2 play];
}
The best way to do something when you're stuck, is to look into the documentation.
In your code, you're creating an object of type AVAudioPlayer, and naming it player1.
AVAudioPlayer Documentation:
http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html
look under the instance methods, and there's a method there called stop.
So in the first method, you can do something like
if(player2.playing){
[player2 stop];
}
[player1 play];
#import <AudioToolbox/AudioServices.h>
SystemSoundID buttonClickSoundID;
+ (void)playButtonClickSound {
if (buttonClickSoundID == 0) {
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"button_click" ofType:#"wav"];
CFURLRef soundURL = (CFURLRef)[NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID(soundURL, &buttonClickSoundID);
}
AudioServicesPlaySystemSound(buttonClickSoundID);
}
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.
I use the following code to play the sounds in my app, but the problem is that it slows down the app. How can I make the sounds happen asynchronously without slowing down the actions?
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle] pathForResource: _sound ofType:# "wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile], &soundID);
AudioServicesPlaySystemSound(soundID);
Do the AudioServicesCreateSystemSoundID during app initialization, or at some other point ahead of time when the user can expect/accept a little lag. It can be performed in the background, but the sound can't be played until it's finished.
AudioServicesPlaySystemSound is asynchronous already.
In other words, to demonstrate how do the init early, appDidFinishLaunching is the earliest opportunity. Make your sounds available to other parts of the app with a public property...
// AppDelegate.h, add this inside the #interface
#property (strong, nonatomic) NSArray *sounds;
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSMutableArray *tempSounds = [NSMutableArray array];
SystemSoundID soundID0;
// you need to initialize _sound0, _sound1, etc. as your resource names
NSString *soundFile0 = [[NSBundle mainBundle] pathForResource: _sound0 ofType:# "wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile0], &soundID0);
[tempSounds addObject:[NSNumber numberWithInt:soundID0]];
SystemSoundID soundID1;
NSString *soundFile1 = [[NSBundle mainBundle] pathForResource: _sound1 ofType:# "wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile1], &soundID1);
// SystemSoundID is an int type, so we wrap it in an NSNumber to keep in the array
[tempSounds addObject:[NSNumber numberWithInt:soundID1]];
self.sounds = [NSArray arrayWithArray:tempSounds];
// do anything else you do for app init here
return YES;
}
Then in SomeViewController.m ...
#import "AppDelegate.h"
// when you want to play a sound (the first one at index 0 in this e.g.)
NSArray *sounds = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).sounds;
AudioServicesPlaySystemSound([sounds[0] intValue]);