简体   繁体   English

如何快速从文档目录播放下一首曲目

[英]How to play next track from Document Directory in swift

I make a simple media player. 我做一个简单的媒体播放器。 I have a UITableViewController with mp3 files from Document Directory and UIViewController which plays mp3 files. 我有一个UITableViewController其中包含来自文档目录的mp3文件和播放mp3文件的UIViewController I pass NSURL of a mp3 file from UITableViewController to UIViewController and I can play it. 我将mp3文件的NSURLUITableViewController传递给UIViewController然后可以播放它。 I want to make buttons are which will be turn track to next or previous. 我要制作的按钮会转到下一个或上一个。 How can I make it? 我该怎么做?

The code is passing NSURL on a specific file. 该代码正在特定文件上传递NSURL。

    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
    }
    if segue.identifier == "listenMusic" {
        playerVC.nameMusicFile = nameOfObjectForPass // name
        playerVC.mp3URL = passMusicFileURL

        // test 
        playerVC.allUrlsVC = allURLS
    }

The code is playing mp3 file 代码正在播放mp3文件

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

UPDATE 更新

I made following 我做了以下

  var arrayMP3url: Array<AnyObject>!

    func playNextSound() {
        var queue = AVQueuePlayer(URL: mp3URL)
        var current = queue.currentItem

        var arrayForSearch = arrayMP3url as! [NSURL]

        var arrNS = arrayForSearch as NSArray

        var index = arrNS.indexOfObject(mp3URL!)
        println("index \(index)")

        println("array for search \(arrayForSearch)")

        println("current song \(current)")
    }

But I always get the same index from NSArray 但是我总是从NSArray获得相同的索引

Use your array of mp3 files, and determine current playing index using indexPath , increase or, decrease the current playing index on button click as Next, or, Prev , and change the mp3 file by the index. 使用您的mp3文件数组,并使用indexPath确定current playing index ,按Next, or, Prev增大或减小按钮上的current playing index ,并按current playing index更改mp3文件。 Replay the music. 重播音乐。

Simple. 简单。

Ping if you need more help. 如果需要更多帮助,请Ping。

Update 更新资料

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicViewController

    var indexPath = tableView.indexPathForSelectedRow();

    //We only need to pass current index, and array of mp3 urls

    playerVC.curIndex=indexPath.row;
    playerVC.mp3s= listOfMP3Files;

}

So as described above, just declare two properties in your playerVC , ie 因此,如上所述,只需在playerVC声明两个属性,即

curIndex as int and mp3s as NSArray. curIndex int和mp3s为NSArray的。

In the function 在功能上

func playMusic() {

    var nameOfObjectForPass = mp3s![curIndex]; // 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
    }

    self.nameMusicFile = nameOfObjectForPass // name
    self.mp3URL = passMusicFileURL

        // test 
    self.allUrlsVC = allURLS

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

Then simply define your next and previous index calculations like 然后只需定义您的下一个和上一个索引计算,例如

func playNextSound(){
   self.curIndex++;
   var maxCount=self.mp3s.count-1;
   if(self.curIndex>maxCount){
         self.curIndex=maxCount;
   }

   playMusic();
}

func playPrevSound(){
   self.curIndex--;
   if(self.curIndex<0){
         self.curIndex=0;
   }

   playMusic();
}

Hope it helps. 希望能帮助到你。 Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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