Guest User

Untitled

a guest
Jan 17th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. -(void)viewDidLoad {
  2.  
  3. CFBundleRef mainBundle = CFBundleGetMainBundle();
  4. CFURLRef soundFileURLRef;
  5. soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"beat", CFSTR ("mp3"), NULL);
  6. UInt32 soundID;
  7. AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
  8. AudioServicesPlaySystemSound(soundID);
  9. }
  10.  
  11. @interface MyClass : AnyParentClass {
  12. AVAudioPlayer *audioPlayer;
  13. }
  14.  
  15. // ...
  16.  
  17. @end
  18.  
  19. NSURL *URLForSoundFile = // ... whatever but it must be a valid URL for your sound file
  20. NSError *error;
  21.  
  22. if (audioPlayer == nil) {
  23. audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:URLForSoundFile error:&error];
  24. if (audioPlayer) {
  25. [audioPlayer setNumberOfLoops:-1]; // -1 for the forever looping
  26. [audioPlayer prepareToPlay];
  27. [audioPlayer play];
  28. } else {
  29. NSLog(@"%@", error);
  30. }
  31. }
  32.  
  33. if (audioPlayer) [audioPlayer stop];
  34.  
  35. // appDelegate.h
  36. @interface AppDelegate : UIResponder <UIApplicationDelegate>
  37. {
  38. UInt32 soundID;
  39. }
  40.  
  41. //appDelegate.m
  42. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  43. {
  44. CFBundleRef mainBundle = CFBundleGetMainBundle();
  45. CFURLRef soundFileURLRef;
  46. soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"beat", CFSTR ("mp3"), NULL);
  47. AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
  48. AudioServicesPlaySystemSound(soundID);
  49.  
  50.  
  51. [NSTimer scheduledTimerWithTimeInterval:lenghtOfSound target:self selector:@selector(tick:) userInfo:nil repeats:YES];
  52. return YES;
  53. }
  54.  
  55. -(void)tick:(NSTimer *)timer
  56. {
  57. AudioServicesPlaySystemSound(soundID);
  58. }
Add Comment
Please, Sign In to add comment