gpt4 book ai didi

ios - 如何同步背景音乐和按钮?

转载 作者:行者123 更新时间:2023-11-29 13:51:45 24 4
gpt4 key购买 nike

应用程序写入。音乐在应用程序的背景上播放。我想从应用程序的每个页面停止和恢复此音乐

但是在一个页面按下音乐停止按钮后,其他页面的按钮没有变化。

当我回到第一页时,音乐又重新开始了。

import Foundation
import AVFoundation

class Music {

static var audioPlayer : AVAudioPlayer?
enum SoundEffect {
case background
}

static func playSound(effect : SoundEffect) {

var soundFileName = ""
switch effect {
case.background:
soundFileName = "background"
}

let bundlePath = Bundle.main.path(forResource: soundFileName, ofType: "wav")
guard bundlePath != nil else {
print("Couldn't find sound file \(soundFileName) in the bundle")
return
}

do {
let soudURL = URL(fileURLWithPath: bundlePath!)

audioPlayer = try AVAudioPlayer(contentsOf: soudURL)
audioPlayer!.numberOfLoops = -1
audioPlayer?.prepareToPlay()
audioPlayer?.play()
} catch {

}
}
}

最佳答案

首先检查 AVAudioPlayer 是否已经初始化,然后检查播放器正在播放或停止。

if let audioPlayer = Music.audioPlayer, audioPlayer.playing {
audioPlayer.stop()
}

关于ios - 如何同步背景音乐和按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59376052/

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