---
title: "#304: Simple Sound Playback with SwiftySound 🔊"
url: https://littlebitesofcocoa.com/304-simple-sound-playback-with-swiftysound
author: Jake Marsh
published: 2017-03-22
topics: ["Libraries"]
description: "Apple ships a ton of great APIs for playing audio in our apps. They offer a wealth of great functionality, but also a fair amount of complexity."
---

# #304: Simple Sound Playback with SwiftySound 🔊

By Jake Marsh, March 22, 2017. HTML: https://littlebitesofcocoa.com/304-simple-sound-playback-with-swiftysound

**Apple** ships [a ton of great APIs](https://littlebitesofcocoa.com/topics/55-audio?v=l) for playing _audio_ in our **apps**.

They offer a wealth of great <i>functionality</i>, but also a fair amount of <i>complexity</i>.

Often we don't need all that power. Perhaps we're adding sound effects or audio feedback to one of our user interfaces, or maybe we just need to loop some background music in the menu of our video game.

Today we'll check out a new library from [Adam Cichy](https://github.com/adamcichy) called [SwiftySound](https://github.com/adamcichy/SwiftySound) that can help us simplify our _audio_ playback code. Let's take a look.

First up, simple playback:

``` swift
Sound.play(file: "unscheduled-offworld-activation.wav")
```

or

``` swift
Sound.play(url: someURL)
```

That's it. Yes, really.

This kind of **`UIImage`**-esque simplicity has always been sort of missing from **UIKit**. Neat to see.

[SwiftySound](https://github.com/adamcichy/SwiftySound) doesn't stop there though, We can easily loop _sounds_:

``` swift
Sound.play(file: "main-menu", fileExtension: "wav", numberOfLoops: -1)
```

We could pass in any `Int` here to loop the music, but we've passed `-1` to indicate we want to loop forever.

One of the most useful features of [SwiftySound](https://github.com/adamcichy/SwiftySound) is the inclusion of a `UserDefaults`-persisted flag for persisting the user's preference of sound playback being enabled or disabled.

We can use this on our **app's Settings screen** to allow users to easily enable/disable sound effects globally. 

``` swift
Sound.enabled = soundsSwitch.on
```

The value is then automatically persisted and respected across app launches. A nice touch by [SwiftySound](https://github.com/adamcichy/SwiftySound)'s author, [Adam Cichy](https://github.com/adamcichy).

We're not required to use this [singleton](https://littlebitesofcocoa.com/4-singletons) `Sound` instance by the way, we can always create a `Sound` instance and pass it around:

``` swift
let sound = Sound(url: soundEffectURL)
```

Then later:

``` swift
sound.play()
```

Last but not least, [SwiftySound](https://github.com/adamcichy/SwiftySound) stands out for being [a single **Swift** file](https://github.com/adamcichy/SwiftySound/blob/master/Classes/Sound.swift).

It's great to see this kind of power <i>and</i> flexibility in such a tiny little **dependency**.

😍 More like this please!

Full documentation for _SwiftySound_ is at [git.io/swiftysound](https://git.io/swiftysound)

<p></p>
