我是新手在Iphone SDK编程,我需要创建音频播放器,但没有阵列。旋律有名字"sound1,sound2,sound3...“
我的代码如下:
NSUInteger x;
for (x=1; x<=tuneNums; x++)
{
path = [[NSBundle mainBundle] pathForResource:@"sound%d" ofType:@"wav"];
if([[NSFileManager defaultManager] fileExistsAtPath:path])
{
aSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
[aSound setNumberOfLoops:0];
aSound.delegate = self;
[aSound prepareToPlay];
}
}
所有的按钮,我有和工作很好,但只有一个梅洛迪:(但如何释放4-5旋律我不知道:
另外,请原谅我的英语--我刚从西伯利亚来
发布于 2011-03-11 17:59:09
你在每个周期分配一个旋律,并且你只得到一个‘AVAudioPlayer’,因为这是在最后一个周期中分配的。
正如7KV7所指出的,您应该在用户按下按钮时分配一个AVAudioPlayer,播放相应的音频文件,然后在按下另一个按钮时释放/重新创建它。
类似于:
- (void)button1Pressed:(id)sender {
// release the current player
if(aSound) {
[aSound release], aSound = nil;
}
// create a new player with the first file
aSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
[aSound setNumberOfLoops:0];
aSound.delegate = self;
[aSound prepareToPlay];
}
如何确定按钮被按下时播放哪个音频文件由您决定。如果你打算只有四个音频文件,最简单的方法是有固定数量的buttonPressed选择器(例如button1Pressed,button2Pressed),并将它们与你的四个按钮相关联。
发布于 2011-03-11 17:31:45
如果我没弄错的话,你应该添加这个方法-
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[player release];
}
别忘了把你的视图控制器设置为播放器代理。
aSound.delegate=self;
https://stackoverflow.com/questions/5271021
复制相似问题