简体   繁体   English

使用AVFoundation随机播放mp3

[英]Random mp3 play with AVFoundation

    override func viewDidAppear(animated: Bool) {

    let fileLocation = NSBundle.mainBundle().pathForResource(self.navigationItem.title, ofType: "mp3", inDirectory: "Story")!

    try! player = AVAudioPlayer(contentsOfURL: NSURL (string: fileLocation as String)!)

    player.play()

I have a folder with 4 MP3s, and a cell list accessing each one of those 4 MP3s. 我有一个包含4个MP3的文件夹,以及一个访问这4个MP3中的每一个的单元格列表。 Why is it playing the same MP3 again and again for each of those cells? 为什么要为每个单元一次又一次地播放相同的MP3?

It is not even the first one from that folder, it is the last one! 它甚至不是该文件夹中的第一个,它是最后一个!

单元格列表,带有mp3的文件夹!

我想您的导航项目标题是一个常量值。

Is this in a UITableView? 这是在UITableView中吗? If so, try doing it through the didselectviewatindexPath from UITableViewDelegate. 如果是这样,请尝试通过UITableViewDelegate中的didselectviewatindexPath进行操作。 Find the indexPath that is clicked and use that to play the mp3 for each cell. 查找被单击的indexPath并使用它来为每个单元格播放mp3。

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

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