简体   繁体   English

如何制作音乐继续在所有视图中播放

[英]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 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 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]
    }
}

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. 请注意,这未经测试-只是显示了基本思想。

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

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