Adding a sound effect to a jump - ios

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);
}

Related

App Audio Not Playing

So in my file I have the following code with the intention that a music file plays in the background :
-(void) BackGroundMusic {
NSString *soundPath = [[NSBundle mainBundle]pathForResource:#"LostInTheSea" ofType:#"mp3"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound(soundID);
}
And then in the view did load section I have :
[self BackGroundMusic];
I have all the toolboxes added such as AVFoundation and AudioToolbox but i can not hear any audio playing . Please help !
You should not be using system sound to play mp3 files as user523234 explains.
Use AVAudioPlayer instead. Here's an example that will loop infinitely:
#import "AVFoundation/AVAudioPlayer.h"
#import "AVFoundation/AVAudioSession.h"
-(void)playbgMusic {
AVAudioPlayer *soundbgMusic;
NSString *soundPath;
soundPath= [[NSBundle mainBundle] pathForResource:#"bgMusic" ofType:#"mp3"];
soundbgMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
soundbgMusic.volume = 1.0;
soundbgMusic.numberOfLoops = -1;
[soundbgMusic prepareToPlay];
soundbgMusic.currentTime = 0.0;
[soundbgMusic play];
}
According to the document for AudioServicesPlaySystemSound:
Sound files that you play using this function must be:
No longer than 30 seconds in duration
In linear PCM or IMA4 (IMA/ADPCM) format
Packaged in a .caf, .aif, or .wav file

iOS - adding a sound on demand in iOS application

Ok, here is the situation. I am writing an iOS application, and I have a text scrolling from bottom to top. At a certain point, let's say when the word "EXAMPLE" shows up, I would like to my app automatically play particular sound, let's say "JINGLE".
How do I do it?
import <AudioToolbox/AudioToolbox.h>
-(void) playSound {
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"changeTrack" ofType:#"mp3"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
//[soundPath release];
}
you call in every condition you want

Looping Audio with AudioToolbox.framework

I have would like to play audio on the startup of my app, but, first of all, the audio doesn't play! No matter how i code it. And secondly I would love to loop it if i can.
#implementation ViewController
-(void)awakeFromNib
{
SystemSoundID soundID;
NSString *soundFile1 = [[NSBundle mainBundle] pathForResource:#"ScaryMusic" ofType:#"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile1], &soundID);
AudioServicesPlaySystemSound(soundID);
NSLog(#"Sound Played");
}
Try this instead:
#import <AVFoundation/AVFoundation.h>
AVAudioPlayer *player;
NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:#"ScaryMusic" withExtension:#"mp3"];
if (soundFileURL != nil)
{
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //infinite
}
[player play];
This is better for mp3's and such. SystemSound is mostly reserved for short .wav type sounds.
If you look in the docs you'll find that mp3 is not among the formats supported by AudioToolbox

asynchronous sounds in iphone app

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]);

system sound on iphone not playing

I want to play a system sound but I don't hear anything:
- (void) play_system_sound
{
NSLog(#"Play system sound");
SystemSoundID soundID;
NSString *path = [[NSBundle bundleWithIdentifier:#"com.apple.UIKit"] pathForResource:#"Tock" ofType:#"aiff"];
NSLog(#"path = %#", path );
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesDisposeSystemSoundID(soundID);
NSLog(#"Play system sound DONE");
}
I don't hear anything. What am I doing wrong?
After AudioServicesPlaySystemSound(soundID) ,don't call AudioServicesDisposeSystemSoundID(soundID) immediately.
Try this :
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle bundleWithIdentifier:#"com.apple.UIKit"] pathForResource:#"Tock" ofType:#"aiff"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
}
-(void)dealloc
{
AudioServicesDisposeSystemSoundID(soundID);
}
- (void)play_system_sound
{
AudioServicesPlaySystemSound(soundID);
}
I don't know if you have solved the problem, but for me, on iOS 7, sometimes the (__bridge CFURLRef) casting won't work, in which case, if you print out the result of (__bridge CFURLRef)[NSURL fileURLWithPath:path], it will be 0x0. This leads to failure of AudioServicesCreateSystemSoundID(). The return value of the function is type of OSStatus. If it fails, the function will return -50, which means one or more parameters are invalid. (If it executes successfully, it returns 0.)
I solved this by using the C style function getting the path of the file:
CFBundleRef mainBundle = CFBundleGetMainBundle ();
CFURLRef soundFileUrl;
soundFileUrl = CFBundleCopyResourceURL(mainBundle, CFSTR("fileName"), CFSTR("wav"), NULL);
AudioServicesCreateSystemSoundID(soundFileUrl, &soundID);

Resources