0

我有一个带有图像的应用程序,一旦用户按下图像,就会播放声音剪辑。一旦我到达最后一张没有声音剪辑的图像(25),如果我按下应用程序自动关闭,我该如何防止这种情况发生。

例如,一旦用户单击 01.Png,我需要播放 C01.mp3 (注意我的所有图像也保存在图像数组中)基本上我有 25.png 这只是我网站的广告,所以它不会有声音剪辑,但是一旦我单击该图像,应用程序就会自动关闭。需要帮助来解决这个问题。

//

#import "SoundsGenerator.h"


@implementation SoundsGenerator

- (id) init
{
    CFBundleRef mainBundle;
    mainBundle = CFBundleGetMainBundle ();

    soundObjects=[[NSMutableArray alloc] init];

    NSMutableArray *soundNames = [[NSMutableArray alloc] initWithObjects:@"c1",
                                @"c2", 
                                @"c3", 
                                @"c4", 
                                @"c5", 
                                @"c6", 
                                @"c7", 
                                @"c8", 
                                @"c9", 
                                @"c10", 
                                @"c11", 
                                @"c12", 
                                @"c13", 
                                @"c14", 
                                @"c15", 
                                @"c16", 
                                @"c17", 
                                @"c18", 
                                @"c19", 
                                @"c20", 
                                @"c21", 
                                @"c22", 
                                @"c23", 
                                @"c24",nil];    

    for(NSString *soundName in soundNames)
    {
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (__bridge CFStringRef)soundName, CFSTR ("mp3"), NULL);

        SystemSoundID soundFileObject;
        // Create a system sound object representing the sound file
        AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject);

        SoundObject *objSound=[[SoundObject alloc] init];
        objSound.soundFileObject=soundFileObject;
        objSound.soundFileURLRef=soundFileURLRef;
        [soundObjects addObject:objSound];
    }

    return self;
}

// Respond to a tap on the System Sound button
- (void) playSound:(int)index 
{
    SoundObject *soundObject=[soundObjects objectAtIndex:index];
    AudioServicesPlaySystemSound ((SystemSoundID)soundObject.soundFileObject);
}


@end
4

1 回答 1

0

代替

for(NSString *soundName in soundNames)
{

for(int i = 0 ; i < soundNames.count-1 ; i++)
{
   NSString *soundName = [soundNames objectAtIndex:i];
   //rest of the code same

这将运行循环直到 24 个索引数组的 23 个索引。

于 2013-01-12T05:26:20.343 回答