gpt4 book ai didi

ios - 如何让音乐在所有 View 中继续播放

转载 作者:行者123 更新时间:2023-11-29 01:19:30 24 4
gpt4 key购买 nike

我将歌曲放入表格中,当我在详细信息页面中播放歌曲时,当我返回歌曲停止时,我希望在所有 View 中继续播放

View Controller

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")
}

表格 View Controller

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

最佳答案

播放器可能随 View 一起释放。您需要让您的播放器可以访问所有详细 View 并引用该单个对象。如果您正在寻找一个简单的解决方案,只需创建一个他们都可以引用的单例:

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

}

}

然后从您的详细信息 View 调用

AudioPlayer.sharedInstance.playAudio(url)

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

关于ios - 如何让音乐在所有 View 中继续播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34775518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com