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

标签 sprite-kit swift3 ios10

在 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/

相关文章:

iOS swift : best way to pass data to other VCs after query is completed

ios - 声明计算变量后,编译器开始提示类没有初始化器

ios - 在 iOS 11 Swift 4 上应用渐变后,UINavigationBar 标题和按钮消失

ios - 从 viewWillLayoutSubviews 移动到 viewDidLoad 后 View 不显示

ios - 跟随玩家时 SpriteKit 相机卡顿

swift - NSNumber 不是 UInt8 的子类型

ios - 围绕原点旋转整个 SKView

swift - 在 ScrollView 中动态隐藏状态栏时滞后/屏幕卡住 (Swift 3)

ios - 新的 iOS 10 Today Widget/Extension 的高度是多少?

swift - Firebase 身份验证注销错误 - Swift