Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 我需要创建一个有4-5个旋律的音频播放器_Iphone_Cocoa Touch_Avaudioplayer_Avfoundation - Fatal编程技术网

Iphone 我需要创建一个有4-5个旋律的音频播放器

Iphone 我需要创建一个有4-5个旋律的音频播放器,iphone,cocoa-touch,avaudioplayer,avfoundation,Iphone,Cocoa Touch,Avaudioplayer,Avfoundation,我是Iphone SDK编程新手 我需要创建音频播放器,但没有阵列。旋律有名字“sound1,sound2,sound3…” 我的代码在这里: NSUInteger x; for (x=1; x<=tuneNums; x++) { path = [[NSBundle mainBundle] pathForResource:@"sound%d" ofType:@"wav"]; if([[NSFileManager defaultManager] fileExistsAtPat

我是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];
    }       
}
nsux整数;

对于(x=1;x如果我没弄错,你应该添加方法-

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

如何确定按下按钮时播放哪个音频文件取决于您。如果您只有四个音频文件,最简单的方法是使用固定数量的按钮按下选择器(例如按钮1按下,按钮2按下)并将它们关联到您的四个按钮。

为什么要分配AVAudioPlayer多次?您不能只分配一次,然后使用它。我考虑过创建按钮的方法,但melody的数量也会增加按钮的数量,这不会写入例如我使用循环创建的20个按钮:)melody连接到按钮,因此使用Melodies的循环,您可以创建按钮,动态地将音频文件的索引存储在每个按钮的标记中,并检索它以确定要播放的文件:int index=((UIButton*)sender).tag;我以前遇到过这个问题,但是由于没有关于什么标签没有引起注意的详细例子,如果有一个例子可以分享,请!!!文档中的“标记:一个整数,可用于标识应用程序中的视图对象。您可以设置此标记的值,然后使用该值标识视图。”没问题。如果你觉得这个答案有用,请别忘了投赞成票/接受它。