1

I've made this code to present all the system sounds in a tableview the problem is I'm getting a nil all the time :| could be cool if u guys could tell me what I've done wrong:

   class CompletetedSoundViewController: UITableViewController {
    
    private var soundList: [String] = []
    private let soundDirectory = "/System/Library/Audio/UISounds"
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "trackCell")
        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: "trackCell", 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)
    }
    
}
   
Avi Sabag
  • 61
  • 8
  • Maybe this helps? https://stackoverflow.com/questions/32036146/how-to-play-a-sound-using-swift – gnahum Jun 24 '20 at 17:12
  • Nope, it only shows how to play a single audio file that u implemented into the project, not the system audio files. – Avi Sabag Jun 24 '20 at 17:40

0 Answers0