你好 StackOverflow 大师。这是我在这里的第一个问题,所以我很高兴能直接加入。
我试图更好地理解 iOS 数组,但我遇到了障碍。我正在制作一个使用 FMOD 的声音应用程序。我的一切工作正常,但我有 9 个按钮,它们的功能几乎完全相同,只是每个按钮在按下时播放不同的 .wav 文件,然后在释放时停止声音。我想将它放入一个数组中并简化和缩短我的代码,这就是我迷路的地方。我剥离了代码以显示我目前正在做的事情。有任何想法吗?
。H
@interface {
FMOD::Sound *sound1;
FMOD::Sound *sound2;
FMOD::Sound *sound3;
FMOD::Sound *sound4;
FMOD::Sound *sound5;
FMOD::Sound *sound6;
FMOD::Sound *sound7;
FMOD::Sound *sound8;
FMOD::Sound *sound9;
}
- (IBAction)playSound1:(id)sender;
- (IBAction)stopSound1:(id)sender;
- (IBAction)playSound2:(id)sender;
- (IBAction)stopSound2:(id)sender;
- (IBAction)playSound3:(id)sender;
- (IBAction)stopSound3:(id)sender;
- (IBAction)playSound4:(id)sender;
- (IBAction)stopSound4:(id)sender;
- (IBAction)playSound5:(id)sender;
- (IBAction)stopSound5:(id)sender;
- (IBAction)playSound6:(id)sender;
- (IBAction)stopSound6:(id)sender;
- (IBAction)playSound7:(id)sender;
- (IBAction)stopSound7:(id)sender;
- (IBAction)playSound8:(id)sender;
- (IBAction)stopSound8:(id)sender;
- (IBAction)playSound9:(id)sender;
- (IBAction)stopSound9:(id)sender;
米。
- (void)viewWillAppear:(BOOL)animated {
[[NSString stringWithFormat:@"%@/sound1.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];
result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &sound1);
ERRCHECK(result);
[[NSString stringWithFormat:@"%@/sound2.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];
result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &sound2);
ERRCHECK(result);
[[NSString stringWithFormat:@"%@/sound3.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];
result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &sound3);
ERRCHECK(result);
[[NSString stringWithFormat:@"%@/sound4.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];
result = system->createSound(buffer, FMOD_SOFTWARE, NULL, &sound4);
ERRCHECK(result);
result = sound4->setMode(FMOD_LOOP_NORMAL);
ERRCHECK(result);
[[NSString stringWithFormat:@"%@/sound5.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];
result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &sound5);
ERRCHECK(result);
[[NSString stringWithFormat:@"%@/sound6.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];
result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &sound6);
ERRCHECK(result);
[[NSString stringWithFormat:@"%@/sound7.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];
result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &sound7);
ERRCHECK(result);
[[NSString stringWithFormat:@"%@/sound8.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];
result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &sound8);
ERRCHECK(result);
[[NSString stringWithFormat:@"%@/sound9.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];
result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &sound9);
ERRCHECK(result);
}
- (IBAction)playSound1:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = system->playSound(FMOD_CHANNEL_FREE, sound1, false, &wob01);
ERRCHECK(result);
}
- (IBAction)stopSound1:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = wob01->stop();
ERRCHECK(result);
}
- (IBAction)playSound2:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = system->playSound(FMOD_CHANNEL_FREE, sound2, false, &wob02);
ERRCHECK(result);
}
- (IBAction)stopSound2:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = wob02->stop();
ERRCHECK(result);
}
- (IBAction)playSound3:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = system->playSound(FMOD_CHANNEL_FREE, sound3, false, &wob03);
ERRCHECK(result);
}
- (IBAction)stopSound3:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = wob03->stop();
ERRCHECK(result);
}
- (IBAction)playSound4:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = system->playSound(FMOD_CHANNEL_FREE, sound4, false, &wob04);
ERRCHECK(result);
}
- (IBAction)stopSound4:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = wob04->stop();
ERRCHECK(result);
}
- (IBAction)playSound5:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = system->playSound(FMOD_CHANNEL_FREE, sound5, false, &wob05);
ERRCHECK(result);
}
- (IBAction)stopSound5:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = wob05->stop();
ERRCHECK(result);
}
- (IBAction)playSound6:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = system->playSound(FMOD_CHANNEL_FREE, sound6, false, &wob06);
ERRCHECK(result);
}
- (IBAction)stopSound6:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = wob06->stop();
ERRCHECK(result);
}
- (IBAction)playSound7:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = system->playSound(FMOD_CHANNEL_FREE, sound7, false, &wob07);
ERRCHECK(result);
}
- (IBAction)stopSound7:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = wob07->stop();
ERRCHECK(result);
}
- (IBAction)playSound8:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = system->playSound(FMOD_CHANNEL_FREE, sound8, false, &wob08);
ERRCHECK(result);
}
- (IBAction)stopSound8:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = wob08->stop();
ERRCHECK(result);
}
- (IBAction)playSound9:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = system->playSound(FMOD_CHANNEL_FREE, sound9, false, &wob09);
ERRCHECK(result);
}
- (IBAction)stopSound9:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = wob09->stop();
ERRCHECK(result);
}
如您所见,所有代码都是重复的。这是我能够让它工作的唯一方法,但我知道这些可以放入一个数组中,我就是想不通。可能是一个 NSMutableArray 并列出“sound1”、“sound2”等。然后在界面生成器中为每个按钮分配一个标签?理想情况下,我想要一个用于 stopSound 的函数,一个用于 playSound 等,它使用标签来播放或停止正确的声音文件。使用 FMOD 的 system->createSound() 时,最后一个参数是一个变量,用于存储新创建的声音。有没有办法将其存储在数组或字典中?如果是这样,我无法弄清楚。
任何建议都将不胜感激。我很想不要再为这个简单的问题头疼了。
谢谢!