【问题标题】:Sprite Kit no longer play sounds with iOS10Sprite Kit 不再使用 iOS10 播放声音
【发布时间】:2017-03-31 06:21:30
【问题描述】:

在 iPhone ios10 上安装后,我准备发货的 appstore 应用不再播放声音!帮助!使用 ios9 一切正常

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

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

【问题讨论】:

  • 不幸的是,声音在 SpriteKit 中一直是个问题。您是否检查以确保 SKAction 找到了声音?尝试创建一个存储动作的变量?听起来很奇怪,尝试将声音文件重新导入到您的项目中...一个接一个...?
  • 来自@SashDing 链接的线程,试试这个:var enemyCollisionSound: SKAction { return SKAction.playSoundFileNamed("Lose_bell.wav", waitForCompletion: false) }
  • @Confused,谢谢。我会做实验。

标签: sprite-kit swift3 ios10


【解决方案1】:

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

它有 2 个 AVAudioPlayer 对象。 1 播放音乐,1 播放声音。

这是课程。

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")

你的声音应该会播放。您必须在字符串中包含文件的类型。我完全摆脱了所有播放声音的 SKAction 并将它们更改为我的代码。现在一切都很完美,没有错误。我也将这个类与我的 JKButtonNode 一起使用来播放声音。

为了记录,我将所有声音文件从 .mp3 制作成 .wav。我不知道这是否是它起作用的原因,但我在遇到 SKAction 错误时将它们全部切换了。

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

JKAudioPlayer.canShareAudio = true

【讨论】:

  • 这可能也消除了我在加载这样的声音时遇到的异常:let actionSound1 = SKAction.playSoundFileNamed("ActionBeep_AP1.6", waitForCompletion: false)
  • 似乎加载听起来像这样,SKActions,如上面的评论,在项目文件夹中的前三个搜索中找不到它,创建了 3 个异常。我有其中的 9 个,所以每次我开始时都有 27 个例外。真气。但在其他任何方面都有效。
  • 这不会预加载声音,因此第一次播放时会有延迟。此外,这不允许同时播放多个效果声音。 SKActions 将允许同时播放多个声音。并且可以通过在类中创建一个变量来预加载(我不知道怎么说)。
【解决方案2】:

请纠正我的英语,对不起。我发现了问题所在。以前,该动作在完成其所有组成元素之前不会结束。 iOS10不考虑声音播放,其他动作一完成就丢弃。比如这个选项不会执行audio sn -p:

run(SKAction.sequence([SKAction.playSoundFileNamed("Lose_bell.wav", waitForCompletion: fals), SKAction.removeFromParent()]))

但这一次要实现:

run(SKAction.sequence([SKAction.playSoundFileNamed("Lose_bell.wav", waitForCompletion: fals),SKAction.wait(forDuration: 5.0), SKAction.removeFromParent()]))

是否有可能以某种方式继续等待表现良好的股票?方法waitForCompletion: true不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 2017-01-24
    • 2011-01-23
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多