2016-11-13 22 views
0

First, this is how my app looks like打在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 


    } 


} 

回答

0

所有你需要添加動作按鈕並指定一些標記到該按鈕,以確定哪一個是clicked.do一些代碼的第一在cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

cell6.myButton.tag = indexPath.row 
cell6.myButton.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)} 

func buttonAction(sender: UIButton!) 
{ 
print("Button tapped") 
let tapedIndex = sender as! UIButton 
let getUrl = buttonDataRifles[tapedIndex.tag] as! NSURL 
} 
+0

究竟是什麼意思,「分配一些標籤到該按鈕,以確定哪個被點擊」? –

+0

我們必須在這裏指定cell6.myButton.tag = indexPath.row作爲標記,所以只要我們只點擊一行按鈕,所以我們可以識別哪個按鈕單擊我們可以播放該行的歌曲。 – Bucket

+0

let getUrl = soundsArray [tapedIndex] as! NSURL 我在這一行發生錯誤,說'不能用'UIButton'類型的索引來下標'NSURL'類型的值 –

0

如果u'r聽起來不多於30秒,我建議使用AudioToolbox。該框架爲此目的而專門設計了功能。

Objective-C的

  1. 進口框架,爲您的聲音

    #import <AudioToolbox/AudioToolbox.h> 
    
  2. 提及與像

    @property (assign, nonatomic) SystemSoundID soundIDOne; 
    
  3. 創建SystemSoundID準備找你sonds在很begginning

    AudioServicesCreateSystemSoundID ((__bridge CFURLRef)([[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Swish" ofType:@"wav"]]), &_soundIDDone); 
    
  4. 播放聲音

    AudioServicesPlaySystemSound(_soundIDOne); 
    
  5. 如果妳需要完成 - 添加處理器

    AudioServicesAddSystemSoundCompletion(_soundIDOne, NULL, NULL, playSoundFinished, NULL); 
    
    
    void playSoundFinished (SystemSoundID sound, void *data) { 
        //do stuff here 
    } 
    

雨燕2.2

import AudioToolbox 

    private var soundID:SystemSoundID? 

    if let filePath = NSBundle.mainBundle().pathForResource("name", ofType: "type") { 
     let pathToResource = NSURL(fileURLWithPath: filePath) 
     soundID = 0 
     if var soundID = soundID { 
      AudioServicesCreateSystemSoundID(pathToResource, &soundID) 
     } 
    } 

    if var soundID = soundID { 
     AudioServicesPlaySystemSound(soundID) 
    } 

    //or create extension 

    extension SystemSoundID { 
     static func playSound(soundName: String, fileExtension: String) { 
      var soundID: SystemSoundID = 0 
      if let filePath = NSBundle.mainBundle().pathForResource(soundName, ofType: fileExtension) { 
       let pathToResource = NSURL(fileURLWithPath: filePath) 
       AudioServicesCreateSystemSoundID(pathToResource, &soundID) 
       AudioServicesPlaySystemSound(soundID) 
      } 
     } 
    } 

    //and usage 

    SystemSoundID.playSound("soundFileName", fileExtension: "extension") 

更多的外觀here

如果ü要使用AVFoundation代替代碼將是這樣的:

import AVFoundation 

    var audioPlayer = AVAudioPlayer() //create player 

    //create all path to y'r resources 

    var path:[NSURL] = [] 

    if let filePath = NSBundle.mainBundle().pathForResource("soundName", ofType: "fileExtension") { 
     let pathToResource = NSURL(fileURLWithPath: filePath) 
     path.append(pathToResource) 
    } 

    //in the very start 
    do { 
     if let selectedPath = path.first { 
      audioPlayer = try AVAudioPlayer(contentsOfURL: selectedPath) 
      audioPlayer.prepareToPlay() 
     } 
    } catch { 
     print(error) 
    } 

    audioPlayer.play() 

在這種情況下,美國可以,聲音播放,因爲之前得到一些延遲要求prepareToPlay的AVAudioPlayer ,要確保你可以播放聲音,你需要一個更復雜的設置AVPlayer與觀察status播放器,如果你喜歡你可以參考this link。另外,如果你的文件的播放時間很短,這個延遲可能會非常小,甚至無法播放,無論如何,對於我來說這並不是因爲上述原因播放大量聲音文件的首選方式

因爲我無法播放告訴你關於AVFoundationQueuePlayer,這也是很好的解決方案。 Here是蘋果示例鏈接