.h
SystemSoundID sound1;
.m
NSURL *soundURL1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"Woosh" ofType:#"mp3"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL1, & sound1);
AudioServicesPlaySystemSound(sound1);
This is the only code i use to play my Woosh.mp3 sound. I ran the app on my iPad and when i changed the volume on the device, the volume of the sound didn't change. I was wondering if there might be another way to implement sound into the app, or add the capability to change volume to this way.
NSURL *soundurl = [[NSBundle mainBundle] URLForResource: #"mysound" withExtension: #"caf"];
AVAudioPlayer *mySoundPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:soundurl error:&error];
mySoundPlayer .volume=1.0f; //between 0 and 1
[mySoundPlayer prepareToPlay];
mySoundPlayer.numberOfLoops=0; //or more if needed
[mySoundPlayer play];
Related
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];
I play a sound effect using:
-(void)playCorrectSound
{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Yes" ofType:#"mp3"];
NSURL *soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
if(! self.myAudioPlayer || ![soundURL isEqual:self.myAudioPlayer.url]){
self.myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
self.myAudioPlayer.delegate = self;
}
[self.myAudioPlayer setVolume:1.0];
[self.myAudioPlayer prepareToPlay];
[self.myAudioPlayer play];
}
I've added this code to three devices, all iPhone 5, all iOS 7.0.6 Two of them play the sound at significantly lower volume than another phone. All System preferences appear the same across all three devices. Would anyone know why one would be so much louder (and how I want it)?
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.
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
I am making a learn-the-alphabet app for toddlers and I have encountered a problem with the sound output. Every sound the app plays when testing on my ipad has the same volume whether the device's sound is set at maximum or mute. In other words, the same sound level comes out regardless of the device's volume setting.
The code I use to play sounds is (using the A-sound as an example):
- (IBAction)aSpill:(id)sender {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) #"aLyd", CFSTR("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
Any help is much appreciated! :)
Please see the answer here AudioServicesPlaySystemSound Volume?
It explains that system sounds don't always use the system volume.
I suggest using AVAudioPlayer to play your sound instead.
So from here : http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html
Try following the code using the below kind of code.
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource: #"sound"
ofType: #"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
[fileURL release];
self.player = newPlayer;
[newPlayer release];
[player prepareToPlay];
It is about halfway down the page.