Basically, I want a sound file to play when I click a button on my app.
There will be many different sounds, so the sound is selected with a picker.
So when the user selects one row, and presses a button, X sound plays, but when another row is selected, and the button pressed, Y sound plays.
- to set up the array, but this has been for text, and not files, so this is where I became unstuck.
Sorry for the inconvenience, and thankyou for your advice.
So you could store the name of your sound files in your array.
Then when the row is picked, use the index value of the picker to reference the index of the array. Then you will have the name of the sound file. You can use this to play the sound using whichever audio tools you are using. Most of these take the name of the sound to play.
The sounds can just be added to the resources of your project.
So, after that, I need these files to play when the buttonPressed method is called.
I have managed to string together some code from different resources, (Apple Documentation included) about playing a sound. However, it's far from right, and although I can get one sound to play, I can not get each one to play independently.
I have managed to string together some code from different resources, (Apple Documentation included) about playing a sound. However, it's far from right, and although I can get one sound to play, I can not get each one to play independently.
Sounds like you are very close. the issue might be that you need to stop any audio already playing as it may not have finished yet. So change your code so that you are just using once avaudioplayer instance to play all sounds and before you play a sound, call [theAudio stop]
Sorry for posting another message, but, if any at all could help. I think I'm on the way to solving what I am struggling with, but just need someone to set me straight if I'm wrong, and help guide me in the right direction.
Sorry for posting another message, but, if any at all could help. I think I'm on the way to solving what I am struggling with, but just need someone to set me straight if I'm wrong, and help guide me in the right direction.
Basically, I have set up the array, and set up the sound to play, but I need to somehow link these together so, instead of just playing one sound for every single row, I need it to play a different sound file for each row.
Basically, I have set up the array, and set up the sound to play, but I need to somehow link these together so, instead of just playing one sound for every single row, I need it to play a different sound file for each row.
So assuming each element of your array contains the name of the sound to play, and that you use this array or at least the values to populate the picker, you end up with a picker whose index numbers correspond.
So when the user selects a row in the picker you can quickly look up the corresponding index in the array and get the name of the sound to play and pass it into your play audio method.
Makes some sense? Why don't you paste your code and then I can be a bit more specific.
The picker displays different languages, for example, English, French etc.
The second array, then stores different phrases in that language, and displays it in a label. So for example, when english is selected, the phrase displayed in the label is in english, ans when french is selected, the label shows the phrase in french.
I have the sounds in the resources folder named 1.wav to 12.wav
The picker displays different languages, for example, English, French etc.
The second array, then stores different phrases in that language, and displays it in a label. So for example, when english is selected, the phrase displayed in the label is in english, ans when french is selected, the label shows the phrase in french.
I have the sounds in the resources folder named 1.wav to 12.wav
Thanks again for your help.
Ok so same approach as you use to display the correct text for the chosen language.
So firstly add the following to your .h file:
AVAudioPlayer* theAudio;
Then in the .m file in the pickerView didSelectRow delegate method add this below your text label code:
Code:
// Initialise Audio Player once only.
// Don't forget to release it when the view is finished with
if (theAudio) {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
}
NSString *wavToPlay = [soundColumn objectAtIndex:row]
NSString *path = [[NSBundle mainBundle] pathForResource:wavToPlay ofType:@"wav"];
[theAudio stop];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
and therefore, nothing happens when I click the button.
You really are a great help, thankyou.
What button? I assumed you wanted the sound to play immediately the picker changed…and that should work.
So what you need to do is some debugging and
1) Make sure the didSelectRow delegate method gets called
2) Make the path is being created correctly i.e. 1.wav
3) Make sure the wav file is in the xcode bundle
Get this working first and then if you prefer that the user presses the button to play the sound you need to use the same code to play the sound but you will need to make not of the currently select picker index each time it changes and pass this in.