Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-15-2009, 10:20 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 44
ioiiooo is on a distinguished road
Default Playing a Random Sound on Touch

On screen touch, I am trying to play one of four sound files randomly. I've created AVAudioPlayer instances for each of the four sounds and set them for prepareToPlay but am getting stuck on how to get a random one to play on touch.

My code for getting a single sound to play on touch is:

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {	
	[playerX play];
}
If anyone could provide assistance in the selection of a random player on touch, I would appreciate it!
ioiiooo is offline   Reply With Quote
Old 02-15-2009, 12:04 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by ioiiooo View Post
On screen touch, I am trying to play one of four sound files randomly. I've created AVAudioPlayer instances for each of the four sounds and set them for prepareToPlay but am getting stuck on how to get a random one to play on touch.

My code for getting a single sound to play on touch is:

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {	
	[playerX play];
}
If anyone could provide assistance in the selection of a random player on touch, I would appreciate it!
(1)put your AVAudioPlayer instances into an array
(2)pick a number between 0 and [soundArray count] using random()
(3)get that object from the array, and play it.

You'll probably need to look up NSMutableArray and random() .
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-15-2009, 08:35 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 44
ioiiooo is on a distinguished road
Default

All right, so after my AVAudioPlayer code, I now have the following:

Code:
		NSMutableArray *sounds;
		sounds = [NSMutableArray arrayWithCapacity: 5];
		[sounds addObject: player1];
		[sounds addObject: player2];
		[sounds addObject: player3];
I'm having a hard time getting an object from the array to play. Can someone help along the lines of :

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {	
	//code to play sound from array here?
}
ioiiooo is offline   Reply With Quote
Old 02-17-2009, 02:30 AM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Be sure to retain that array - you didn't init it, so it'll be autoreleased.

Code:
NSMutableArray *sounds;
sounds = [NSMutableArray arrayWithCapacity: 5];
[sounds retain];
[sounds addObject: player1];
[sounds addObject: player2];
[sounds addObject: player3];
Try this - I haven't compiled it, but it should work.

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {	
//code to play sound from array here?
AVAudioPlayer *player  = [sounds objectAtIndex: 0];       
[player play];
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-17-2009, 10:43 AM   #5 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 44
ioiiooo is on a distinguished road
Default

Thanks, Smasher! That got me going in the right direction and I have it working now.
ioiiooo is offline   Reply With Quote
Old 03-10-2009, 09:18 PM   #6 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 3
Hags007 is on a distinguished road
Default

ioiiooo, care to share a little more of what you did to get this to work? I have about 1 week's experience writing code for the iPhone and am trying to accomplish the same thing for a project for school. I have, thus far, been able to learn everything from this forum for what I need for my presentation and this seems to be the biggest obstacle for me right now.

Any help would be greatly appreciated and credit will be given to anyone willing to aid me in my project.
Hags007 is offline   Reply With Quote
Old 03-10-2009, 09:34 PM   #7 (permalink)
dot
Registered Member
 
Join Date: Feb 2009
Posts: 72
dot is on a distinguished road
Default

There's a lot of info in this thread. What do you have so far?
dot is offline   Reply With Quote
Old 03-10-2009, 10:39 PM   #8 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 3
Hags007 is on a distinguished road
Default

So far, I have been able to figure out how to assign a single sound to a single button (relatively simple using sample code and this forum) but I am having trouble understanding how to play random sounds from an array.

Ultimately, I want to be able to select a specific sound to play or play a random sound from the array, user selects which option. I see two actions being used for this, a playSelectedSound action and a playRandomSound action. Since I am new to this, I am trying to take it one step at a time and chip away at a single problem until I come up with a solution. Right now, I am working on the playRandomSound action problem.

Thank you, in advance, for any insight you can provide.
Hags007 is offline   Reply With Quote
Old 03-10-2009, 10:55 PM   #9 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by Hags007 View Post
So far, I have been able to figure out how to assign a single sound to a single button (relatively simple using sample code and this forum) but I am having trouble understanding how to play random sounds from an array.

Ultimately, I want to be able to select a specific sound to play or play a random sound from the array, user selects which option. I see two actions being used for this, a playSelectedSound action and a playRandomSound action. Since I am new to this, I am trying to take it one step at a time and chip away at a single problem until I come up with a solution. Right now, I am working on the playRandomSound action problem.

Thank you, in advance, for any insight you can provide.
Which part do you need help with? Creating an array, picking random numbers, or using the number to pull from the array? Post what you have so far.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-13-2009, 09:41 PM   #10 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 3
Hags007 is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
Which part do you need help with? Creating an array, picking random numbers, or using the number to pull from the array? Post what you have so far.
All of it, I guess. I have tried to implement the AVAudioPlayer with the NSMutableArray using generic sounds but I just can't get it. Like I said previously, I am about one week into writing code for Cocoa Touch/iPhone. Some of it seems to make sense and then I get a complete block on other parts and this is one of them. I don't understand how to assign sound files to a NSMutableArray, how to call them at random or at my discretion, etc.

Here is what I have that isn't working.

This clip I have in MySoundApp.h file:
Code:
@interface AVAudioPlayer : NSObject {
	NSMutableArray *sounds;
	sounds = [NSMutableArray arrayWithCapacity: 5];
	[sounds retain];
	[sounds addObject: player1];
	[sounds addObject: player2];
	[sounds addObject: player3];
}
This clip I have in the MySoundApp.m file:
Code:
- (void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event {
	AVAudioPlayer *player = [sounds objectAtIndex: 0];
	[player play];
}
I know I am missing something(s) that is/are big and I have been trying to figure it out on my own the last couple of days without much luck. I feel bad even asking for help but I am at a standstill. Being new to this, it's no wonder really. I feel pretty fortunate to be able to get this far. Like I said, I figured out how to play a single sound with the press of one button and was quite pleased to get that far. I am the type that likes to learn and figure things out on my own and if you, or anyone else, can point me in the direction of some documentation in the form of a book or tutorial or reference that illustrates clearly enough for a newbie to understand, I will gladly go there. On the other hand, if you are willing to give me some code hints/help, I will gladly take that as well.

Thanks again,

James

PS Perhaps I should clarify what it is I am trying to accomplish. I may be on the wrong path altogether. What my term paper is about is the economic and social influence the App Store has had on mobile gaming and life in general. I am using the success of the infamous iFart application as an example and my intent was to show how simple the application was, code wise, and illustrate how it has sold over 350k copies (best info I have) to date and generated a wave of fart apps as well as others that are just as ridiculous or simple. So, with that said, I want to ultimately mimic the iFart app itself and all it features, but my own artwork of course. This is not something I would sell or even try to give away and I probably won't use fart noises but rather animal sounds or music notes to illustrate the app. Am I on the right track or completely off base?

Last edited by Hags007; 03-13-2009 at 09:57 PM.
Hags007 is offline   Reply With Quote
Old 03-14-2009, 01:56 AM   #11 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

I think you should look at some more example code. Start with Apple's Hello world, and work your way up from there once you understand that.

For what you posted: in the .h file, you can only create variables and list the methods of your object. It's kind of a blueprint for what you build in your .m file. So the code to create, retain, and fill the array should go in the .m file, not the .h file.

So where in the .m file? You probably want to create the array once, so the code to create and fill the array should go in a method like viewDidLoad inside a view controller. Then you can wire up a button to an action, and play the sounds in that action.

Re: your thesis - I guess you're finding out that the coding is not so simple, if you're not familiar with iPhone programming. Many things are easy for an expert, but difficult to beginners. Actually, it brings to mind this story:

snopes.com: Know Where Man
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-14-2009, 03:47 AM   #12 (permalink)
dot
Registered Member
 
Join Date: Feb 2009
Posts: 72
dot is on a distinguished road
Default

Quote:
Originally Posted by Hags007 View Post
What my term paper is about is the economic and social influence the App Store has had on mobile gaming and life in general.
Sounds very interesting. I'd love to read it once you're done. iphonedot@gmail.com
dot is offline   Reply With Quote
Old 08-26-2009, 02:21 AM   #13 (permalink)
shiva
 
shiva.0537's Avatar
 
Join Date: Jun 2009
Location: Hyderabad
Age: 26
Posts: 54
shiva.0537 is on a distinguished road
Send a message via Skype™ to shiva.0537
Default

Quote:
Originally Posted by smasher View Post
Be sure to retain that array - you didn't init it, so it'll be autoreleased.

Code:
NSMutableArray *sounds;
sounds = [NSMutableArray arrayWithCapacity: 5];
[sounds retain];
[sounds addObject: player1];
[sounds addObject: player2];
[sounds addObject: player3];
Try this - I haven't compiled it, but it should work.

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {	
//code to play sound from array here?
AVAudioPlayer *player  = [sounds objectAtIndex: 0];       
[player play];
}
Hi,

I have around 20 sounds, do i need to create 20 audio player objects and need to create a sounds array?

It is too difficult..

Please could any one tell me right way to solve my problem?

thanks
shiva
shiva.0537 is offline   Reply With Quote
Reply

Bookmarks

Tags
avaudioplayer, random, sound

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 325
5 members and 320 guests
Anwerbl, guusleijsten, HowEver, LEARN2MAKE, mottdog
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,879
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:39 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0