Dynamic loading of mp3 files in AVAudioPlayer - Pass-by-value argument
I am trying to play dynamically few mp3 sounds using AVAudioPlayer.
The following code works fine and indeed plays the sound, nevertheless I get this error when Analyzing
"Pass-by-value argument in messag开发者_JAVA技巧e expression is undefined"
Why so and how should one fix it?
+(void)playSound:(int)soundName
{
NSString *theSound;
switch (soundName) {
case 1:
theSound = @"beep1.mp3";
break;
case 2:
theSound = @"beep2.mp3";
break;
case 3:
theSound = @"beep3.mp3";
break;
default:
break;
}
//
NSURL *s1 = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], theSound]];
NSError *error;
AVAudioPlayer *sound1;
sound1 = [[[AVAudioPlayer alloc] initWithContentsOfURL:s1 error:&error] autorelease];
sound1.numberOfLoops = 0;
//[sound1 prepareToPlay];
[sound1 play];
}
Do you really want to try and play a sound file with no path if soundName
is not 1,2 or 3?
For your default clause, either report an error, or just silently return. Don't create a player with an invalid path and ask to it to play it, it will only get upset.
Try NSString *theSound = @"";
Setting a default value usually solves this problem.
Explanation:
The compiler is not sure that theSound
will actually contain a value when it is used. It is good practice to assign default values to some objects, especially when dealing with loops and selection statements.
精彩评论