تفاصيل العمل

import UIKit

import AVFoundation

class ViewController: UIViewController {

var player: AVAudioPlayer?

override func viewDidLoad() {

super.viewDidLoad()

}

@IBAction func keyPressed(_ sender: UIButton) {

guard let soundName = sender.currentTitle else {

print("Error: Button title is nil.")

return

}

playSound(soundName: soundName)

}

func playSound(soundName: String) {

guard let url = Bundle.main.url(forResource: soundName, withExtension: "wav") else {

print("Error: Could not find sound file named \(soundName).wav")

return

}

do {

player = try AVAudioPlayer(contentsOf: url)

player?.play()

} catch {

print("Error: Could not play sound - \(error.localizedDescription)")

}

}

}

ملفات مرفقة

بطاقة العمل

اسم المستقل
عدد الإعجابات
0
عدد المشاهدات
46
تاريخ الإضافة
المهارات