0

I want to play some songs from a folder that I have added to xcode.

I use tabbed application and the code is like this:

func playThis(thisOne:String)
{
    do
    {
        let audioPath = Bundle.main.path(forResource: thisOne, ofType: ".mp3")
        try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
        audioPlayer.play()
    }
    catch
    {
        print ("ERROR")
    }
}

override func viewDidLoad() {

    super.viewDidLoad()
    label.text = songs[thisSong] //error index out of range
}

But when I run it, the song not appear in first view controller and when I click the second view controller tab app crashes and the reason is:

index out of range

First view controller code is like this:

import UIKit
import AVFoundation

var audioPlayer = AVAudioPlayer()
var songs:[String] = []
var thisSong = 0
var audioStuffed = false

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var myTableView: UITableView!

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return songs.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
    cell.textLabel?.text = songs[indexPath.row]
    return cell
}

and my way to pass the song to detail view controller:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    do
    {
        let audioPath = Bundle.main.path(forResource: songs[indexPath.row], ofType: ".mp3")
        try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
        audioPlayer.play()
        thisSong = indexPath.row
        audioStuffed = true
    }
    catch
    {
        print ("ERROR")
    }
}

second view controller code:

override func viewDidLoad()
{
    super.viewDidLoad()
    gettingSongNames()
}


override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
}


//FUNCTION THAT GETS THE NAME OF THE SONGS
func gettingSongNames()
{
    let folderURL = URL(fileURLWithPath:Bundle.main.resourcePath!)

    do
    {
        let songPath = try FileManager.default.contentsOfDirectory(at: folderURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)

        //loop through the found urls
        for song in songPath
        {
            var mySong = song.absoluteString

            if mySong.contains(".mp3")
            {
                let findString = mySong.components(separatedBy: "/")
                mySong = findString[findString.count-1]
                mySong = mySong.replacingOccurrences(of: "%20", with: " ")
                mySong = mySong.replacingOccurrences(of: ".mp3", with: "")
                songs.append(mySong)
            }

        }

        myTableView.reloadData()
    }
    catch
    {
        print ("ERROR")
    }
}
Francesco Deliro
  • 3,899
  • 2
  • 19
  • 24
Fzee
  • 33
  • 6

1 Answers1

0

Not getting enough conditions from the provided code but if you want to stop the crash handle the exceptions and handle every scenario of the code Example: always check the array.count > 0 before accessing the values. In your case :

if songs.count > 0 { label.text = songs[thisSong] }

Y_Y
  • 331
  • 1
  • 6
  • Inside viewdidload – Y_Y Nov 23 '17 at 07:49
  • i was input it, but when i run... list of song isn't appear and in detail view controller when i click play there nothing changes what... – Fzee Nov 23 '17 at 07:53
  • Did you not mean to write `thisSong < songs.count`? (And related changes to the text of course) – CRD Nov 26 '17 at 20:36