Sayed shoaib
Sayed shoaib

Reputation: 25

how make music Continue play in all views

I put songs into a table, and when i play the song in the detail page work and when i get back the song stop i want Continue play in all views

ViewController

if let audio=NSBundle.mainBundle().pathForResource( navigationItem.title , ofType: "mp3") {
    let url=NSURL (fileURLWithPath: audio)

    do {
        player = try AVAudioPlayer (contentsOfURL: url)
    }

    catch{"erorr"}       

    player.play()            
} else {
    print("erorr")
}

tableViewController

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("indeaa", sender: tableView)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "indeaa" {
        let vc = segue.destinationViewController as UIViewController
        let indexpath:NSIndexPath = tableaa.indexPathForSelectedRow!
        vc.title = arr[indexpath.row]
    }
}

Upvotes: 0

Views: 81

Answers (1)

toast
toast

Reputation: 116

The player is probably getting released with the view. You would need to make your player accessible to all detail views and reference that single object. If you are looking for an easy solution, just create a singleton they can all reference:

class AudioPlayer{

  static let sharedInstance = new AudioPlayer();

  var player : AVAudioPlayer

  func playAudio(url : NSURL){

    //see if player is playing, stop and release, setup new player

  }

}

Then from your detail view just call

AudioPlayer.sharedInstance.playAudio(url)

Note this isn't tested - but just shows the basic idea.

Upvotes: 1

Related Questions