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:
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:
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() .
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.
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.
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.
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.
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?
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: