r/iOSDevelopment • u/specialist-lab-246 • Jul 23 '22
I made a simple music app for toddlers road trip entertainment
For my 2yo I wrote this tiny app, because we're going on a road trip and entertainment will be key. We found an app that shows a bunch of tiles and when you press a tile it starts playing music, but it malfunctions and crashes constantly. So, "how hard can it be?" I wrote a dead-simple app myself. Each tile corresponds to a song, when you press it the tile will be highlighted and the song will play on repeat. When pressed again it stops. That's all.
Here's a screenshot and here's the code. Let me know if you use it or if you run into any issues compiling and running it.

I use Xcode 13.4.1 and run this on an iPad Air and an iPad Pro gen2. Here are some instructions, as complete as I manage from the top of my head:
create an iOS App project
put this code in ContentView.swift
add mp3's to the project (right next to the swift files and assets)
in the Assets add the cover pictures (you can also add an app icon there)
register as a developer (it's free) and have your project signed by clicking the project (topmost entry in the left sidebar) -> "Signing & Capabilities" tab -> "Team"
instead of a simulator, select your iOS device (after connecting it to your computer with a cable) and compile/run your project
done!
import SwiftUI
import AVFoundation
var audioPlayer: AVAudioPlayer!
struct Song: Hashable {
let index: Int
let imageName: String
let trackName: String
}
struct ContentView: View {
@State private var playingSong:Int = 0
let songs : [Song] = [
Song(index: 1, imageName: "cover1", trackName: "song1"),
Song(index: 2, imageName: "cover2", trackName: "song2"),
Song(index: 3, imageName: "cover3", trackName: "song3")
]
private let adaptiveColumns = [
GridItem(.adaptive(minimum: 175, maximum: 175)) // ipad air: 160, ipad pro: 175 (24 songs)
]
var body: some View {
//ScrollView { // uncomment this line and add a } below to get a scrollbar
LazyVGrid(columns: adaptiveColumns, spacing: 20) {
ForEach(songs, id: \.self) { song in
Button(action: {
do {
print("currently playing song: \(playingSong)")
let soundFileURL = Bundle.main.url(forResource: song.trackName, withExtension: "mp3")
audioPlayer = try AVAudioPlayer(contentsOf: soundFileURL!)
guard let player = audioPlayer else { return }
if playingSong == song.index {
player.pause()
playingSong = 0
}
else {
player.numberOfLoops = -1
player.play(atTime: player.deviceCurrentTime + 1.1)
print("playing: " + song.trackName)
playingSong = song.index
}
}
catch { print("error occurred") }
}) {
ZStack {
let current = song.index == playingSong
let glowRadius = !current ? 0.0 : 12.0
Image(song.imageName)
.resizable()
.scaledToFit()
.shadow(color: .red, radius: glowRadius)
.shadow(color: .red, radius: glowRadius)
}
}
}
}
}
}Acknowledgements:
the images in the screenshot are from flaticon.com
the first lines of code are based on this music app tutorial by iOS Academy
the grid view is as explained by Code Palace in this tutorial
