Audioplayer play on condition
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
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];
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.
精彩评论