Download volume to SKAudioNode in a certain time

0

I am trying to download the volume to a SKAudioNode using the following code:

audioNode?.run(SKAction.changeVolume(to: 0.0, duration: 3.0 ))

You should lower the volume to 0.0 in 3.0 seconds. But the sound stops immediately.

Is it possible to do this in SpriteKit?

    
asked by Maetschl 21.10.2016 в 02:24
source

1 answer

1

I found the following solution:

Swift 3:

extension SKAction  {  
  // All the change... actions for SKAudioNode are broken. They do not work with looped audio. These are replacements  

  public class func _changeVolumeTo(endVolume: Float, duration: NSTimeInterval) -> SKAction  
       {  
       var startVolume : Float!  
       var distance : Float!  

   let action = SKAction.customActionWithDuration(duration)  
        {  
        node, elapsedTime in  

        if let soundNode = node as? SoundNode  
             {  
             if startVolume == nil  
                  {  
                  startVolume = soundNode.volume  
                  distance = endVolume - startVolume  
                  }  

             let fraction = Float(elapsedTime / CGFloat(duration))  
             let newVolume = startVolume + (distance * fraction)  

             soundNode.volume = newVolume  
             }  
        }  

   return action  
   }  
  }  

Source: link

    
answered by 22.04.2017 / 16:25
source