How to edit a SystemSoundID object volume? - ios

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

Related

Volume on my device doesn't effect the volume

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

Audio wont play in objective c

I have the following code that used to work to play a sound in my project however after iv done some playing around over the past few weeks on other aspects of the project it dose not seem to play.
The audio file is in the main directory and works in preview.
Any ideas?
//playsound
AVAudioPlayer *showsound;
NSString *audiopath = [[NSBundle mainBundle] pathForResource:#"mouse1" ofType:#"wav"];
NSURL *audiourl = [NSURL fileURLWithPath:audiopath];
showsound = [[AVAudioPlayer alloc]initWithContentsOfURL:audiourl error:Nil];
[showsound play];
It's because after the line showsound play your method comes to an end and showsound (your AVAudioPlayer) is released. Hence there is nothing to do any playing any more. Solution: retain it! (For example, set an instance variable to hold on to it.)

Play audio after a Time Interval IOS7

Hi in my application, audio is playing when user press the button but now I have two audio buttons like audio1 and audio2, now I want to play second audio after a time interval once the audio1 is completed by clicking the same button.
- (IBAction)btn1:(id)sender {
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"audio" ofType:#"mp3" ];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[self.audioPlayer play];
}
The above code I have used for play one audio now I want to play second audio once the first audio completed please tell me how to make it.
Thanks.
Try this:
You have to add a bool variable with the name 'firstAudioPlayed' on start the variable should be NO or FALSE.
- (IBAction)btn1:(id)sender {
if (![self.audioPlayer isPlaying]) {
NSString *audioPath;
if (self.firstAudioPlayed) {
audioPath = [[NSBundle mainBundle] pathForResource:#"audio2" ofType:#"mp3" ];
} else {
audioPath = [[NSBundle mainBundle] pathForResource:#"audio1" ofType:#"mp3" ];
}
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[self.audioPlayer play];
self.firstAudioPlayed = !self.firstAudioPlayed;
}
}
I'm not sure if it's working like this, else just ask with a comment...
Explication of the code:
First you check if the player is already playing a song, if he isn't, you check if the first audio already has played (self.firstAudioPlayed) with this you can give the correct path to load the audio file. Now you play this audio file.
(Sorry for my grammar and my english, corrections are welcome)
You probably should use player's delegate. Set audioPlayer.delegate=self; and implement audioPlayerDidFinishPlaying:successfully: method to see when a player finished playing.
Then you can do whatever you want in there, for example start the second audio.

How to manage UIButton click sound iOS

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.

Playing audio when a button is pressed

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

Resources