views:

182

answers:

4

Hi.

I have written the following code:

-(void)runTimer
{
    myTicker=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
    myTicker = [NSTimer scheduledTimerWithTimeInterval:60.00 target:self selector:@selector(vibrate) userInfo:nil repeats:YES];
}

-(void)showActivity
{
    NSDateFormatter *formatter =
                  [[[NSDateFormatter alloc] init] autorelease];
            NSDate *date = [NSDate date];
        [formatter setTimeStyle:NSDateFormatterMediumStyle];
         [timerLabel setText:[formatter stringFromDate:date]];
}
-(void) vibrate{
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);

}

I want to play an audio file instead of vibrating the iPhone. Is that possible?

A: 

Yes. Assume you have a .caf file, you can play it with

SystemSoundID sound_id;
AudioServicesCreateSystemSoundID(NSURL_of_your_caf_file, &sound_id);
...
AudioServicesPlaySystemSound(sound_id);

To play music you may want to use AVAudioPlayer instead.

KennyTM
A: 

Sure. See AVAudioPlayer class.

Costique
+1  A: 

Tangential to the main question but the method called by the timer selector should be of the form...

-(void) aribtaryMethodName:(NSTimer *) theTimer;

...or it may not be called properly. It's selector should look like this:

myTicker=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(aribtaryMethodName:) userInfo:nil repeats:YES];
TechZen
+1 even thought not answer to the actual question, but an important fix to presented code to make it work. Thanx!
JOM
A: 

Check Apple's sample project BubbleLevel. You want the SoundEffect.h and SoundEffect.m into your project and then just call:

mySound = [[SoundEffect alloc]
    initWithContentsOfFile:[mainBundle pathForResource:@"mySound"
    ofType:@"aif"]];
[mySound play];    

Warning: I was able to play only AIF sounds converted via iTunes. Anything else failed, including CAF files. In short: import your whatever sound into iTunes (drag'n'drop), change export to AIF format and then export the file.

JOM