Swifty Sound is a simple library that lets you deal with Swift sounds easily.
Sound.play(file: "dog.wav")
Sound.play(url: fileURL)
More advanced example:
Sound.play(file: "dog", fileExtension: "wav", numberOfLoops: 3)
The above will play the sound three times.
Specify a negative number of loops to play the sound continously in an intinite loop:
Sound.play(file: "dog", fileExtension: "wav", numberOfLoops: -1)
Stop currently playing sounds:
Sound.stopAll()
Enable/disable all sounds:
Sound.enabled = true
Sound.enabled = false
The value of Sound.enabled
property will be automatically persisted in UserDefaults
and restored on the next launch of your app.
Change sound categories. SwiftySound provides a simple way of changing sound category:
Sound.category = .ambient
This changes the category of the underlying shared AVAudioSession
instance. The default value is SoundCategory.ambient
. Due to AVAudioSession
architecture, this property is not available on macOS.
You can also create an instance of a Sound class and store it somewhere in your app.
let sound = Sound(url: fileURL)
sound.play()
- Playing single sounds
- Loops
- Infinite loops
- Playing the same sound multiple times simultaneously
- Global static variable to enable/disable all sounds
- Swift 3
- Xcode 8.0 or later
- iOS 8.0 or later
- tvOS 9.0 or later
- OS X 10.10 or later
CocoaPods is a dependency manager which automates and simplifies the process of using 3rd-party libraries in your projects. See the Get Started section for more details.
platform :ios, '8.0'
use_frameworks!
pod 'SwiftySound'
Carthage is a lightweight dependency manager for Swift and Objective-C. It leverages CocoaTouch modules and is less invasive than CocoaPods.
To install with carthage, follow the instruction on Carthage
github "adamcichy/SwiftySound"
Drop the Sound.swift
file into your project, link against AVFoundation.framework
and you are ready to go.
Swifty Sound is licensed under the MIT License.