如何在swift

时间:2015-07-23 21:49:57

标签: ios xcode swift directory nsindexpath

我从特定文件的NSIndexPathUITableViewController获取NSIndexPath数组。我从NSIndexPath数组中的文档文件夹中获取mp3文件,然后从NSIndexPath文件中的UITableViewCell中获取所选行中的特定文件。

我想切换mp3文件需要更改UIViewController中的NSIndexPath播放音乐。或者有不同的方法是从文件夹中切换mp3文件吗?

UITableViewController代码

    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
    }
}

UIViewController代码

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

}

代码播放音乐

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

我在控制台中获得了以下结果

index {length = 2,path = 0 - 0}它是当前对象

它是文档文件夹中的所有对象  所有对象[{length = 2,path = 0 - 0},{length = 2,path = 0 - 1}]

1 个答案:

答案 0 :(得分:0)

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