Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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 如何将声音放入数组中?_Iphone_Objective C_Ios - Fatal编程技术网

Iphone 如何将声音放入数组中?

Iphone 如何将声音放入数组中?,iphone,objective-c,ios,Iphone,Objective C,Ios,我有10种声音,把它们放到一个数组中会更容易。但是我对Obj C是新手 这是到目前为止的代码,这只是一个声音。我可以复制粘贴10次。但如果所有10种声音都在一个数组中,我就可以用这些声音做更多的功能 - (IBAction)oneSound:(id)sender; { NSString *path = [[NSBundle mainBundle] pathForResource:@"Vocal 1" ofType:@"mp3"]; if (oneAudio) [oneAudio

我有10种声音,把它们放到一个数组中会更容易。但是我对Obj C是新手

这是到目前为止的代码,这只是一个声音。我可以复制粘贴10次。但如果所有10种声音都在一个数组中,我就可以用这些声音做更多的功能

- (IBAction)oneSound:(id)sender; {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Vocal 1" ofType:@"mp3"];
    if (oneAudio) [oneAudio release];
    NSError *error = nil;
    oneAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
    if (error)
        NSLog(@"%@",[error localizedDescription]);
    oneAudio.delegate = self;
    [oneAudio play];    


}
谢谢

这里有一个方法

NSMutableArray *soundArray = [NSMutableArray arrayWithCapacity:10];
[soundArray addObject:oneAudio];
.
.
.
[soundArray addObject:tenAudio];

或者,您也可以将音频内容的路径放入数组,并在需要时对其进行实例化。

将数组的文件路径制作成字符串,将其添加到数组中,并仅在需要时访问它们,否则将占用大量内存。

要将哪个对象放入数组?你找过关于Objective-C和数组的教程吗?这里有一个:这是关于集合的Apple文档:。基本上是10种声音,那么对象就是声音吗?知道对象是什么以及如何将对象添加到数组中几乎是最基本的基础知识,听起来你需要先阅读一两篇教程,尝试一些东西,如果你有特殊困难,请回到这里。我建议将声音路径作为字符串存储在数组中,并仅在需要时创建声音对象。声音对象可能需要大量内存。