How to lower the volume of music in swift?
Asked Answered
N

2

11

I am creating a SpriteKit Game with background music looping. The problem is that the music is too loud. How do I lower the volume?

Here is the code I used to set up the music

    var backGroundMusic = AVAudioPlayer()
    var bgMusicUrl:NSURL = NSBundle.mainBundle().URLForResource("BackGround", withExtension: "wav")
    backGroundMusic = AVAudioPlayer(contentsOfURL:bgMusicUrl, error: nil)
    backGroundMusic.numberOfLoops = (-1)
    backGroundMusic.prepareToPlay()
    backGroundMusic.play()
Nuclear answered 19/8, 2014 at 23:35 Comment(0)
U
22

Have you tried this?

backGroundMusic.volume = 0.5

Volume goes from 0.0 to 1.0 (full volume). https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/index.html

Unicef answered 20/8, 2014 at 3:21 Comment(3)
I have the same issue. I tried this, but whatever I do, its always on max volume. Even with the value 0.1. Not sure what to do :sNady
@Nady seems I don't have this problem.Churchwarden
@Nady make sure you set the volume after play()Benton
H
0

Try this:

backGroundMusic.setVolume(0.05, fadeDuration: TimeInterval(1))
Harcourt answered 20/1, 2022 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.