私はテーブルビューで別のガンを表示するアプリを持っています。私はテーブルビューのセルにボタンを持っています。私はそれらのボタンをタップすると、銃の音を再生したい。オーディオファイルの配列を作成しました。そして私は進歩を遂げたと信じていますが、私は自分が望むものを達成できませんでした。ここにあなたの助けをテーブルビューのセルでボタンをタップすると、配列内のサウンドを再生する
をありがとう、私は、この行でエラーを取得する私のコード
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()
}
}
です。それは私はあなたがそれへの参照を持っているので、ボタンタグであなたのindexPath.rowを保存しようとここに
let sound = self.setupAudioPlayerWithFile(soundArrayRifles, type: "mp3")
私は 'setupAudioPlayerWithFile'は配列全体ではなく最初の引数として文字列を取ることを意味すると思います –
あなたは何か提案がありますか? –