Audioplayer在条件下发挥

时间:2010-09-16 09:09:02

标签: iphone avaudioplayer

我有很少的图像,我通过for循环显示这些图像,所以我在mCurrentImageView中得到一个值,表示当前显示的是哪个图像。

现在我想在每个图像视图,不同的音频上播放音频。 对于前,

if(mCurrentImageView ==0) {
play "a"
}
if (mCurrentImageView ==1)
{
play "b"
}
....
像pt这样的东西。 我不知道该怎么做,因为我应该有一个Audioplayer,它应该根据opf mCurrentImageView的值来播放不同的音频。

这是我的Avaudioplayer代码

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

PLZ建议,我该怎么做才能继续前进。

问候

2 个答案:

答案 0 :(得分:3)

最简单的解决方案是使用AVAudioPlayer。因此,当您想要播放新的声音时,您只需发布旧播放器并使用您想要播放的文件创建一个新播放器。

答案 1 :(得分:3)

AVAudioPlayer只允许您在初始化时指定URL,因此,正如eviltrue所说,您必须创建一个新的URL来播放不同的文件。例如。假设你有一个属性来保存玩家的路径和属性:

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

你已经初步化了路径:

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

图像更改时,您可以执行以下操作:

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];