5

I have an app I am working on and It uses some of the hardware sensors to provide data on screen, there is a Label that updates with the number. I want a sound to play whenever the number is above 100 or something. For example, say it was reading numbers then all of the sudden it finds a good spot (or whatever), then I would like a sound to play or a light to light up. I am an absolute beginner and it would be nice if the answer would be easy for a absolute beginner to understand.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
DanielsCaleb0
  • 81
  • 1
  • 2
  • 10

6 Answers6

10

I am using the system AudioToolbox.framework for playing sounds in my simple game. I added this static function to common MyGame class:

+ (SystemSoundID) createSoundID: (NSString*)name
{
  NSString *path = [NSString stringWithFormat: @"%@/%@",
                     [[NSBundle mainBundle] resourcePath], name];


  NSURL* filePath = [NSURL fileURLWithPath: path isDirectory: NO];
  SystemSoundID soundID;
  AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
  return soundID;
} 

I added the "Morse.aiff" file to project Resources and initialized it in (any) class initialization with the following:

self.mySound = [MyGame createSoundID: @"Morse.aiff"];

And then I played sound with this call:

AudioServicesPlaySystemSound(mySound);

Also, don't forget to import AudioServices.h file.

This audio toolbox can play also different sound formats.

Fergal
  • 5,213
  • 6
  • 35
  • 44
Krešimir Prcela
  • 4,257
  • 33
  • 46
  • Remember that you will need to call AudioServicesDisposeSystemSoundID to avoid memory leak! (because SystemSoundID is from a lower-level framework) – am_ May 14 '13 at 08:41
4

h

#import <AVFoundation/AVFoundation.h>

@interface CMAVSound : NSObject {
    AVAudioPlayer *audioPlayer;
}

- (id)initWithPath:(NSString*)fileNameWithExctension;
- (void)play;

@end

m

#import "CMAVSound.h"

@implementation CMAVSound

-(void)dealloc {
    [audioPlayer release];
}

- (id)initWithPath:(NSString*)fileNameWithExctension {
    if ((self = [super init])) {
        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], fileNameWithExctension]];

        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

        if (audioPlayer == nil) {
            NSLog(@"%@", [error description]);
        }
    }
    return self;
}

- (void)play {
    [audioPlayer play];
}

@end
borisdiakur
  • 10,387
  • 7
  • 68
  • 100
1

Check out the documentation for the AVAudioPlayer class. It allows you to play sounds clips. If you have troubles implementing that, show us some code.

Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88
1

If the sounds gonna up to 5 seconds and no stereo output is needed I would recommend you to do that with system sounds. It is easy and better solution then any other. Apple sample code is provided under name SysSound

EDIT1 Or maybe tutorial could help you more http://howtomakeiphoneapps.com/2009/08/how-to-play-a-short-sound-in-iphone-code/

Vanya
  • 4,973
  • 5
  • 32
  • 57
  • this is great, def. helps. any idea how to make it work when it gets past a certain number? – DanielsCaleb0 Nov 18 '10 at 15:25
  • 1
    without seeing any code it is not easy to say how, but let's guess your number is integer, there should be in your code if-statement which will compare values (place it where you assign the values) then just under the if-statement place your system sound and it gonna play – Vanya Nov 18 '10 at 15:38
  • This explains the different players and when to use them: http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html – griotspeak Nov 18 '10 at 22:24
0

Here you can find list of AudioServicesPlaySystemSound

http://iphonedevwiki.net/index.php/AudioServices

Ramdhas
  • 1,765
  • 1
  • 18
  • 26