问题描述
我有一个定义为 Music.swift 的类,编码如下:
var jqmodalshow = jQuery.noConflict();
jqmodalshow(document).ready(function(){
jqmodalshow("#myModalDialog").modal('show');
jqmodalshow("#myModal").modal('show');
});
首次加载应用程序时,会通过调用 StartPlaying() 自动开始播放音乐。这工作得很好。之后,我有一个设置菜单,其中有一个音乐播放开关:
import Foundation
import AVFoundation
class Music {
var isPlaying: Bool = false
public var backgrndSound = AVAudioPlayer()
func isMusicPlaying() -> Bool {
isPlaying = UserDefaults.standard.bool(forKey: "isPlaying")
return isPlaying
}
func StartPlaying() {
let path = Bundle.main.path(forResource: "Music.mp3",ofType: nil)!
let url = URL(fileURLWithPath: path)
do {
self.backgrndSound = try AVAudioPlayer(contentsOf: url)
self.backgrndSound.numberOfLoops = -1
self.backgrndSound.play()
UserDefaults.standard.setValue(true,forKey: "isPlaying")
} catch {
// couldn't load file :(
}
}
func StopPlaying() {
self.backgrndSound.pause()
self.backgrndSound.stop()
}
}
当我点击开关时,它确实会触发 StopPlaying(),但尽管点击了,背景中的音乐仍会继续播放。
我不知道为什么会这样,除非无法从原始创建访问 AV 对象,因此无法正确停止它;但到目前为止我也无法弄清楚。
任何帮助或建议将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)