04-01-2009, 09:33 AM
#1 (permalink )
Registered Member
Join Date: Mar 2009
Posts: 157
Stopping Sound and Controlling Volume
I have an app that simply plays a short .wav file on the click of a button and would like it to also stop the sound at the click of another button and implement a slider for control of the volume. Im using the AudioToolbox/AudioServices file and my code for playing the sound looks like this:
- (IBAction)push {
NSString *tmpFileName = [[NSString alloc] initWithFormat:@"mario_09"];
NSString *fileName = [[NSBundle mainBundle] pathForResource:tmpFileName ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:fileName], &soundID);
AudioServicesPlaySystemSound (soundID);
[tmpFileName release];
}
any help would be greatly appreciated. thanks much.
04-01-2009, 10:27 AM
#2 (permalink )
Registered Member
Join Date: Mar 2009
Posts: 101
i had the same issue and added this just above the code for the next button that was pressed that needed to cancel any sound currently playing:
[soundEffect release];
now i can click button one and mid sound press button two and have sound 1 canceled and sound 2 played.
04-01-2009, 10:42 AM
#3 (permalink )
Registered Member
Join Date: Mar 2009
Posts: 157
Thanks, what do i replace soundEffect with? my file name or SoundID or what?
04-01-2009, 03:18 PM
#4 (permalink )
Registered Member
Join Date: Mar 2009
Posts: 101
Quote:
Originally Posted by
starwarsdevwookie59
Thanks, what do i replace soundEffect with? my file name or SoundID or what?
I think for you it will be soundID
04-01-2009, 03:29 PM
#5 (permalink )
Registered Member
Join Date: Mar 2009
Posts: 157
- (IBAction)push {
NSString *tmpFileName = [[NSString alloc] initWithFormat:@"mario_09"];
NSString *fileName = [[NSBundle mainBundle] pathForResource:tmpFileName ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:fileName], &soundID);
AudioServicesPlaySystemSound (soundID);
[tmpFileName release];
}
- (IBAction)stop {
[soundID release];
(ERROR SOUNDID UNDECLARED)
NSString *tmpFileName = [[NSString alloc] initWithFormat:@"mario_08"];
NSString *fileName = [[NSBundle mainBundle] pathForResource:tmpFileName ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:fileName], &soundID);
AudioServicesPlaySystemSound (soundID);
[tmpFileName release];
}
I did this but i got the error listed above in all caps.
08-13-2010, 05:33 PM
#6 (permalink )
Registered Member
Join Date: Aug 2010
Posts: 3
try this
you need to declare soundID as an instance variable. when you declare it in the function its only available in that function. so create a instance variable at the top then you dont need to put SystemSoundID soundID; everywhere
SystemSoundID *soundID;
- (IBAction)push {
NSString *tmpFileName = [[NSString alloc] initWithFormat:@"mario_09"];
NSString *fileName = [[NSBundle mainBundle] pathForResource:tmpFileName ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:fileName], &soundID);
AudioServicesPlaySystemSound (soundID);
[tmpFileName release];
}
- (IBAction)stop {
[soundID release];
NSString *tmpFileName = [[NSString alloc] initWithFormat:@"mario_08"];
NSString *fileName = [[NSBundle mainBundle] pathForResource:tmpFileName ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:fileName], &soundID);
AudioServicesPlaySystemSound (soundID);
[tmpFileName release];
}
I did this but i got the error listed above in all caps.[/quote]
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl