2016-11-13 41 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是苹果示例链接