Xcode 如何只播放一次声音
我的代码有点问题,当我启动我的应用程序时,谁让我播放声音。但问题是每次我回到第一个屏幕时,声音都会再次播放,我希望它只播放一次。当菜单屏幕第一次弹出时 这是我的密码Xcode 如何只播放一次声音,xcode,swift,audio,Xcode,Swift,Audio,我的代码有点问题,当我启动我的应用程序时,谁让我播放声音。但问题是每次我回到第一个屏幕时,声音都会再次播放,我希望它只播放一次。当菜单屏幕第一次弹出时 这是我的密码 var bubbleSound: SystemSoundID! bubbleSound = createBubbleSound() AudioServicesPlaySystemSound(bubbleSound) (……) 功能 func createBubbleSound() -> SystemSoundID
var bubbleSound: SystemSoundID!
bubbleSound = createBubbleSound()
AudioServicesPlaySystemSound(bubbleSound)
(……)
功能
func createBubbleSound() -> SystemSoundID {
var soundID: SystemSoundID = 0
let soundURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), "bubble", "wav", nil)
AudioServicesCreateSystemSoundID(soundURL, &soundID)
return soundID
}
您可以像这样定义一个
struct
:
然后在视图中加载:
if(!MyViewState.hasPlayedSound) {
var bubbleSound: SystemSoundID!
bubbleSound = createBubbleSound()
AudioServicesPlaySystemSound(bubbleSound)
MyViewState.hasPlayedSound = true
}
然后,您可以修改MyViewState.hasPlayedSound
,并允许UIViewController
根据需要再次播放声音。AudioServicesPlaySound(bubbleSound)
位于何处?这是否在您的视图中加载了?是!在我看来
if(!MyViewState.hasPlayedSound) {
var bubbleSound: SystemSoundID!
bubbleSound = createBubbleSound()
AudioServicesPlaySystemSound(bubbleSound)
MyViewState.hasPlayedSound = true
}