Skip to content

Instantly share code, notes, and snippets.

@irichardson
Last active February 3, 2020 07:03
Show Gist options
  • Save irichardson/1af5dca4a48ac8f6e571 to your computer and use it in GitHub Desktop.
Save irichardson/1af5dca4a48ac8f6e571 to your computer and use it in GitHub Desktop.
Singleton AudioManager written in Swift
//
// AudioManager.swift
// blocks
//
// Created by Ian Richardson on 8/22/14.
// Copyright (c) 2014 3 Screen Apps. All rights reserved.
//
import Foundation
import AVFoundation
class AudioManager {
var audioPlayer = AVAudioPlayer()
class var sharedInstance: AudioManager {
struct Static {
static var instance: AudioManager?
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token) {
Static.instance = AudioManager()
}
return Static.instance!
}
func playAudio(fileName: String, fileType: String, loop: Int){
var url = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource(fileName, ofType: fileType)!)
audioPlayer = AVAudioPlayer(contentsOfURL: url, error: nil)
audioPlayer.play()
audioPlayer.numberOfLoops = loop
}
func stopAudio(){
audioPlayer.stop()
}
}
@lastMove
Copy link

lastMove commented Oct 7, 2016

In the "PlayAudio" function just check which audio is playing, and if it's
the same don't restart it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment