The problem in changing the volume of an audio stream

Hello ,

I run my audio file with the following code but volume does not change:

function \_M.playStream (name, params) if not loadedSounds[name] then loadedSounds[name] = audio.loadStream(sounds[name]) end audio.play(loadedSounds[name], { channel = params.channel, fadein = 1000, loops = params.loops or 0 }) \_M.setVolume( params.volume, { channel = params.channel } ) end

Volume function code:

function \_M.setVolume (volume, params) audio.setVolume( volume, { channel = params.channel } ) end

And in this case I used the function:

\_G.BackMusic = 50 sounds.playStream("backMusic", { channel = 1, volume = \_G.BackMusic / 100, loops = -1 }) sounds.setVolume(\_G.BackMusic / 100, { channel = 1 })

But the volume does not apply and the numbers will change nothing happens.

This problem is only in the stream.
please guide me.

Hi @forghaniir,

So, the music does play? And assuming so, it begins at 0.5 volume (50/100)? Meaning, is the function “_M.setVolume()” definitely being called from within the “_M.playStream()” function? Can you confirm that by adding a basic “print()” statement inside the “_M.setVolume()” function to ensure it’s reaching that point?

Best regards,

Brent

Hi ,

Yes, the sound runs but it does not change the volume with the default volume.

As I said, No 0.5 does not play.

I "audio.setVolume" I tried it, but the volume did not change.

Function “_M.setVolume” in this case have changed and the message was printed twice:

function \_M.setVolume (volume, params) audio.setVolume( volume, { channel = params.channel } ) print("Volume changed.") end

where is the problem from?

Thank you.

Hello,
I need urgent help. There is no solution?
Thanks,
forghaniir

I suggest creating a small test case showing the issue in its entirety (main.lua, config.lua, build.settings, and one streamed sound).

Then link the zip file containing your example here so others can look and give early feedback.  

The alternative is someone here does all those steps and shows that it is working or not for you.  I personally prefer to help folks if they’ve given me a (complete but small) working sample to look at.

Help us by making a test case and we’ll help you by looking into the problem.

Hi @forghaniir,

Please simply zip up and attach your project here (in the forums). I don’t believe this is a Corona bug, so please do not file a bug report at this time. If we diagnose it as a bug after seeing your project, then we can take the necessary steps on that.

Best regards,

Brent

Hello ,

I uploaded a sample project that has the same problem.

Download Project

Your code is wrong, but there are solutions ahead…

You are using a fadein and not accounting for the time before setting the volume.

 local fadein = 1000 audio.play(loadedSounds[name], { channel = params.channel, fadein = fadein, loops = params.loops or 0 }) -- Wait, then set volume timer.performWithDelay( fadein + 30, function() \_M.setVolume( params.volume, { channel = params.channel } ) end )

If you want to fade in to a specific volume level (from 0) you’ll need to do something like this:

function \_M.playStream (name, params) params = params or {} if not loadedSounds[name] then loadedSounds[name] = audio.loadStream(sounds[name]) end audio.play(loadedSounds[name], { fadein = params.fadein, channel = params.channel, loops = params.loops or 0 }) if( params.fadeInTo ) then -- Start at 0 \_M.setVolume( 0, { channel = params.channel } ) -- Transition a proxy to raise level over time. local proxy = { step = 0 } local mt = { \_\_index = function(t,k) return t["step"] end, \_\_newindex = function (t,k,v) if(\_M.setVolume) then local volume = v \_M.setVolume( volume, { channel = params.channel } ) end t["step"] = v ; end } setmetatable(proxy,mt) transition.to( proxy, { delay = params.fadeInToDelay, time = params.fadeInToTime or 1000, volume = params.fadeInTo } ) end end

Then you can do this:

sounds.playStream("backMusic", { channel = 1, fadeInTo = 0.5, fadeInToDelay = 2000, fadeInToTime = 5000, loops = -1 })

Don’t try to use fadein and my new code fadeInTo at the same time.  It is one or the other.

Hello and thank you ,

I got the first code you use, but the volume will change after a few seconds. My code:

function \_M.playStream (name, params) if not loadedSounds[name] then loadedSounds[name] = audio.loadStream(sounds[name]) end audio.play(loadedSounds[name], { channel = params.channel, fadein = 1000, loops = params.loops or 0 }) timer.performWithDelay( 1030, function() \_M.setVolume( params.volume, { channel = params.channel } ) end , 1 ) end

In the meantime, if you can explain a little bit about “FadeIn”.

I found a solution! The volume of our sound code to code play.
Of course, if you want to be the automatic channel selection use the following code:

function \_M.playStream (name, params) if not loadedSounds[name] then loadedSounds[name] = audio.loadStream(sounds[name]) end local currentChannel = audio.findFreeChannel() \_M.setVolume( params.volume, { channel = currentChannel } ) audio.play(loadedSounds[name], { channel = currentChannel, fadein = 1000, loops = params.loops or 0 }) end

Thanks all

Hi @forghaniir,

So, the music does play? And assuming so, it begins at 0.5 volume (50/100)? Meaning, is the function “_M.setVolume()” definitely being called from within the “_M.playStream()” function? Can you confirm that by adding a basic “print()” statement inside the “_M.setVolume()” function to ensure it’s reaching that point?

Best regards,

Brent

Hi ,

Yes, the sound runs but it does not change the volume with the default volume.

As I said, No 0.5 does not play.

I "audio.setVolume" I tried it, but the volume did not change.

Function “_M.setVolume” in this case have changed and the message was printed twice:

function \_M.setVolume (volume, params) audio.setVolume( volume, { channel = params.channel } ) print("Volume changed.") end

where is the problem from?

Thank you.

Hello,
I need urgent help. There is no solution?
Thanks,
forghaniir

I suggest creating a small test case showing the issue in its entirety (main.lua, config.lua, build.settings, and one streamed sound).

Then link the zip file containing your example here so others can look and give early feedback.  

The alternative is someone here does all those steps and shows that it is working or not for you.  I personally prefer to help folks if they’ve given me a (complete but small) working sample to look at.

Help us by making a test case and we’ll help you by looking into the problem.

Hi @forghaniir,

Please simply zip up and attach your project here (in the forums). I don’t believe this is a Corona bug, so please do not file a bug report at this time. If we diagnose it as a bug after seeing your project, then we can take the necessary steps on that.

Best regards,

Brent

Hello ,

I uploaded a sample project that has the same problem.

Download Project

Your code is wrong, but there are solutions ahead…

You are using a fadein and not accounting for the time before setting the volume.

 local fadein = 1000 audio.play(loadedSounds[name], { channel = params.channel, fadein = fadein, loops = params.loops or 0 }) -- Wait, then set volume timer.performWithDelay( fadein + 30, function() \_M.setVolume( params.volume, { channel = params.channel } ) end )

If you want to fade in to a specific volume level (from 0) you’ll need to do something like this:

function \_M.playStream (name, params) params = params or {} if not loadedSounds[name] then loadedSounds[name] = audio.loadStream(sounds[name]) end audio.play(loadedSounds[name], { fadein = params.fadein, channel = params.channel, loops = params.loops or 0 }) if( params.fadeInTo ) then -- Start at 0 \_M.setVolume( 0, { channel = params.channel } ) -- Transition a proxy to raise level over time. local proxy = { step = 0 } local mt = { \_\_index = function(t,k) return t["step"] end, \_\_newindex = function (t,k,v) if(\_M.setVolume) then local volume = v \_M.setVolume( volume, { channel = params.channel } ) end t["step"] = v ; end } setmetatable(proxy,mt) transition.to( proxy, { delay = params.fadeInToDelay, time = params.fadeInToTime or 1000, volume = params.fadeInTo } ) end end

Then you can do this:

sounds.playStream("backMusic", { channel = 1, fadeInTo = 0.5, fadeInToDelay = 2000, fadeInToTime = 5000, loops = -1 })

Don’t try to use fadein and my new code fadeInTo at the same time.  It is one or the other.

Hello and thank you ,

I got the first code you use, but the volume will change after a few seconds. My code:

function \_M.playStream (name, params) if not loadedSounds[name] then loadedSounds[name] = audio.loadStream(sounds[name]) end audio.play(loadedSounds[name], { channel = params.channel, fadein = 1000, loops = params.loops or 0 }) timer.performWithDelay( 1030, function() \_M.setVolume( params.volume, { channel = params.channel } ) end , 1 ) end

In the meantime, if you can explain a little bit about “FadeIn”.