SoundManager
提供了一种在 App 中播放声音的简单方法。
如果您觉得 SoundManager
有用,并希望帮助支持其持续开发和维护,请考虑进行小额捐赠,尤其是在您将其用于商业产品中的情况下。
通过像您这样的贡献者的支持,我才能继续免费构建、发布和维护高质量、文档完善的 Swift 包,例如 SoundManager
。
Swift Package Manager (Xcode 11 及以上版本)
https://github.com/Appracatappra/SoundManager.git
。为什么不使用 CocoaPods 或 Carthage 等?
支持多个依赖管理器会使维护库的工作量呈指数级增长,并耗费更多时间。
由于 Swift Package Manager 已与 Xcode 11(及更高版本)集成,因此这是继续支持的最简单选择。
SoundManager
提供了一种在 App 中不同频道播放声音的简单方法,并且为背景音乐和背景音效设置不同的频道。
SoundManager
具有三个内置的用户偏好属性来控制声音。
/// Global variable that if `true`, background music will be played in the game.
@AppStorage("playBackgroundMusic") public var shouldPlayBackgroundMusic: Bool = true
/// Global variable that if `true`, background sounds will be played in the game.
@AppStorage("playBackgroundSounds") public var shouldPlayBackgroundSounds: Bool = true
/// Global variable that if `true`, sound effect will be played in the game.
@AppStorage("playSoundEffects") public var shouldPlaySoundEffects: Bool = true
您可以在 App 中提供一个 UI,让最终用户控制他们的声音偏好设置。
SoundManager
提供了几个功能,可以轻松地在不同的频道上播放音乐或声音。 最常用的有:
public func startBackgroundMusic(song:String)
开始循环播放给定的声音作为背景音乐。public func playBackgroundSound(sound:String)
开始循环播放给定的声音作为背景音效。public func stopBackgroundMusic()
停止当前播放的背景音乐。public func stopBackgroundSound()
停止当前播放的背景音效。public func playSoundEffect(sound:String, channel:SoundEffectChannel = .channel01, didFinishPlaying:FinishedPlaying? = nil)
在给定频道上播放给定的音效。 如果提供了回调处理程序,则会在声音停止播放时通知您。public func stopSoundEffect(channel:SoundEffectChannel = .channel01)
停止在给定频道上播放的音效。注意: 在 App 的 Bundle 中包含任何 音乐 或 声音 文件。 在
SoundManager
中调用方法时,请同时包含声音文件的扩展名,例如:MySound.mp3
。 另请注意,声音区分大小写。
该 软件包 包括所有功能的完整 DocC 文档。