Воспроизведение различных звуков в TableView
Теперь я хочу, чтобы играть различные звуки при нажатии кнопки внутри этих ячеек таблицы просмотра. Я очень новичок в программировании, и я не мог понять, как это сделать. Я создал массив для своих звуковых файлов, но я не знаю, как назначить их этим кнопкам подряд. Большое спасибо за вашу помощь
И ЭТО МОЙ КОД
import UIKit
import AVFoundation
class ViewController6: UIViewController, UITableViewDataSource, UITableViewDelegate {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Array of Rifles
var arrayOfRifles = ["AK47", "AUGA1", "FAMAS", "G36K", "K1A", "K2", "M4A1", "M4Silencer", "M16", "M60", "MicroGALIL", "QBZ95", "RPK", "ScarHeavy", "Scarlight", "SG552", "TAR21", "Tommy", "Type89", "XM8"]
var buttonDataRifles = ["AK47", "AUGA1", "FAMAS", "G36K", "K1A", "K2", "M4A1", "M4Silencer", "M16", "M60", "MicroGALIL", "QBZ95", "RPK", "ScarHeavy", "Scarlight", "SG552", "TAR21", "Tommy", "Type89", "XM8"]
var soundsArray: [NSURL] = [
NSURL(fileURLWithPath: Bundle.main.path(forResource: "AK47", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "AUGA1", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "FAMAS", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "G36K", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "K1A", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "K2", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "M4A1", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "M4Silencer", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "M16", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "M60", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "MicroGALIL", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "QBZ95", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "RPK", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "ScarHeavy", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "ScarLight", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "SG552", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "TAR21", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "Tommy", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "Type89", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "XM8", ofType: "mp3")!)
]
//Functions for tableView
//Cell - For Rifles
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (arrayOfRifles.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell6 = tableView.dequeueReusableCell(withIdentifier: "cell6", for: indexPath) as! ViewControllerTableViewCell6
cell6.myImage.image = UIImage(named: arrayOfRifles[indexPath.row] + ".jpg")
cell6.myButton.setTitle(buttonDataRifles[indexPath.row], for: UIControlState.normal)
return (cell6)
cell6.myButton.tag = indexPath.row
cell6.myButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)}
@IBAction func buttonAction(_ sender: UIButton) {
print("Button tapped")
let tapedIndex = sender as UIButton
let getUrl = soundsArray[tapedIndex] as! NSURL
}
}
Что именно вы имеете в виду под «назначить какой-то тэг этой кнопке, чтобы определить, на какой щелчок»? –
Мы должны назначить здесь cell6.myButton.tag = indexPath.row как тег, поэтому всякий раз, когда мы нажимаем только на строку кнопки, мы можем определить, на какую кнопку щелкнуть по основанию, мы можем воспроизводить песню этой строки. – Bucket
let getUrl = soundsArray [tapedIndex] as! NSURL Я получил ошибку в этой строке, в которой говорится: «Нельзя индексировать значение типа« NSURL »с индексом типа« UIButton » –