Reputation: 589
I'm trying to play a short alarm sound in my iphone application i'm using this code i found but it won't play any sound.
NSString *toneFilename = [[NSBundle mainBundle] pathForResource:@"alarm" ofType:@"wav"];
NSURL *toneURLRef = [NSURL fileURLWithPath:toneFilename];
SystemSoundID Sound;
AudioServicesCreateSystemSoundID(
(__bridge CFURLRef) toneURLRef,
&camerSound
);
AudioServicesPlaySystemSound(Sound);
when i try to make vibration it works though :
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
Thx
Upvotes: 2
Views: 1055
Reputation:
This works perfect for me.
Add AVFoundation and AudioToolBox frameworks into the project
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface TestViewController : UIViewController
{
SystemSoundID SoundID;
}
@implementation TestViewController
-(void) viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"caf"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &SoundID); //When using ARC, make sure to use __bridge
}
-(void) playSound {
AudioServicesPlaySystemSound(SoundID);
}
Upvotes: 1
Reputation: 2216
Using AVFoundationFramework
Add the AVFoundation.framework
to project.
#import <AVFoundation/AVFoundation.h>
NSString *toneFilename = [[NSBundle mainBundle] pathForResource:@"alarm" ofType:@"wav"];
NSURL *toneURLRef = [NSURL fileURLWithPath:toneFilename];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL: toneURLRef error: nil];
player.currentTime = 0;
player.volume = 1.0f;
[player play];
Upvotes: 3
Reputation: 80265
You are not addressing your Sound
variable correctly when creating the sound. Instead you are using camerSound
. Calling the AudioServicesPlayAlertSound
with the correct parameter should work.
Upvotes: 1