我有一個應用程序,我在一個表格視圖中顯示不同的槍。我在我的tableview單元格中有一個按鈕。當我點擊這些按鈕時,我想要彈奏槍聲。我爲我的音頻文件創建了一個數組。我相信我已經取得了進展,但我無法實現我想要的。感謝您的幫助當你點擊一個tableview單元格中的按鈕時,在連續的數組裏面播放聲音
這裏是我的代碼
import UIKit
import AVFoundation
class ViewController6: UIViewController, UITableViewDataSource, UITableViewDelegate {
//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 soundArrayRifles = ["AK47", "AUGA1", "FAMAS", "G36K", "K1A", "K2", "M4A1", "M4Silencer", "M16", "M60", "MicroGALIL", "QBZ95", "RPK", "ScarHeavy", "Scarlight", "SG552", "TAR21", "Tommy", "Type89", "XM8"]
var audioPlayer: AVAudioPlayer = AVAudioPlayer()
func setupAudioPlayerWithFile(file: NSString, type: NSString) -> AVAudioPlayer? {
let path = Bundle.main.path(forResource: file as String, ofType: type as String)
let url = NSURL.fileURL(withPath: path!)
var audioPlayer : AVAudioPlayer?
do {
try audioPlayer = AVAudioPlayer(contentsOf: url)
} catch {
print("Player not available")
}
return audioPlayer
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//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
}
@IBAction func buttonAction(_ sender: UIButton) {
let range: UInt32 = UInt32(soundArrayRifles.count)
//FIND OUT WHICH SOUND HERE
let sound = self.setupAudioPlayerWithFile(soundArrayRifles, type: "mp3")
sound.play()
}
}
我在這條線得到一個錯誤。它不會讓我加上「soundArrayRifles」點陣這裏
let sound = self.setupAudioPlayerWithFile(soundArrayRifles, type: "mp3")
我想'setupAudioPlayerWithFile'意味着字符串作爲第一個參數,而不是整個數組 –
好吧,你有什麼建議? –