         timer = nil
     }
 
-    private func addObservers() {
-        let center = NotificationCenter.default
-        center.addObserver(self, selector: #selector(handleNotification(_:)), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)
-        center.addObserver(self, selector: #selector(handleNotification(_:)), name: Notification.Name.UIApplicationDidEnterBackground, object: nil)
-    }
-
-    func handleNotification(_ notification: Notification) {
-        switch notification.name {
-        case Notification.Name.UIApplicationWillEnterForeground:
-            scheduleTimer()
-        case Notification.Name.UIApplicationDidEnterBackground:
-            invalidateTimer()
-        default:
-            break
-        }
+    func callout() {
+        callback?(player)
     }
 
     func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {


    static let sharedInstance = AudioPlayer()

    private let session: AVAudioSession = {
        let session = AVAudioSession.sharedInstance()
        try? session.setCategory(AVAudioSessionCategoryPlayback)
        return session
    }()
    private var commands: [Any] = []
    private var player: AVAudioPlayer?
    private var info: [String: Any] = [:]
    private var timer: Timer?
    private var callback: ((_ player: AVAudioPlayer?) -> Void)?

    func start() {
        try? session.setActive(true)
        UIApplication.shared.beginReceivingRemoteControlEvents()
        registerControlEvents()
        if UIApplication.shared.applicationState == .active {
            scheduleTimer()
        }
        addObservers()
    }

    func stop() {
        invalidateTimer()
        player?.stop()
        player = nil
        info.removeAll()
        MPNowPlayingInfoCenter.default().nowPlayingInfo = nil
        deregisterControlEvents()
        UIApplication.shared.endReceivingRemoteControlEvents()
        try? session.setActive(false)
    }

    func play(data: Data, time: Date, _ eventCallback: ((_ player: AVAudioPlayer?) -> Void)? = nil) {

        info[MPMediaItemPropertyAlbumTitle] = album ?? ""
        info[MPMediaItemPropertyArtist] = artist ?? ""
        if let artwork = artwork {
            info[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: artwork.size) { (size) in artwork }
        }
        update()
    }

    private func update() {
        info[MPMediaItemPropertyPlaybackDuration] = player?.duration ?? 0
        info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player?.currentTime ?? 0
        MPNowPlayingInfoCenter.default().nowPlayingInfo = info
    }

    private func handleControlEvents() {
        let center = MPRemoteCommandCenter.shared()
        center.pauseCommand.isEnabled = true
        center.playCommand.isEnabled = true
