简体   繁体   English

如何将声音排列成阵列?

[英]How to put sounds in an array?

I have 10 sounds, and it would be easier for me to put them into an array. 我有10种声音,对我来说,将它们放入数组会更容易。 However I'm new to Obj C. 但是我是Obj C的新手。

Here is the code so far, this is just for 1 sound. 这是到目前为止的代码,仅适用于1种声音。 I could copy and paste it 10 times. 我可以复制并粘贴10次。 But if all 10 sounds are in an array I'll be able to do more functions with the sounds. 但是,如果所有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];    


}

thanks 谢谢

Heres one way, 这是一种方式

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

or you could also put the paths to the audio content into the Array and instantiate them when required. 或者您也可以将音频内容的路径放入Array中,并在需要时实例化它们。

取数组的文件路径,将它们变成字符串,将THOSE添加到数组,然后仅在需要时访问它们,否则您将消耗大量内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM