Search code examples
iosxcodeswiftdirectorynsindexpath

How to change NSIndexPath in another UIViewController in swift


I get NSIndexPath array from UITableViewController and NSIndexPath of a specific file. I get mp3 files from the document folder in NSIndexPath array and I get a specific file from the selected row from UITableViewCell in a NSIndexPath file.

I want to switch mp3 files for it need change NSIndexPath in UIViewController is which plays music. Or is there different methods are which switching mp3 files from document folder?

The UITableViewController code

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicViewController
    var indexPath = tableView.indexPathForSelectedRow()
    var nameOfObjectForPass = listOfMP3Files![indexPath!.row] // default it's name and
    var fileManager = NSFileManager.defaultManager()

    var wayToFile = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
    var passMusicFileURL: NSURL? // for pass mp3

    if let documentPath: NSURL = wayToFile.first as? NSURL {
        let musicFile = documentPath.URLByAppendingPathComponent(nameOfObjectForPass)
        println(musicFile)
        passMusicFileURL = musicFile
    }

    var currentTrackIndex = tableView.indexPathForSelectedRow()!

    var allIndexTable = tableView.indexPathsForVisibleRows()! as! [NSIndexPath]

    if segue.identifier == "listenMusic" {
        playerVC.nameMusicFile = nameOfObjectForPass // name
        playerVC.mp3URL = passMusicFileURL

        // test 

        playerVC.currentIndex = currentTrackIndex

        playerVC.allIndex = allIndexTable
    }
}

The UIViewController code

var currentIndex: NSIndexPath!
var allIndex = [AnyObject]() as! [NSIndexPath]

func playNextSound() {
    println("index \(currentIndex)")

    println("all object \(allIndex)")
    var indextFound = find(allIndex, currentIndex)        
    println("index found \(indextFound)")

}

The code is which plays music

 func playMusic() {
    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil) // == true
    AVAudioSession.sharedInstance().setActive(true, error: nil)
    var error: NSError?
    audioPlayer = AVAudioPlayer(contentsOfURL: mp3URL, error: &error)
   // audioPlayer.prepareToPlay()
    if currentPause == nil {
    } else {
        audioPlayer.currentTime = currentPause
    }
    //
    audioPlayer.volume = 0.5
    audioPlayer.play()
}

I get following results in the console

index {length = 2, path = 0 - 0} it is current object

it is all object from document folder all object [ {length = 2, path = 0 - 0}, {length = 2, path = 0 - 1}]


Solution

  • var newURL: NSURL!
    @IBAction func nextTrack(sender: UIButton) {
        println("touched next")
        var nowRow: Int = self.currentRow
        var maxCount = self.arrayOfMP3.count-1 // -1
        if nowRow < maxCount {
            nowRow = self.currentRow++
            println("plus")
        } else if nowRow == arrayOfMP3.endIndex {
            nowRow = maxCount
            println("equals")
        }
        // for play/pause button setting 
        currentPause = nil
        //println("current pause\(currentPause)")
        next = false
        //
    
        println("current row \(currentRow)")
        println("end index \(arrayOfMP3.endIndex)")
    
        var nextTrack = arrayOfMP3![nowRow]
    
        var nextDirectory = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
    
        if var nextURL: NSURL = nextDirectory.first as? NSURL {
            newURL = nextURL.URLByAppendingPathComponent(nextTrack)
        }
    
        var nameArtist: String!
        var nameSong: String!
        var imageString: NSData!
    
        var itemPlayer = AVPlayerItem(URL: newURL)
    
        var listMetaData = itemPlayer.asset.commonMetadata as! [AVMetadataItem]
        for item in listMetaData {
            if item.commonKey == "title" {
                nameSong = item.stringValue
            }
            if item.commonKey == "artist" {
                nameArtist = item.stringValue
            }
            if item.commonKey == "album" {
                item.stringValue
            }
            if item.commonKey == "artwork" {
                imageString = item.dataValue
            }
        }
    
        nameLabel?.text = "\(nameArtist) \(nameSong)"
        if imageString != nil {
            imageView?.image = UIImage(data: imageString)
        } else {
            imageView?.image = nil
        }
        tapButtonPlay = false
        playMusic()
    }