Swiftmas – Day 07 – Soundtrack

Posted by

·

What’s your favourite Christmas song?

For the Seventh prompt, we have Soundtrack. What will you create?

I’m looking forward to seeing what you come up with today.

Bring holiday cheer to your fingertips with a Festive Soundboard App! In this tutorial, I’ll walk you through creating a colorful soundboard app where users can tap buttons to play festive sounds like “Jingle Bells,” “Santa’s Ho Ho Ho,” and more. This project is beginner-friendly and uses SwiftUI’s declarative syntax. Now we are halfway through the challenge, I am starting to up the code a little though.

Getting Started


1. Set Up Your Project

You can find the sample project in the GitHub repository here

Create a new SwiftUI project in Xcode:

  1. Open Xcode and select Create a new project.
  2. Choose the App template, click Next.
  3. Enter your project name (e.g., “FestiveSoundboard”), set the interface to SwiftUI, and click Create.

What You’ll Learn

  1. Building a responsive grid layout for buttons.
  2. Using AVAudioPlayer to play sound files.
  3. Styling buttons with vibrant colors and shadows.
  4. Adding a custom background for a festive feel.

Resources

You can find the sample project in the GitHub repository here

Below are all the links to each resource I used. Feel free to mix it up though!

Jingle Bells SoundSleigh Bells SoundSanta SoundFireplace SoundBrass BandElf SoundBackground Image


Step 1: Setting Up the Soundboard Layout

The core of the app is a grid of buttons, where each button corresponds to a festive sound. Start by defining the layout using SwiftUI’s LazyVGrid.

Code Snippet: Soundboard Layout

Explanation:

  1. Sound Data: Each sound has a name, a color, and a file name stored in the sounds array.
  2. Grid Layout: A LazyVGrid arranges buttons in a 2-column grid.
  3. Button Design:
    • Background Colors: Each button is styled with a unique color from the sounds array.
    • Shadows and Rounded Corners: Add depth and style with cornerRadius and shadow.

Step 2: Playing Sounds

Now that the buttons are in place, let’s add functionality to play the sounds when a button is tapped. This is done using the AVAudioPlayer class. I should note, if you are new to adding sound files into your application, simply drag and drop them into your Xcode project along the left (file navigator), when prompted, make sure your app is ticked as the target and continue on.

Code Snippet: Sound Playback

Explanation:

  1. File Loading: The playSound(named:) function attempts to load the sound file with .mp3.wav, or .aiff extensions. Depending on your files you can change or remove as needed for your file extensions.
  2. Audio Playback: If the file is found, it uses AVAudioPlayer to play the sound.

Step 3: Adding a Festive Background

A soundboard app should look as festive as it sounds! Let’s add a custom background image with a blur effect and overlay.

Code Snippet: Background Styling

Explanation:

  • Image: The background uses an image named bg. Ensure this image is added to your asset catalog.
  • Blur Effect: A subtle blur (blur(radius: 3)) softens the background.
  • Overlay: A semi-transparent black overlay darkens the background for better text contrast.

Step 4: Final Touches

Test your app to ensure:

  1. All sound files are loaded correctly and play when tapped.
  2. Buttons are responsive and visually appealing.

What’s Next?

Expand your Festive Soundboard with these ideas:

  1. Add More Sounds: Include more festive audio like carol singers, snowstorms, or jingling bells.
  2. Volume Control: Add a slider to let users adjust the playback volume.
  3. Dynamic Colors: Randomize button colors for a fresh look every time the app is launched.
  4. Interactive Animations: Add button press animations for visual feedback.

🎉 Congratulations! You’ve built a Festive Soundboard App in SwiftUI. Share your app with family and friends and spread the holiday cheer! 🎄


Discover more from

Subscribe to get the latest posts sent to your email.

thecodingsprite avatar

About the author

Hi! My name is Billie, my friends call me Billie Boo. I am a self taught iOS developer with a background in computer science, animation, graphic design & web design. I love sharing my knowledge & projects with the world & that is my mission for this blog. It’s never too late or too hard to follow your dreams.