move old music code here

This commit is contained in:
Alfred Gao 2017-01-06 16:51:39 +08:00
parent 4fd9f7abdd
commit 813f28f7e1
3 changed files with 133 additions and 11 deletions

View File

@ -54,13 +54,13 @@ public class ResourcePackageReader: NSObject {
///
/// only chack this during 2 step loacting
#if os(iOS)
var _devicecustimizable = true
var _deviceType = ".ipad"
var _deviceModel = ".ipadmini"
var _devicecustimizable = true
var _deviceType = ".ipad"
var _deviceModel = ".ipadmini"
#elseif os(OSX)
var _devicecustimizable = false
var _deviceType = ""
var _deviceModel = ""
var _devicecustimizable = false
var _deviceType = ""
var _deviceModel = ""
#endif
/// read data from package
@ -183,15 +183,17 @@ public class ResourcePackageReader: NSObject {
super.init()
}
public var onDeInit: [() -> Void] = []
deinit {
for fn in onDeInit {
fn()
for (_, soundid) in sounds {
AudioServicesDisposeSystemSoundID(soundid)
}
}
/// used by Themes extension
var _textFormater = TextFormater()
/// used by sounds extension
var musicPlayer : AVAudioPlayer?
var sounds: [String : SystemSoundID] = [:]
}

View File

@ -0,0 +1,110 @@
//
// Sounds.swift
//
// Created by Alfred Gao on 2017/1/6.
//
//
import Foundation
import AVFoundation
extension ResourcePackageReader {
///
///
/// sound effect
/// - parameter withVibrate:
public func playSound(key: String, withVibrate: Bool = false) {
// get volume config from other class
var soundToPlay : SystemSoundID = 0
if let _sid = sounds[key] {
// reuse loaded sound
soundToPlay = _sid
} else {
// loading sound data
var _sdata: Data? = Data()
if !_useprefix {
_sdata = getData(key: key)
} else {
if let _value = getData(key: _keyprefix + key) {
_sdata = _value
} else {
_sdata = getData(key: _keyprefixbackward + key)
}
}
// register system sound
if _sdata != nil {
let _file = URL(fileURLWithPath: NSTemporaryDirectory().appending("sounds.\(UUID())"))
do {
// write to temp file
try _sdata!.write(to: _file)
// reg sound
AudioServicesCreateSystemSoundID(_file as CFURL, &soundToPlay)
if soundToPlay != 0 {
sounds[key] = soundToPlay
}
} catch {
// reg failed
return
}
// delete temp file
do {
try FileManager.default.removeItem(at: _file)
} catch {
}
} else {
// load sound data failed
return
}
}
if withVibrate {
AudioServicesPlayAlertSound(soundToPlay)
} else {
AudioServicesPlaySystemSound(soundToPlay)
}
}
///
///
/// play music
func playMusic(_ key: String, loops: Int = 1, volume: Float = 100) {
stopMusic()
// load music data
var _sdata: Data? = Data()
if !_useprefix {
_sdata = getData(key: key)
} else {
if let _value = getData(key: _keyprefix + key) {
_sdata = _value
} else {
_sdata = getData(key: _keyprefixbackward + key)
}
}
// play music
if let _musicData = _sdata {
do {
musicPlayer = try AVAudioPlayer(data: _musicData)
musicPlayer?.volume = volume
musicPlayer?.numberOfLoops = loops
musicPlayer?.play()
} catch {
}
}
}
///
///
/// stop music
func stopMusic() {
musicPlayer?.stop()
}
}

View File

@ -11,7 +11,6 @@ import TextFormater
import UIKit
#elseif os(OSX)
import Cocoa
typealias UIImage = NSImage
#endif
extension ResourcePackageReader {
@ -218,6 +217,7 @@ extension ResourcePackageReader {
///
///
/// get image from theme
#if os(iOS)
public func getImage(_ key: String) -> UIImage? {
if let _data = self[key],
let img = UIImage(data: _data) {
@ -226,4 +226,14 @@ extension ResourcePackageReader {
return nil
}
}
#elseif os(OSX)
public func getImage(_ key: String) -> NSImage? {
if let _data = self[key],
let img = NSImage(data: _data) {
return img
} else {
return nil
}
}
#endif
}