gpt4 book ai didi

sprite-kit - Sprite Kit 不再使用 iOS10 播放声音

转载 作者:行者123 更新时间:2023-12-01 12:25:04 28 4
gpt4 key购买 nike

在 iPhone ios10 上安装后,我的 ready to ship appstore 应用程序不再播放声音!帮助!使用 ios9,一切都完美运行

SKAction.playSoundFileNamed("Lose_bell.wav", waitForCompletion: false)

[476:72963] SKAction:播放声音资源时出错

最佳答案

我真的被这个问题惹恼了,所以我创建了自己的自定义类 JKAudioPlayer 来暂时避免这个问题并为我处理所有音频。

它有 2 个 AVAudioPlayer 对象。 1 用于播放音乐,1 用于通过它播放声音。

这是类(class)。

import Foundation
import SpriteKit
import AVFoundation

/**Manages a shared instance of JKAudioPlayer.*/
private let JKAudioInstance = JKAudioPlayer()

/**Provides an easy way to play sounds and music. Use sharedInstance method to access
a single object for the entire game to manage the sound and music.*/
open class JKAudioPlayer {

/**Used to access music.*/
var musicPlayer: AVAudioPlayer!
var soundPlayer: AVAudioPlayer!

/** Allows the audio to be shared with other music (such as music being played from your music app).
If this setting is false, music you play from your music player will stop when this app's music starts.
Default set by Apple is false. */
static var canShareAudio = false {
didSet {
canShareAudio ? try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
: try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
}
}

/**Creates an instance of the JAAudio class so the user doesn't have to make
their own instance and allows use of the functions. */
open class func sharedInstance() -> JKAudioPlayer {
return JKAudioInstance
}

/**Plays music. You can ignore the "type" property if you include the full name with extension
in the "filename" property. Set "canShareAudio" to true if you want other music to be able
to play at the same time (default by Apple is false).*/
open func playMusic(_ fileName: String, withExtension type: String = "") {
if let url = Bundle.main.url(forResource: fileName, withExtension: type) {
musicPlayer = try? AVAudioPlayer(contentsOf: url)
musicPlayer.numberOfLoops = -1
musicPlayer.prepareToPlay()
musicPlayer.play()
}
}

/**Stops the music. Use the "resumeMusic" method to turn it back on. */
open func stopMusic() {
if musicPlayer != nil && musicPlayer!.isPlaying {
musicPlayer.currentTime = 0
musicPlayer.stop()
}
}

/**Pauses the music. Use the "resumeMusic" method to turn it back on. */
open func pauseMusic() {
if musicPlayer != nil && musicPlayer!.isPlaying {
musicPlayer.pause()
}
}

/**Resumes the music after being stopped or paused. */
open func resumeMusic() {
if musicPlayer != nil && !musicPlayer!.isPlaying {
musicPlayer.play()
}
}

/**Plays a single sound.*/
open func playSoundEffect(named fileName: String) {
if let url = Bundle.main.url(forResource: fileName, withExtension: "") {
soundPlayer = try? AVAudioPlayer(contentsOf: url)
soundPlayer.stop()
soundPlayer.numberOfLoops = 1
soundPlayer.prepareToPlay()
soundPlayer.play()
}
}
}

下面是我的使用方法。首先,创建一个全局变量来处理音频(如果需要,也可以只处理声音)。

let audio = JKAudioPlayer.sharedInstance()

然后,每当您想播放声音时,只需执行此操作即可。

audio.playSoundEffect(named: "SoundFileName.type")

您的声音应该会播放。您必须在字符串中包含文件类型。我完全摆脱了所有播放声音的 SKActions 并将它们更改为我的代码。现在一切正常,没有错误。我也将此类与我的 JKButtonNode 结合使用来播放声音。

郑重声明,我将所有的声音文件都从 .mp3 制作成了 .wav。我不知道这是否是它起作用的原因,但我在出现 SKAction 错误时将它们全部切换。

此外,我还为自己添加了一个快捷方式。如果您希望用户能够在玩游戏时播放音乐,您可以在 GameViewController 中添加此代码。

JKAudioPlayer.canShareAudio = true

关于sprite-kit - Sprite Kit 不再使用 iOS10 播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40642526/

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