Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import AVFoundation
- class SoundPlayer {
- // Singleton in order to access the player from 'everywhere'
- class var sharedInstance : SoundPlayer {
- struct Static {
- static let instance : SoundPlayer = SoundPlayer()
- }
- return Static.instance
- }
- // Properties
- var error:NSError?
- var audioPlayer = AVAudioPlayer()
- let soundURL = NSURL(string:"/System/Library/Audio/UISounds/sms-received1.caf")
- init() {
- AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
- AVAudioSession.sharedInstance().setActive(true, error: nil)
- audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: &error)
- if (error != nil) {
- println("[SoundPlayer] There was an error: \(error)")
- }
- // else {
- // audioPlayer.prepareToPlay()
- // }
- }
- func playSound() {
- if (audioPlayer.playing) {
- audioPlayer.stop()
- }
- audioPlayer.play()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement