代码之家  ›  专栏  ›  技术社区  ›  Joe

如何为iPhone应用程序在Objective C中设置值?

  •  1
  • Joe  · 技术社区  · 15 年前

    我在Objective C和为iPhone开发方面很新,所以我很抱歉,如果这是一个简单的答案,我已经试过在Google上搜索3天了,并且买了iPhone开发的傻瓜版,在这样的事情上没有运气。

    基本上我想做的是在按下按钮时播放一个声音,然后我想在音频文件的值上加上1。例如

    //Play audio file (y.wav)   ---y being the variable I would like to set.
    if (y > 13) {               ---there are a total of 14 sounds starting with file 0.wav going to 13.wav.
       y = 0;                   ---so if the value is greater than 13 I want to start the cycle over with y equaling 0.
    } else {
       y++;                     ---if y is not greater than 13 I want to add one to it so it plays the next sound file next time.
    }
    

    我确实有一些JavaScript的历史,所以这个例子更多地反映了JavaScript而不是Objective-C。这个应用程序类似于iFart,但是在你开始讨论我的案例之前,不要再制作另一个不是我实际制作的放屁/打嗝应用程序,这只是我的应用程序这一部分的一个类似概念。因此,如果有人能帮助我学习如何将其转化为客观C,或者是一种完全不同的方法来达到我的目标,我将非常感激。

    谢谢,乔伊

    4 回复  |  直到 15 年前
        1
  •  1
  •   Amagrammer    15 年前
    - (void) playMySound
    {
        static int soundIndex = 0;
        const int soundCount = 14;
    
        // create sound name from base string and wound index, as Jeff shows above
    
        // play the sound
    
        if (++soundIndex >= soundCount)
        {
            soundIndex = 0;
        }
    
        // or just do it in one line, viz
        //soundIndex = (soundIndex + 1) % soundCount;
    
    }
    
        2
  •  0
  •   Jeff Kelley    15 年前

    你会想这样做:

    for ( NSUInteger i = 0; i < 14; i++ ) {
        NSString *filename = [ NSString stringWithFormat:@"%d.wav", i ];
        // Code to play the sound
    }
    

    有其他方法可以做到这一点,但是一定要查看NSString的开发人员文档 +stringWithFormat: 方法。

        3
  •  0
  •   VoidPointer    15 年前

    只要你愿意 周期 一些整数值,使用 modulo 增量期间的运算符。一般的模式是

    counter = (counter + 1) % maxValue;
    

    这将确保计数器始终在0和(maxValue-1)之间,这非常适合在包含maxValue元素的数组中循环。

    我也建议对你可以播放的声音的实际数量和文件名灵活。因此,你可以将你的声音名称存储在一个nsarry中。

    在你的班上,你应该有两个字段。包含声音名称的数组和包含要播放的当前声音编号的整数:

    NSUinteger currentSound;
    NSMutableArray *soundArray;
    

    初始化数组。例如在viewDidLoad中。。。

    currentSound = 0;
    // get the base URL of the app bundle
    NSURL *baseURL =[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
    NSMutableArray *sounds = [[NSMutableArray alloc] init];  
    // remember to release the array and the player objects on dealloc.
    // add sounds...
    NSError *p_error = nil;
    [sounds addObject: [[AVAudioPlayer alloc] initWithContentsOfURL:
        [NSURL URLWithString: @"1.wav" relativeToURL: baseURL] error: &p_error]];
    // check error an initialize other sounds...
    

    ... 现在在按下按钮时触发的操作中。。。

    AVAudioPlayer *player = [soundArray objectAtIndex: currentSound];
    currentSound = (currentSound + 1) % [soundArray count];
    [player play];
    

    modulo(%)运算符将起作用,以便一旦currentSound等于数组中对象的计数,它将 轻弹 回到0。

        4
  •  0
  •   JoeyParis    15 年前

    好吧,这就是我所拥有的。

    - (void)viewDidLoad {
            currentSound = 0;
            CFBundleRef mainBundle;
            mainBundle = CFBundleGetMainBundle ();
            NSURL *baseURL =[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
            NSMutableArray *sounds = [[NSMutableArray alloc] init];
            NSError *p_error = nil;
            [sounds addObject: [[AVAudioPlayer alloc] initWithContentsOfURL:
            [NSURL URLWithString: @"0.wav" relativeToURL: baseURL] error: &p_error]];
    
    
            soundArray = sounds;
            [super viewDidLoad];
        }
    
        - (IBAction)playFailSound {
            AVAudioPlayer *player = [soundArray objectAtIndex: currentSound];
            currentSound = (currentSound + 1) % [soundArray count];
            [player play];
    

    但是,我仍然不知道如何添加我自己的声音文件。我需要在头文件中声明AVAudioPlayer吗?如果是这样,我该怎么办?

    我再次为我把事情弄得过于复杂而道歉,但我正在努力,而且不想和苹果的文档相处。