Playing sound on the Mac Desktop in Objective C.

By | March 11, 2011

There are different ways for playing sound on the Mac in Objective C.

Let’s look at some of the methods..
1. Play by filename.

           NSSound *mySound = [NSSound soundNamed:@"sound"];

if you have no file named “sound” in your application’s main bundle, then it will return nil.

2. Play by pathName

           sound = [[NSSound alloc] initWithContentsOfFile:@"/Volumes/Audio/sound.aiff"
           byReference:YES];

3. Play by URL

        NSURL *soundURL = [NSURL
        fileURLWithString:@"file://~/soundfiles/sound.aiff"];
        NSSound *sound = [[NSSound alloc] initWithContentsOfURL:soundURL

byReference:NO];
Then to play the sound add

         if (![sound isPlaying])
                [sound play];

To pause the sound

               [sound pause];

To stop the sound.

              [sound stop];

To resume the sound

             [sound resume];

Please leave your valuable comments if this post was useful…..

Leave a Reply

Your email address will not be published. Required fields are marked *