samedi 31 janvier 2015

Incorrect playing audio files in UITableView - Swift, iOS

I am making an app with the function of playing audio in Swift. All audio files are showed in UITableView, when I click the row an appropriate audio files must be played. The problem is that, for example, when I click the first row it doesn't play the audio, but if I click the second one, the audio of first row is played. Image of tableView: http://ift.tt/1uMx23K The code is below:



func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
var mp3file: AnyObject = mp3sPaths[indexPath.row]
cell.textLabel?.text = mp3file.lastPathComponent
return cell
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
var mp3file = mp3sPaths[indexPath.row] as NSString
var mp3URL: NSURL = NSURL.fileURLWithPath(mp3file)!
var error: NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: mp3URL, error: &error)
audioPlayer?.stop()
audioPlayer?.prepareToPlay()
audioPlayer?.play()
}

Aucun commentaire:

Enregistrer un commentaire