Iphone 当我到达ios5中的最后一个声音片段时,应用程序挂起

Iphone 当我到达ios5中的最后一个声音片段时,应用程序挂起,iphone,ios5,ios4,Iphone,Ios5,Ios4,我有一个带有图像的应用程序,一旦用户按下图像,就会播放声音片段。当我看到最后一张没有声音剪辑的图像(25)时,如果我按下该按钮,应用程序将自动关闭,我如何防止出现这种情况 例如,我需要在用户单击01.Png后播放C01.mp3(注意,我的所有图像也保存在图像阵列中)基本上我有25.Png这只是我网站的广告,因此它不会有声音剪辑,但一旦我单击该图像,应用程序就会自动关闭。需要帮助来解决这个问题 // #import "SoundsGenerator.h" @implementation So

我有一个带有图像的应用程序,一旦用户按下图像,就会播放声音片段。当我看到最后一张没有声音剪辑的图像(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
替换

for(NSString *soundName in soundNames)
{

for(int i=0;i

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

程序到底在哪里崩溃?控制台消息是什么?您只有24个声音名称,您的意思是它在24日崩溃?还是说它在24日之后迭代,当它成为哨兵时崩溃?如果您试图播放一个不存在的声音(访问超过soundObjects边界的索引),然后您将崩溃。是的,那么我如何避免需要检查图像是否为img1.png,然后它应该播放c01.mp3实际上最后一个图像只是添加到我的其他应用程序中,因此不会有任何声音文件
for(int i = 0 ; i < soundNames.count-1 ; i++)
{
   NSString *soundName = [soundNames objectAtIndex:i];
   //rest of the code same