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 08-15-2010, 07:41 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default Sounds with Pickers

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.

Any help would be greatly appreciated!

Thanks.
benblanchard is offline   Reply With Quote
Old 08-15-2010, 08:47 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default

Anyone able to offer me some help?

Cheers again.
benblanchard is offline   Reply With Quote
Old 08-16-2010, 01:50 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default

Bump.

Anyone have any knowledge of how to solve my problem?!
benblanchard is offline   Reply With Quote
Old 08-16-2010, 02:07 AM   #4 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by benblanchard View Post
Bump.

Anyone have any knowledge of how to solve my problem?!
Well you just do what you've said. Set up a picker with your choices. When the picker delegate method gets called, play the corresponding sound.

Suggest you look at apple examples and start coding. No one here is going to write the code for you.
wuf810 is offline   Reply With Quote
Old 08-16-2010, 02:15 AM   #5 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default

I am not suggesting that anyone should write the code for me at all.

The part I am struggling on is setting up the picker with the different files.
I have always used -
Code:
 NSArray *array = [[NSArray alloc] initWithObjects:
- 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.
benblanchard is offline   Reply With Quote
Old 08-16-2010, 02:25 AM   #6 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by benblanchard View Post
I am not suggesting that anyone should write the code for me at all.

The part I am struggling on is setting up the picker with the different files.
I have always used -
Code:
 NSArray *array = [[NSArray alloc] initWithObjects:
- 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.
wuf810 is offline   Reply With Quote
Old 08-16-2010, 02:33 AM   #7 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default

Thankyou very much.

I think I have stored the file names in it

Code:
soundColumn = [[NSArray alloc] initWithObjects:@"1.wav",
								@"2.wav",
								@"3.wav",
								@"4.wav",
								@"5.wav",
								@"6.wav",
								@"7.wav",
								@"8.wav",
								@"9.wav",
								@"10.wav",
								@"11.wav",
								@"12.wav",
								nil];

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.

Code:
-(IBAction)buttonPressed
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"%d" ofType:@"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}

Thanks.
benblanchard is offline   Reply With Quote
Old 08-16-2010, 02:43 AM   #8 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by benblanchard View Post

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.

Code:
-(IBAction)buttonPressed
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"%d" ofType:@"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}

Thanks.
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]
wuf810 is offline   Reply With Quote
Old 08-16-2010, 02:47 AM   #9 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default

Cheers.

I'll have a go and mess around with the code a bit.

I appreciate your help
benblanchard is offline   Reply With Quote
Old 08-16-2010, 12:14 PM   #10 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default

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.

Thanks
benblanchard is offline   Reply With Quote
Old 08-16-2010, 12:34 PM   #11 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by benblanchard View Post
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.

Thanks
What's your question?
wuf810 is offline   Reply With Quote
Old 08-16-2010, 12:36 PM   #12 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default

Quote:
Originally Posted by wuf810 View Post
What's your question?
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.
benblanchard is offline   Reply With Quote
Old 08-16-2010, 12:42 PM   #13 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by benblanchard View Post
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.
wuf810 is offline   Reply With Quote
Old 08-16-2010, 12:59 PM   #14 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default

.h

Code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

@interface TWSSViewController : UIViewController <AVAudioPlayerDelegate> {
	
	IBOutlet UIButton *sayItButton;
	IBOutlet UIPickerView *picker;
	IBOutlet UILabel *translatedTextLabel;
	NSArray *languagesColumn;
	NSArray *translatedTextColumn;
	NSArray *soundColumn;



}

@property (nonatomic, retain) IBOutlet UIButton *sayItButton;
@property (nonatomic, retain) IBOutlet UIPickerView *picker;
@property (nonatomic, retain) IBOutlet UILabel *translatedTextLabel;
@property (nonatomic, retain) NSArray *languagesColumn;
@property (nonatomic, retain) NSArray *translatedTextColumn;
@property (nonatomic, retain) NSArray *soundColumn;


-(IBAction)buttonPressed;

@end
.m

Code:
#import "TWSSViewController.h"
#import <AVFoundation/AVFoundation.h>

@implementation TWSSViewController

@synthesize picker;
@synthesize translatedTextLabel;
@synthesize translatedTextColumn;
@synthesize languagesColumn;
@synthesize sayItButton;
@synthesize soundColumn;







- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSArray *array = [[NSArray alloc] initWithObjects:
					  @"Dutch",
					  @"English",	
					  @"French",
					  @"German",
					  @"Greek",
					  @"Italian",
					  @"Japanese",
					  @"Polish",
					  @"Portuguese",
					  @"Russian",
					  @"Spanish",
					  @"Welsh",
					  nil];
	
	
	translatedTextColumn = [[NSArray alloc] initWithObjects:
							@"Dat is wat ze zei",
							@"That's what she said",
							@"C'est ce qu'elle a dit",
							@"Das ist, was sie sagte",
							@"Αυτός είναι αυτό που είπε",
							@"Questo č quello che ha detto",
							@"それは彼女が言ったのです",
							@"To, co powiedziała",
							@"Isso é o que disse",
							@"Вот что она сказала",
							@"Eso es lo que ella dijo",
							@"Dyna beth ddywedodd hi",										
							nil];
	
	soundColumn = [[NSArray alloc] initWithObjects:@"1",
									@"2",
									@"3",
									@"4",
									@"5",
									@"6",
									@"7",
									@"8",
									@"9",
								        @"10",
									@"11",
									@"12",
									nil];
	
	self.languagesColumn = array;
	[array release];
	
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    translatedTextLabel.text =  [translatedTextColumn objectAtIndex:row];

	
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    return [languagesColumn count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    return [languagesColumn objectAtIndex:row];
	
}

-(void)viewDidAppear:(BOOL)animated
{
	[super viewDidAppear:animated];
	[picker selectRow:1 inComponent:0 animated:NO];
}

-(IBAction)buttonPressed
{

}

As I've said, I'm only new, and this may be going in the wrong direction.
benblanchard is offline   Reply With Quote
Old 08-16-2010, 01:10 PM   #15 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by benblanchard View Post
.h

As I've said, I'm only new, and this may be going in the wrong direction.
That's fine.

So let me check with a some questions first:

The choices displayed in the Picker are basically the language choice i.e English, French etc.


When the user chooses a language you want to play a sound associated with that language?


Do you have the sound files added to your project in the resources folder? Are they named 2.wav, 3.wav etc?
wuf810 is offline   Reply With Quote
Old 08-16-2010, 01:13 PM   #16 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default

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.
benblanchard is offline   Reply With Quote
Old 08-16-2010, 01:25 PM   #17 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by benblanchard View Post
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];
I've not tested this so let know how you get on…
wuf810 is offline   Reply With Quote
Old 08-16-2010, 01:32 PM   #18 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default

That's great thanks, except for one error I receive.

It says "'path' undeclared" on the line below in red


Code:
translatedTextLabel.text =  [translatedTextColumn objectAtIndex:row];
	
	// 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];
benblanchard is offline   Reply With Quote
Old 08-16-2010, 01:38 PM   #19 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by benblanchard View Post
That's great thanks, except for one error I receive.

It says "'path' undeclared" on the line below in red


Code:
translatedTextLabel.text =  [translatedTextColumn objectAtIndex:row];
	
	// 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];
My mistake these lines

NSString *wavToPlay = [soundColumn objectAtIndex:row];
NSString *path = [[NSBundle mainBundle] pathForResource:wavToPlay ofType:@"wav"];

needs to go above the if (theAudio) line
wuf810 is offline   Reply With Quote
Old 08-16-2010, 01:43 PM   #20 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default

Okay, that returns no errors now. Thankyou

However, there is no code in:

Code:
-(IBAction)
{

}
and therefore, nothing happens when I click the button.

You really are a great help, thankyou.
benblanchard is offline   Reply With Quote
Old 08-16-2010, 01:53 PM   #21 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by benblanchard View Post
Okay, that returns no errors now. Thankyou

However, there is no code in:

Code:
-(IBAction)
{

}
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.
wuf810 is offline   Reply With Quote
Old 08-16-2010, 01:58 PM   #22 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default

Unfortunately, I get no sound playing at all.

I think it may be to do with the Frameworks. I have them added, and referenced (I think).
benblanchard is offline   Reply With Quote
Old 08-16-2010, 02:02 PM   #23 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by benblanchard View Post
Unfortunately, I get no sound playing at all.

I think it may be to do with the Frameworks. I have them added, and referenced (I think).
They must be added because otherwise you would get errors when compiling.

I've go to go now so suggest you do a bit of debugging like I suggested in my previous message.

You are very close…so go for it :-)
wuf810 is offline   Reply With Quote
Old 08-16-2010, 02:10 PM   #24 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 48
benblanchard is on a distinguished road
Default

Thanks for your help!

I'll see what I can do!
benblanchard is offline   Reply With Quote
Old 08-17-2010, 05:47 AM   #25 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by benblanchard View Post
Thanks for your help!

I'll see what I can do!
Did you make any progress on this?
wuf810 is offline   Reply With Quote
Reply

Bookmarks

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: 344
5 members and 339 guests
bignoggins, Chickenrig, givensur, linkmx, PlutoPrime
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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