views:

55

answers:

2

hi all, i hv few images , and i am showing these images through for loop, so i am getting a value in mCurrentImageView , which indicates which image is currently shown up.

now i want to play audio on each image view, different audios. for ex,

if(mCurrentImageView ==0) {
play "a"
}
if (mCurrentImageView ==1)
{
play "b"
}
....

something like dat. bu t i am not sure how to do this, since i should have one Audioplayer, which should play different audios on the basis opf mCurrentImageView's value.

this is my Avaudioplayer code

filePath = [[NSBundle mainBundle] pathForResource:@"b"ofType:@"mp3"];
    fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    [player prepareToPlay];
    [player play];

plz suggest, what should i do to move further.

regards

+3  A: 

The most simple solution is to use AVAudioPlayer. So when you want to play new sound you just release old player and create a new one with file you want to play.

eviltrue
i edit my question by adding my player code.So if i hv 20 audios, than i hv to create 20 AudioPLayer???is it like dat??
shishir.bobby
not really, you should just destroy and create a new player each time you need it.
eviltrue
+2  A: 

AVAudioPlayer only allows you to specify an URL when it is initialized, thus, as eviltrue says, you have to create a new one to play a different file. E.g. assuming you have a property holding the paths and a property for the player:

@property (readonly, copy) NSArray *paths;
@property (readwrite, retain) AVAudioPlayer *player;

And you have initialized the paths:

paths = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", nil];

You can do the following when the image changes:

NSString *path = [paths objectAtIndex:mCurrentImage];
filePath = [[NSBundle mainBundle] pathForResource:path ofType:@"mp3"];
fileURL = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
self.player = newPlayer;
[newPlayer release];
[self.player prepareToPlay];
[self.player play];
Georg Fritzsche
dats f9....but track should match to the image which is currenlty on view.so as i asked in my question, if(image ==1){play track1}...something like dat.
shishir.bobby
@shishir: Yes, thats what the array is used for - take a look at the `[paths objectAtIndex:mCurrentImage]` expression.
Georg Fritzsche
oh yea....sry ..i missed that line....
shishir.bobby