2016-11-16 48 views
3

在iPhone ios10上安裝後,我準備發佈appstore應用程序不再播放聲音!幫幫我!隨着ios9一切都非常完美Sprite Kit不再使用iOS10播放聲音

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

[476:72963] SKAction:錯誤播放聲音資源

+0

同樣的問題https://forums.developer.apple.com/thread/63532 – SashDing

+0

不幸的是,SpriteKit存在很長的聲音問題。你有沒有檢查過SKAction的聲音?試圖創建一個變量來存儲操作?聽起來很奇怪,嘗試將聲音文件重新導入到您的項目中......一個接一個......? – Confused

+0

from @SashDing鏈接,試試這個:'var enemyCollisionSound:SKAction {return SKAction.playSoundFileNamed(「Lose_bell.wav」,waitForCompletion:false)}' – Confused

回答

2

我被這個問題真的惱火,所以我創建了一個名爲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") 

你的聲音應該播放。您必須在字符串中包含文件的類型。我完全擺脫了播放聲音的所有SKA,並將它們改爲我的代碼。這一切現在完美,沒有錯誤。我使用這個類與我的JKButtonNode一起播放聲音。

爲了記錄,我將所有的聲音文件從.mp3轉換爲.wav。我不知道這是否是它的工作原理,但當我有SKAction錯誤時,我將它們全部切換。

此外,我爲我添加了一個快捷方式。如果您希望用戶在玩遊戲時能夠播放音樂,可以在GameViewController中添加此代碼。

JKAudioPlayer.canShareAudio = true 
+2

非常感謝! – SashDing

+0

這可能也會擺脫加載聲音時出現的異常:'let actionSound1 = SKAction.playSoundFileNamed(「ActionBeep_AP1。6「,waitForCompletion:false)' – Confused

+0

看起來像這樣加載聲音,SKActions就像上面的註釋一樣,在項目文件夾的前三個搜索中找不到它,創建了3個例外,我有9個,所以這是27例外,每次我開始時,都是真氣,但在其他意義上起作用。 – Confused

2

請糾正我的英語,對不起。我發現問題是什麼。此前,該行動在完成其所有組成要素之後纔會結束。 iOS10不考慮聲音播放重要事項,並在完成其他操作項目後立即丟棄它。例如,此選項將不執行音頻片段:

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不起作用。