Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 如何只播放一次声音_Xcode_Swift_Audio - Fatal编程技术网

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
}