Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(void)viewDidLoad {
- CFBundleRef mainBundle = CFBundleGetMainBundle();
- CFURLRef soundFileURLRef;
- soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"beat", CFSTR ("mp3"), NULL);
- UInt32 soundID;
- AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
- AudioServicesPlaySystemSound(soundID);
- }
- @interface MyClass : AnyParentClass {
- AVAudioPlayer *audioPlayer;
- }
- // ...
- @end
- NSURL *URLForSoundFile = // ... whatever but it must be a valid URL for your sound file
- NSError *error;
- if (audioPlayer == nil) {
- audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:URLForSoundFile error:&error];
- if (audioPlayer) {
- [audioPlayer setNumberOfLoops:-1]; // -1 for the forever looping
- [audioPlayer prepareToPlay];
- [audioPlayer play];
- } else {
- NSLog(@"%@", error);
- }
- }
- if (audioPlayer) [audioPlayer stop];
- // appDelegate.h
- @interface AppDelegate : UIResponder <UIApplicationDelegate>
- {
- UInt32 soundID;
- }
- //appDelegate.m
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- CFBundleRef mainBundle = CFBundleGetMainBundle();
- CFURLRef soundFileURLRef;
- soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"beat", CFSTR ("mp3"), NULL);
- AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
- AudioServicesPlaySystemSound(soundID);
- [NSTimer scheduledTimerWithTimeInterval:lenghtOfSound target:self selector:@selector(tick:) userInfo:nil repeats:YES];
- return YES;
- }
- -(void)tick:(NSTimer *)timer
- {
- AudioServicesPlaySystemSound(soundID);
- }
Add Comment
Please, Sign In to add comment