Swiftgram/TelegramUI/ServiceSoundManager.swift
2017-04-18 19:53:47 +03:00

45 lines
1.3 KiB
Swift

import Foundation
import SwiftSignalKit
import AudioToolbox
private func loadSystemSoundFromBundle(name: String) -> SystemSoundID? {
let path = "\(frameworkBundle.resourcePath!)/\(name)"
let url = URL(fileURLWithPath: path)
var sound: SystemSoundID = 0
if AudioServicesCreateSystemSoundID(url as CFURL, &sound) == noErr {
return sound
}
return nil
}
public final class ServiceSoundManager {
private let queue = Queue()
private var messageDeliverySound: SystemSoundID?
private var incomingMessageSound: SystemSoundID?
init() {
self.queue.async {
self.messageDeliverySound = loadSystemSoundFromBundle(name: "MessageSent.caf")
self.incomingMessageSound = loadSystemSoundFromBundle(name: "notification.caf")
}
}
public func playMessageDeliveredSound() {
self.queue.async {
if let messageDeliverySound = self.messageDeliverySound {
AudioServicesPlaySystemSound(messageDeliverySound)
}
}
}
public func playIncomingMessageSound() {
self.queue.async {
if let incomingMessageSound = self.incomingMessageSound {
AudioServicesPlaySystemSound(incomingMessageSound)
}
}
}
}
public let serviceSoundManager = ServiceSoundManager()