Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrM

macrumors 6502
Original poster
Oct 13, 2008
259
1
California
Ok guys, I'm really new to the iPhone dev scene. Anyway, I was messing around with it and I cant really understand what I am missing. I'm teaching myself how to do it, so that's probably my first error.

Anyway, the app I'm making (for fun, not going to publish it) has 3 different buttons. I want each of those buttons to trigger a different sound. But here's my problem. Each button plays the same one. I am using IB to give each of the buttons the same action (which I've defined as "btnplaysoundClick")

In Xcode I have the following code set up-

PHP:
- (IBAction)btnplaysoundClick
{
	isound *sound = [[isound alloc] initWithFileName:@"file1.wav"];
	[sound play];
	
	isound *soundd = [[isound alloc] initWithFileName:@"file2.wav"];
	[soundd play];
	
	isound *sounddd = [[isound alloc] initWithFileName:@"file3.wav"];
	[sounddd play];
	
}

I changed the file names (sound, soundd, sounddd, and file1 2 and 3 for this post.

Anyway, does anyone see what my problem is? They all play the same sound. Any help is appreciated!
 

ayasin

macrumors 6502
Jun 26, 2008
318
0
Because you hooked all three buttons up to the same method. You need to create 3 methods (one with each sound) then hook each button up to it's respective method. Also you may consider releasing the isound you alloced so you don't leak memory.
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
Because you hooked all three buttons up to the same method. You need to create 3 methods (one with each sound) then hook each button up to it's respective method. Also you may consider releasing the isound you alloced so you don't leak memory.

He could also add (id)sender as a parameter of his action method, and then examine it to see what button was pressed/which sound to play.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
He could also add (id)sender as a parameter of his action method, and then examine it to see what button was pressed/which sound to play.
Like so:
Code:
- (IBAction)btnplaysoundClick:(id)sender
{ 
    if (sender == btn1) {
        isound *sound = [[isound alloc] initWithFileName:@"file1.wav"]; 
        [sound play];
    } else if (sender == btn2) { 
        isound *soundd = [[isound alloc] initWithFileName:@"file2.wav"]; 
        [soundd play]; 
    } else {
        isound *sounddd = [[isound alloc] initWithFileName:@"file3.wav"]; 
        [sounddd play];
    } 
     
}
 

ayasin

macrumors 6502
Jun 26, 2008
318
0
He could also add (id)sender as a parameter of his action method, and then examine it to see what button was pressed/which sound to play.

True, but based on his requirements that's more work (he'd need to create outlets for the buttons or examine some property at runtime) and less clean code wise for no real gain IMHO. Though, you are correct...it would have been more correct for me to say the easiest way to deal with this is to create 3 methods and hook each button up to a different one.
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
True, but based on his requirements that's more work (he'd need to create outlets for the buttons or examine some property at runtime) and less clean code wise for no real gain IMHO. Though, you are correct...it would have been more correct for me to say the easiest way to deal with this is to create 3 methods and hook each button up to a different one.

True...He can also use the (int)tag property of the buttons so that he doesn't have to create outlets for them. You can set their value in IB and examine it in the method to see what button was pressed.

Or he could just load all of the sounds into an array and then play the one corresponding to the tapped button's tag:

Code:
- (IBAction)btnplaysoundClick:(id)sender
{ 
    UIButton *button = (UIButton *)sender;
    isound *sound = (isound *)[soundArray objectAtIndex:button.tag];
    [sound play];

}

This code of course assumes that the sounds were preloaded into the array elsewhere, and that the button's tags are set from 0 to 2 corresponding to how their respective sounds were put into the array.
 

MrM

macrumors 6502
Original poster
Oct 13, 2008
259
1
California
Thanks for the speedy replies, guys! I will try it out and get back on here to let you know if I got it to work.
 

kalimba

macrumors regular
Jun 10, 2008
102
0
Once you get it working, change all of the .WAV files to be fart sounds and you'll have an instant App Store application!

/sarcasm
 

MrM

macrumors 6502
Original poster
Oct 13, 2008
259
1
California
Once you get it working, change all of the .WAV files to be fart sounds and you'll have an instant App Store application!

/sarcasm

Ha. This is purely because I want to learn and I have free time.

Anyhow, I tried giving them all different methods (I'll try the other way next) and I made some progress. The other two buttons now have their own actions set up in IB, and are saved, but they just don't play anything now(the first one still works). Here's the code I changed in Xcode.

Code:
- (IBAction)btnplaysoundClick
{
	isound *sound = [[isound alloc] initWithFileName:@"sound.wav"];
	[sound play];
}


- (IBAction)btnplaysoundClick2
{	
	isound *soundd = [[isound alloc] initWithFileName:@"soundd.wav"];
	[soundd play];
}


- (IBAction)btnplaysoundClick3
{
	isound *sounddd = [[isound alloc] initWithFileName:@"sounddd.wav"];
	[sounddd play];	
}

and then there is this
Code:
@interface iSirenViewController : UIViewController {

}[B]
- (IBAction)btnplaysoundClick; 
- (IBAction)btnplaysoundClick2; 
- (IBAction)btnplaysoundClick3;
[/B]
@end

I feel like I'm overlooking something very simple here. Again, thanks for the help you guys!
 

ayasin

macrumors 6502
Jun 26, 2008
318
0
Are you sure you hooked the other buttons up to the correct methods (and removed the links to the first method)? Also release the objects you allocated after the play method, otherwise you're going to leak memory.
 

MrM

macrumors 6502
Original poster
Oct 13, 2008
259
1
California
Are you sure you hooked the other buttons up to the correct methods (and removed the links to the first method)? Also release the objects you allocated after the play method, otherwise you're going to leak memory.

Yes, double checked and checked again. I must be missing something somewhere else. And thank you for the tip on releasing the allocated memory. I'll keep taking a stab at it and get back to you.
 

ayasin

macrumors 6502
Jun 26, 2008
318
0
Here's a project that I threw together that does basically what you are looking for. Rather than play a sound, it sets the background color of the view to red, green or blue depending on which button you push. You can look through it and hopefully see where you're going wrong.
 

Attachments

  • ButtonSample.zip
    18.7 KB · Views: 53

ayasin

macrumors 6502
Jun 26, 2008
318
0
It has the default copyright notices that XCode throws in there (I forgot to remove them)...ignore those and feel free to do whatever you like with that project.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.