Simple way to play a sound file (.aif)

后端 未结 4 1304
猫巷女王i
猫巷女王i 2021-02-02 17:45

I\'m making an app for Mac OS X using Xcode, and I want it to play a alert sound when something happens.

What is the simplest code for playing a sound in Objective-C/Coc

4条回答
  •  [愿得一人]
    2021-02-02 18:16

    For example, add the AudioToolbox framework, the add the sound file to your resources / project, then add the below to your .h:

    #import 
    

    And then: (after @interface { blah } in your .h)

    - (IBAction) playSound;
    

    Add the following to your .m: (change 'Ping' and 'wav' to the respective file name and type)

    - (IBAction) playSound
    {
        SystemSoundID soundID;
        NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"Ping" ofType:@"wav"];
        AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
        AudioServicesPlaySystemSound(soundID); 
        [soundFile release];
    }
    

提交回复
热议问题