Last active
February 3, 2020 07:03
-
-
Save irichardson/1af5dca4a48ac8f6e571 to your computer and use it in GitHub Desktop.
Singleton AudioManager written in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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() | |
} | |
} |
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
I utilize the AudioManager Class in a view controller like this:
Then I dismiss the current view controller and re-enter the view controller, the audio restart and play from the beginning.
I want to know how to make the audio continue playing without restart.
Thank you.