Skip to content

Instantly share code, notes, and snippets.

@Nirma
Last active October 11, 2020 11:57
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nirma/e246ebe9e300accb317fe8b1f639d9a1 to your computer and use it in GitHub Desktop.
Save Nirma/e246ebe9e300accb317fe8b1f639d9a1 to your computer and use it in GitHub Desktop.
Display all available System Sounds for iOS, plays sound when tapped.
//
// SoundListViewController.swift
// SoundSampler
//
// Created by Nicholas Maccharoli on 2016/12/06.
// Copyright © 2016年 Nicholas Maccharoli. All rights reserved.
//
import UIKit
import AudioToolbox
class SoundListViewController: UITableViewController {
private var soundList: [String] = []
private let soundDirectory = "/System/Library/Audio/UISounds"
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseId")
soundList = FileManager.default.enumerator(atPath: soundDirectory)!.map { String(describing: $0) }
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return soundList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseId", for: indexPath)
let soundFileName = soundList[indexPath.row]
let fullyQualifiedName = soundDirectory + "/" + soundFileName
let url = URL(fileURLWithPath: fullyQualifiedName)
var soundId: SystemSoundID = 0
AudioServicesCreateSystemSoundID(url as CFURL, &soundId)
cell.textLabel?.text = "\(soundFileName) \(soundId)"
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let soundFileName = soundList[indexPath.row]
let fullyQualifiedName = soundDirectory + "/" + soundFileName
let url = URL(fileURLWithPath: fullyQualifiedName)
var soundId: SystemSoundID = 0
AudioServicesCreateSystemSoundID(url as CFURL, &soundId)
AudioServicesPlaySystemSound(soundId)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment