“This is the 21st day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”
π author’s home page: Hai Yongπ Author profile: πCSDN full stack quality creator, π₯HDZ core group member π Fan benefits: fans send six books every week and various small gifts from time to time
An overview of the
SwiftySound is a simple library that lets you handle Swift sounds with ease.
A static method
Sound.play(file: "dog.wav")
Copy the code
Sound.play(url: fileURL)
Copy the code
More advanced examples:
Sound.play(file: "dog", fileExtension: "wav", numberOfLoops: 2)
Copy the code
The sound will be played three times.
Specifies a negative loop to play sound continuously in an infinite loop:
Sound.play(file: "dog", fileExtension: "wav", numberOfLoops: -1)
Copy the code
Stop the current sound:
Sound.stopAll()
Copy the code
Enable/disable all sounds:
Sound.enabled = true
Sound.enabled = false
Copy the code
The value of the sound. enabled property will be automatically retained and restored in UserDefaults the next time your application starts.
Change the sound category. SwiftySound provides an easy way to change the sound category:
Sound.category = .ambient
Copy the code
This changes the category of the underlying shared AVAudioSession instance. The default value is soundcategory.ambient. This property is not available on macOS due to the AVAudioSession architecture.
createSoundInstances of the class
You can also create an instance of the Sound class and store it somewhere in your application.
let mySound = Sound(url: fileURL)
mySound.play()
Copy the code
Creating an instance has additional benefits, such as the ability to adjust volume and play callbacks.
Change the volume
You can change the volume of each Sound instance.
mySound.volume = 0.5
Copy the code
The value of the volume property should be between 0.0 and 1.0, with 1.0 being the maximum.
The callback
You can pass the callback to the play method. It plays after the sound has finished playing. For loop sounds, a callback is invoked after the last loop is played.
mySound.play { completed in
print("completed: (completed)")}Copy the code
If the sound stops, breaks, or plays incorrectly, the callback is not invoked.
The characteristics of
- Play a single sound
- cycle
- An infinite loop
- Play the same sound several times at once
- Stop all sounds using the global static method
- Ability to pause and resume
- Adjust the volume
- The callback
- Enable/disable global static variables for all sounds
requirements
- Swift 5
- Xcode 10.2 or later
- IOS 8.0 or later
- TvOS 9.0 or later
- MacOS 10.9 or later
Please use SwiftySound version 0.7.0 for Xcode 8 and Swift 3 support. For Xcode 9 and Swift 4 support, use SwiftySound version 1.0.0.
The installation
Install using CocoaPods
CocoaPods is a dependency manager that automates and simplifies the process of using third-party libraries in your projects.
Podfile
platform :ios, '8.0'
use_frameworks!
pod 'SwiftySound'
Copy the code
Install using Carthage
Carthage is a lightweight dependency manager for Swift and Objective-C. It leverages the CocoaTouch module and is less invasive than CocoaPods.
To install using Carthage, follow the instructions on Carthage
Cartfile
github "adamcichy/SwiftySound"
Copy the code
Install using Swift Package manager
Swift Package Manager is a tool for managing Swift code distribution. Simply add the URL for this repo to your package. swift file as a dependency:
import PackageDescription
let package = Package(
name: "YourPackage",
dependencies: [
.Package(url: "https://github.com/adamcichy/SwiftySound.git",
majorVersion: 0)])Copy the code
Then run Swift Build and wait for SPM to install SwiftySound.
Manual installation
Put the sound. swift file into your project, link to avfoundation.framework, and you’re ready to go.
license
SwiftySound is licensed under the MIT License.
GitHub
Github.com/adamcichy/S…
Write it at the end
The author is determined to build a fishing site with 100 small games, update progress: 41/100
I’ve been writing tech blogs for a long time, mostly through Nuggets, and this is my post about a simple library that lets you handle Swift sounds with ease. I like to share technology and happiness through articles. You can visit my blog at juejin.cn/user/204034… For more information. Hope you like it! π
π welcomes your comments and suggestions in the comments section! π