我把歌曲放到桌子上,当我在详细页面工作中播放歌曲时,当我回到歌曲停止时,我希望在所有视图中继续播放
的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]
}
}
答案 0 :(得分:1)
玩家可能会获得视图发布。您需要使您的播放器可以访问所有详细视图并引用该单个对象。如果您正在寻找一个简单的解决方案,只需创建一个他们都可以参考的单例:
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
}
}
然后从您的详细信息视图中调用
AudioPlayer.sharedInstance.playAudio(url)
请注意,这未经过测试 - 只是显示了基本想法。