Advertisement
Guest User

SoundPlayer.swift

a guest
Apr 1st, 2015
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import AVFoundation
  2.  
  3. class SoundPlayer {
  4.  
  5. // Singleton in order to access the player from 'everywhere'
  6. class var sharedInstance : SoundPlayer {
  7. struct Static {
  8. static let instance : SoundPlayer = SoundPlayer()
  9. }
  10. return Static.instance
  11. }
  12.  
  13. // Properties
  14. var error:NSError?
  15. var audioPlayer = AVAudioPlayer()
  16. let soundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound", ofType: "wav")!) // Working
  17. //let soundURL = NSURL(string:"/System/Library/Audio/UISounds/sms-received1.caf") // Working too
  18.  
  19. init() {
  20. AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
  21. AVAudioSession.sharedInstance().setActive(true, error: nil)
  22.  
  23. audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: &error)
  24.  
  25. if (error != nil) {
  26. println("[SoundPlayer] There was an error: \(error)")
  27. }
  28. // else {
  29. // audioPlayer.prepareToPlay()
  30. // }
  31. }
  32.  
  33. func playSound() {
  34. if (audioPlayer.playing) {
  35. audioPlayer.stop()
  36. }
  37. audioPlayer.play()
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement