2016-11-14 30 views
0

我有一個應用程序,我在一個表格視圖中顯示不同的槍。我在我的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") 
+0

我想'setupAudioPlayerWithFile'意味着字符串作爲第一個參數,而不是整個數組 –

+0

好吧,你有什麼建議? –

回答

0

嘗試保存按鈕標記您的indexPath.row讓你不得不對它的引用。

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) 
    cell6.myButton.tag = indexPath.row 
    return cell6 
} 

不是在您的按鈕動作做這樣的事情:

@IBAction func buttonAction(_ sender: UIButton) { 
    let soundIndex = sender.tag 
    //FIND OUT WHICH SOUND HERE 
    let sound = self.setupAudioPlayerWithFile(soundArrayRifles[soundIndex], type: "mp3") 
    sound.play() 

} 

這不是測試,但它應該做的,你需要什麼。

+0

謝謝你的回答。沒有錯誤了。但是當我構建應用程序時,它會打開,但是當我點擊一個按鈕時它會崩潰。這條線有問題'let url = NSURL.fileURL(withPath:path!)' –

+0

這意味着你的路徑不好。考慮索引= 0,你的聲音路徑應該是「AK47.mp3」。如果該路徑無效,您應該得到nil作爲響應,並且當您調用NSURL.fileURL(withPath:path!)時 - 注意! - 打開它沒有崩潰的東西。 –

+0

我很抱歉,但我不太明白你的意思 –

0
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) 
    cell6.myButton.tag = indexPath.row 
    cell6.myButton.addTarget(self, action: #selector(ViewController6.buttonAction(sender:)), for: .touchUpInside) 
    return cell6 
    } 

    func buttonAction(_ sender: UIButton) { 

      let sound = self.setupAudioPlayerWithFile(soundArrayRifles[sender.tag], type: "mp3") 
      sound.play() 

    } 
+0

當我點擊一個按鈕時,該應用程序崩潰:( –

+0

確保你的單元格中的按鈕沒有鏈接到任何'IBAction' –

+0

我認爲你直接在ViewController6中添加按鈕,你應該爲自定義行添加自定義類。添加到按鈕您的自定義類 – aahmetbas

相關問題