ios - 如何快速播放文档目录中的下一首轨道

标签 ios xcode swift file

我做了一个简单的媒体播放器。我有一个 UITableViewController,其中包含文档目录中的 mp3 文件和播放 mp3 文件的 UIViewController。我将一个 mp3 文件的 NSURLUITableViewController 传递给 UIViewController 并且我可以播放它。我想让按钮变成下一个或上一个轨道。我怎样才能做到?

代码在特定文件上传递 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
    }

代码正在播放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()
}

更新

我做了以下

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

但我总是从 NSArray 得到相同的索引

最佳答案

使用您的 mp3 文件数组,并使用 indexPath 确定 current playing index,在单击按钮时增加或减少 current playing index作为 Next, or, Prev,并根据索引更改 mp3 文件。 重放音乐。

简单。

如果您需要更多帮助,请联系。

更新

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;

}

所以如上所述,只需在您的playerVC中声明两个属性,即

curIndex 作为 int,mp3s 作为 NSArray。

在函数中

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

然后简单地定义你的下一个和上一个索引计算,如

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

希望对您有所帮助。干杯。

关于ios - 如何快速播放文档目录中的下一首轨道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31584337/

相关文章:

xcode - 有没有办法跳转到 Xcode 中的目标/方案选择菜单?

swift - 更新包含数组字典的 plist 中的值时出现问题

ios - 在 UIScrollView 中停止和启动视频

html - 我怎样才能修复我的虚线边框只发生在 ios 而不是 android 上的错误?

ios - 我可以在 Xcode 4.1 中使用 Storyboard吗?

iphone - iOS:可执行文件已使用无效的权利进行签名

iphone - 注册Nib :forReuseidentifier with custom UTTableViewCell and Storyboards

iphone - 我的 Phonegap 项目中没有 PhoneGap.plist

swift - 在子类中初始化属性委托(delegate)

swift - Swift 中未解析的标识符