[英]want to create an audio playlist dynamically by using “.plist” file an then want to play it in iphone in built audio player
[英]I need to create an audio player with 4-5 melodies
我是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個旋律:(
PS請原諒我的英語,我剛剛離開西伯利亞
如果我答對了,您應該添加方法-
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[player release];
}
並且不要忘記將視圖控制器設置為播放器委托。
aSound.delegate=self;
您在每個周期上分配一個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)並將它們與四個按鈕相關聯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.