Skip to content

Commit

Permalink
Added the source code, exampe app, podspec and README.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcichy committed Feb 22, 2017
1 parent 5fb3df2 commit 5f3e117
Show file tree
Hide file tree
Showing 34 changed files with 1,942 additions and 0 deletions.
133 changes: 133 additions & 0 deletions Classes/Sound.swift
@@ -0,0 +1,133 @@
//
// Sound.swift
// SwiftySound
//
// Created by Adam Cichy on 21/02/17.
//
// Copyright (c) 2017 Adam Cichy
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Foundation
import AVFoundation

open class Sound {

public static var playersPerSound: Int = 5 {
didSet {
Sound.stopAll()
Sound.sounds.removeAll()
}
}

private static var sounds = [URL: Sound]()

private static let defaultsKey = "com.moonlightapps.SwiftySound.enabled"

public static var enabled: Bool = {
return !UserDefaults.standard.bool(forKey: defaultsKey)
}() { didSet {
let value = !enabled
UserDefaults.standard.set(value, forKey: defaultsKey)
if value {
stopAll()
}
}
}

private let players: [AVAudioPlayer]

private var counter = 0

// MARK: - Initialization
public init?(url: URL) {
var myPlayers: [AVAudioPlayer] = []
for _ in 0..<Sound.playersPerSound {
if let player = try? AVAudioPlayer(contentsOf: url) {
myPlayers.append(player)
}
}
if myPlayers.count == 0 {
return nil
}
players = myPlayers
Sound.sounds[url] = self
}

// MARK: - Main play method
@discardableResult public func play(numberOfLoops: Int = 0) -> Bool {
if !Sound.enabled {
return false
}
let player = players[counter]
counter = (counter + 1) % players.count
player.numberOfLoops = numberOfLoops
return player.play()
}

// MARK: - Stop playing
public func stop() {
for player in players {
player.stop()
}
}

// MARK: - Convenience static methods
@discardableResult public static func play(file: String, fileExtension: String? = nil, numberOfLoops: Int = 0) -> Bool {
if let url = url(for: file, fileExtension: fileExtension) {
return play(url: url, numberOfLoops: numberOfLoops)
}
return false
}

@discardableResult public static func play(url: URL, numberOfLoops: Int = 0) -> Bool {
if !Sound.enabled {
return false
}
var sound = sounds[url]
if sound == nil {
sound = Sound(url: url)
}
return sound?.play(numberOfLoops: numberOfLoops) ?? false
}

public static func stop(for url: URL) {
let sound = sounds[url]
sound?.stop()
}

public static func stop(file: String, fileExtension: String? = nil) {
if let url = url(for: file, fileExtension: fileExtension) {
let sound = sounds[url]
sound?.stop()
}
}

public static func stopAll() {
for sound in sounds.values {
sound.stop()
}
}

// MARK: - Private helper method
private static func url(for file: String, fileExtension: String? = nil) -> URL? {
return Bundle.main.url(forResource: file, withExtension: fileExtension)
}

}
10 changes: 10 additions & 0 deletions Example/Podfile
@@ -0,0 +1,10 @@
platform :ios, '8.0'

target 'SwiftySoundExample' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!

# Pods for SwiftySoundExample
pod 'SwiftySound', :path => '..'

end
16 changes: 16 additions & 0 deletions Example/Podfile.lock
@@ -0,0 +1,16 @@
PODS:
- SwiftySound (0.1.0)

DEPENDENCIES:
- SwiftySound (from `..`)

EXTERNAL SOURCES:
SwiftySound:
:path: ..

SPEC CHECKSUMS:
SwiftySound: 4444fba6cf5a1042d48ed002f49ecc643a462539

PODFILE CHECKSUM: 89161fbfac7d858e95250684d3810ab14d4c94c7

COCOAPODS: 1.2.0
25 changes: 25 additions & 0 deletions Example/Pods/Local Podspecs/SwiftySound.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5f3e117

Please sign in to comment.