简体   繁体   中英

Swift: Array of audio files

Using Swift 2 how would I create an array of audio files to be used on a collection view?

There are 4 options "Ocean", "Birds", "Nature", "Waves" and they are displayed as cells.

When a cell is clicked, I need it to play the sound. Can you please help?

    var imageArray = [UIImage(named: "ocean"),UIImage(named: "birds"),UIImage(named: "nature"),UIImage(named: "wave")]
var nameArray = ["Ocean","Birds","Nature","Waves"]



func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.imageArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! homeCollectionViewCell

    cell.iconImage?.image = imageArray[indexPath.item!]
    cell.label.text = nameArray[indexPath.item!]

    return cell
}

Do you know how to play the sound? Have you done it yet? If so, you need to use the following function and do something like this:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        //grab the sound from the array
        let sound = your_array_with_sounds[indexPath.row]

        //if you want to play the sound in this view:
           <code to play the sound>          
        //if you want to play the sound in another view:
           <perform the segue and pass the sound to the view you want>
    }

I hope this help. If not, add a bit more of information to your question and we will help you.

You need to use the AVAudioPlayer Class for Play the Audio For that you need to import the below class

import AVFoundation

you need to create the function with Following code

let audioName:String!


func playAudio(){
    let url = NSURL.fileURLWithPath(
                NSBundle.mainBundle().pathForResource(audioName, 
                ofType: "mp3")!)

        var error: NSError?

        audioPlayer = AVAudioPlayer(contentsOfURL: url, error: &error)

        if let err = error {
            println("audioPlayer error \(err.localizedDescription)")
        } else {
            audioPlayer?.delegate = self
            audioPlayer?.prepareToPlay()
        }
      if let player = audioPlayer {
               player.play()
       }
}

in collection View

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
{
          audioName = arrayName[indexPath.row]
}

Swift 4.0

let audioName:String!
func playAudio(){
    let url = NSURL.fileURL(
        withPath: Bundle.main.path(forResource: audioName,
                                              ofType: "mp3")!)
    var error: NSError?

    let audioPlayer = try? AVAudioPlayer(contentsOf: url)
    audioPlayer?.delegate = self
    audioPlayer?.prepareToPlay()

    if let player = audioPlayer {
        player.play()
    }
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
{
          audioName = arrayName[indexPath.row]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM