Difference between revisions of "Source:setVolume"

m
m (Examples)
 
(5 intermediate revisions by 4 users not shown)
Line 6: Line 6:
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|number|volume|The volume for a Source, where 1.0 is normal volume.}}
+
{{param|number|volume|The volume for a Source, where 1.0 is normal volume. Volume cannot be raised above 1.0.}}
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
Nothing.
 +
== Examples ==
 +
Make a sound quieter or completely silent.
 +
<source lang="lua">
 +
function love.load()
 +
    sound = love.audio.newSource("sound.wav")
 +
 +
    -- Note that this code, as-is, will set the volume to 1.0, as per the last line, and that's how sound:play() will play it back.
 +
    sound:setVolume(0.5) -- 50% volume
 +
    sound:setVolume(0) -- No sound
 +
    sound:setVolume(1) -- Reset to maximum volume.
 +
end
 +
</source>
 +
Set different volumes depending on the sound type.
 +
<source lang="lua">
 +
function love.load()
 +
    effect = love.audio.newSource("soundeffect.wav")
 +
    music = love.audio.newSource("music.mp3")
 +
 +
    masterVolume = 0.5 -- Maximum volume for all sounds
 +
    effectVolume = 0.75
 +
    musicVolume = 1
 +
 +
    effect:setVolume(masterVolume * effectVolume)
 +
    music:setVolume(masterVolume * musicVolume)
 +
end
 +
</source>
 +
 
== See Also ==
 
== See Also ==
 
* [[parent::Source]]
 
* [[parent::Source]]
 
[[Category:Functions]]
 
[[Category:Functions]]
 
{{#set:Description=Sets the current volume of the Source.}}
 
{{#set:Description=Sets the current volume of the Source.}}
 +
{{#set:Since=000}}
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|Source:setVolume}}
 
{{i18n|Source:setVolume}}

Latest revision as of 14:26, 31 May 2018

Sets the current volume of the Source.

Function

Synopsis

Source:setVolume( volume )

Arguments

number volume
The volume for a Source, where 1.0 is normal volume. Volume cannot be raised above 1.0.

Returns

Nothing.

Examples

Make a sound quieter or completely silent.

function love.load()
    sound = love.audio.newSource("sound.wav")

    -- Note that this code, as-is, will set the volume to 1.0, as per the last line, and that's how sound:play() will play it back.
    sound:setVolume(0.5) -- 50% volume
    sound:setVolume(0) -- No sound
    sound:setVolume(1) -- Reset to maximum volume.
end

Set different volumes depending on the sound type.

function love.load()
    effect = love.audio.newSource("soundeffect.wav")
    music = love.audio.newSource("music.mp3")

    masterVolume = 0.5 -- Maximum volume for all sounds
    effectVolume = 0.75
    musicVolume = 1

    effect:setVolume(masterVolume * effectVolume)
    music:setVolume(masterVolume * musicVolume)
end

See Also


Other Languages