swift - 背景音乐,我的音乐相互重叠,我想让音乐循环播放
问题描述
导入 UIKit 导入 AVFoundation
类 BackgroundViewController: UIViewController { var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
playBackgroundStartMusic()
}
override func viewWillAppear(_ animated: Bool) {
audioPlayer.stop()
}
//MARK: - Background Music
func playBackgroundStartMusic() {
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "IntroMusic", ofType: "wav")!))
audioPlayer.prepareToPlay()
audioPlayer.play()
audioPlayer.numberOfLoops = -1
}
catch {
print(error)
}
}
}
解决方案
如果您遇到音乐覆盖视图更改的问题,只需将 AVFoundation 中的 audioPlayer.stop() 放在显示下一个屏幕的按钮上,这样背景音乐就会停止并且不再播放。
推荐阅读
- docker - 如何在生产中处理更新基于 docker-compose 的应用程序
- javascript - 将 onclick 事件传递给没有全局函数的模板文字
- swift - 在真实设备 iOS Swift 上进行单元测试时尝试生成随机密钥对时出错
- karate - 使用 Gradle 的不同空手道报告
- java - 未转换为系统属性的环境变量
- java - 如何使用 Material Design 更改操作栏文本颜色?
- windows - 如何在 Windows CMD 中重命名文件(命令提示符)
- php - 如何以原始函数树枝的形式在我的 var 中取消转义字符串 html?
- java - RecyclerView 在活动中总是空白
- powershell - 如何将 powershell 中文件的 SHA256 哈希值与已知值进行比较?