I've been using the following function to play sounds.
- (void) playFile:(NSString *)nameOfFile
{
NSString *tmpFileName = [[NSString alloc] initWithFormat:nameOfFile];
NSString *fileName = [[NSBundle mainBundle] pathForResource:tmpFileName
ofType:#"mp3"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)
[NSURL fileURLWithPath:fileName], &soundID);
AudioServicesPlaySystemSound (soundID);
}
I've noticed that if the button that calls this is pressed again, the sound just plays over the top of the existing playing sound. How can I augment this code to stop sounds playing again and instead 'restart' the playing file? Should I play the sounds using a different API?
You can use AVAudioPlayer to do that, just allocate it in your init/viewdidload then when the button is pressed do something like:
[self.player stop]
self.player.currentTime=0;
[self.player play];
Related
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
Im using SystemSoundID to create a sound when a button was pressed, this way:
this is the object declaration:
SystemSoundID soundEffect;
this is in the viewDidLoad:
NSString *soundPath = [[NSBundle mainBundle]pathForResource:#"create_button_sound" ofType:#"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID(CFBridgingRetain(soundURL), &soundEffect);
And finally when the button was pressed:
AudioServicesPlaySystemSound(soundEffect);
What is the best way to control the sound of the effect? I want to make sure that if someone have his iphone volume level set to max so the sound wont be crazy loud..
thanks##!
According to this SO question (AudioServicesPlaySystemSound Volume? -- slightly out of date since Sounds is no longer under General), you're going to get stuck because of a global setting that can override volumes for system sounds.
The workaround is to use AVAudioPlayer instead, if that's a possibility.
Example:
NSString *soundPath = [[NSBundle mainBundle]pathForResource:#"create_button_sound" ofType:#"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *mySoundPlayer =[[AVAudioPlayer alloc] soundURL error:&error];
mySoundPlayer .volume=0.8f; //between 0 and 1
[mySoundPlayer prepareToPlay];
mySoundPlayer.numberOfLoops=0; //or more if needed
[mySoundPlayer play];
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
I am using the following code to get a sound on button click.And it works perfect. The problem is this sound is not getting reduced if i reduce the volume on the phone. I want to manage button click sound as per the volume button. I am not using input view.
SystemSoundID soundID;
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"sound1" ofType:#"mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID ((CFURLRef) CFBridgingRetain(soundUrl) , &soundID);
AudioServicesPlaySystemSound(soundID);
As Duncan C suggests, try AVAudioPlayer:
Import AVFoundation.framework.
Replace your code snippet with:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"sound1" ofType:#"mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[player play];
What I do is to create an AVAudioPlayer for my button sound and play that. AVAudioPlayer honors the system volume.
I've created a separate IBAction method that plays the sound, and then I link my buttons to both actions. That way it's easy to turn the sound on and off simply by linking the action. If you always want sounds to play then you might want to put a call to a sound play method directly in your IBAction methods.
Check Click here that might help you.
In my app I use AVAudioPlayer to play some mp3. My code is:
- (void) play{
NSString *path = [NSString stringWithFormat:#"%#/%#",
[[NSBundle mainBundle] resourcePath],
[arraysound objectAtIndex:appDelegate.indexSound]];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
soundID = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
soundID.delegate = self;
[soundID prepareToPlay];
[soundID play];
}
but I have 40 sounds to play; I use everytime soundID to play my sounds
example I have sound1.mp3, sound2.mp3, sound3.mp3......
How can I release my soundId?
When my sound change I call "play" method but this method alloc a new AVAudioPlayer everytime
I Know
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
successfully: (BOOL) flag {
but I don't understand how to release my sound id inside this delegate method, because if I release inside soundId my app crash
after some minutes that I use my app I have with crash this message "shm_open failed: "Apple Audio Queue"
please help me...thanks
You don't want to release soundID in your delegate method. You want to release player. Judging from your code, soundID is an ivar. When the delegate gets called, soundID may have been set to another value. But the player parameter will be the AVAudioPlayer that has just finished playing.