0

First, this is how my app looks like

Now I want to play different sounds when I click buttons inside these table view cells. I am very new at programming and I couldn't figure out how to do this. I have created an array for my sound files but I don't know how to assign them to those buttons in a row. Thank you very much for your help

AND THIS IS MY CODE

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


    }


}
Kindle Q
  • 944
  • 2
  • 19
  • 28
Volkan Elçi
  • 163
  • 2
  • 13

2 Answers2

0

First of all you need to add action to button and assign some tag to that button to identify which one is clicked.do some code on 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 
}
Bucket
  • 449
  • 3
  • 20
0

If u'r sounds not more that 30 sec I suggest to use AudioToolbox. This framework has specially designed functionality for such purpose.

Objective-C

  1. import framework

    #import <AudioToolbox/AudioToolbox.h>
    
  2. make reference for your sound with creating SystemSoundID like

    @property (assign, nonatomic) SystemSoundID soundIDOne;
    
  3. Prepare you'r sonds in the very begginning

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

    AudioServicesPlaySystemSound(_soundIDOne);
    
  5. If u need completion - add handler

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

Swift 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")

For more look here

In case u want to use AVFoundation instead code will be like:

    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()

In this case, u can get some delay before sound playing because of required prepareToPlay for AVAudioPlayer, to be sure that u can play sound u need a little bit more complex setup of AVPlayer with observation for status of player, if u like u can refer to this link. Also, if u have files with short duration this delay can be very small or even absent, anyway, as for me this is not preferred way to play a lot of sound files due to reason described above

And of cause I cant tell u about AVFoundationQueuePlayer, this is also good solution. Here is Apple Sample link

hbk
  • 10,908
  • 11
  • 91
  • 124