Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Alfonso Lagrasta
Alfonso Lagrasta
7,551 Points

Adding dfferent sound to every prediction of the Crystal Ball app.

Hi there, I'm trying to add not only one single sound but as many different sounds as the number of predictions in the Crystal Ball app, more precisely I'm trying to assign a sound to every different prediction then recalling both the predction and that particular sound associated to it (like the sound of a voice reading that prediction) randomly in the ViewController. I'm still struggling with it though. Any ideas? Thank you.

Amit Bijlani Pasan Premaratne

1 Answer

Change each sound's name to a number, such as 0.mp3 or 6.mp3 and use arc4random() method to generate a random number in a given range, then use that number as a .mp3 string.

Alfonso Lagrasta
Alfonso Lagrasta
7,551 Points

Thank you Joseph. How can I link a sound to a specific string of prediction, like a sound with a voice reading that particular string of prediction? So when i call the predicitons randomly I'll call that string with excatly that sound.

You could use a dictionary to set multiple sounds to a prediction (string) as an array. In this case, the key would be the prediction string and the value would be an array of strings that contain the sound name.

Since it's an array, you can use the same random number principle to pick a random item from the array and use that (the string which contains the name of the mp3 file) to play a random sound.

Alfonso Lagrasta
Alfonso Lagrasta
7,551 Points

Thanks again. Can you write a sample code to that?

NSDictionary *predicts = @{"A prediction": @[@"play.mp3", @"hello.mp3", @"smooth.mp3"]};
[[predicts objectForKey:"A prediction"] objectAtIndex: random_number];