Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
audio Event.COMPLETE - Gideros Forum

audio Event.COMPLETE

hello gideros people,

Is it the best way to play sounds one after the other?
	local arabaudio1 = csv:getField(self.i, "audio1")
	local arabaudio2 = csv:getField(self.i, "audio2")
	local arabaudio3 = csv:getField(self.i, "audio3")
	if arabaudio1 ~= "null" then
		local sound1 = Sound.new("audio/"..arabaudio1..".ogg")
		local audio1 = sound1:play()
		if arabaudio2 ~= "null" then
			audio1:addEventListener(Event.COMPLETE, function()
				local sound2 = Sound.new("audio/"..arabaudio2..".ogg")
				local audio2 = sound2:play()
				if arabaudio3 ~= "null" then
					audio2:addEventListener(Event.COMPLETE, function()
						local sound3 = Sound.new("audio/"..arabaudio3..".ogg")
						local audio3 = sound3:play()
					end)
				end
			end)
		end
	end
+ do I need to add audio:removeEventListener?

Thank you for your advice.

Comments

  • Apollo14Apollo14 Member
    edited May 30 Accepted Answer
    Definitely 'for i=1,3' loop can be used here.
    And I guess we can write it more elegantly using Lua coroutines (they're perfect for such purposes).
    But if there're not many audios, I'd leave the code as it is.

    P. S. We can't 'removeEventListener' with anonymous function:
    audio2:addEventListener(Event.COMPLETE, function()
    	local sound3 = Sound.new("audio/"..arabaudio3..".ogg")
    	local audio3 = sound3:play()
    end)
    audio2:removeEventListener(...) --we can't do it
    But in the recent releases super useful call 'removeAllListeners' was added, so now we can do it this way:
    audio2:removeAllListeners()
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • MoKaLuxMoKaLux Member
    thank you apollo14, for the moment I have to read at most 3 words. Then may be later more than 3 words maybe 10!

    I would be interested in a for loop implementation.

    Could you please show me some sample code?

    Or I can try myself really and come back!

    Thank you for your advice apollo14.
  • Apollo14Apollo14 Member
    edited May 30
    MoKaLux said:

    thank you apollo14, for the moment I have to read at most 3 words. Then may be later more than 3 words maybe 10!

    I would be interested in a for loop implementation.

    Could you please show me some sample code?

    Or I can try myself really and come back!

    Thank you for your advice apollo14.

    I think something like that will work (I'm also new to coroutines, never used them in actual projects):
    local numberOfSounds=100
     
    local ourAudioCoroutine = coroutine.create(function ()	
    	for i=1,numberOfAudios do
    		local arabaudio = csv:getField(self.j, "audio"..i)
    		local sound = Sound.new("audio/"..arabaudio..".ogg")
    		local audio = sound:play()
     
    		audio:addEventListener(Event.COMPLETE, function()
    			audio:removeAllListeners(); audio,sound,arabaudio=nil,nil,nil
    			coroutine.resume(ourAudioCoroutine)
    		end)
     
    		coroutine.yield()
    	end
    end)
     
    --now let's start our coroutine:
    coroutine.resume(ourAudioCoroutine)
    you can check more on coroutines:
    https://www.lua.org/pil/9.1.html
    http://lua-users.org/wiki/CoroutinesTutorial
    https://www.tutorialspoint.com/lua/lua_coroutines.htm
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • MoKaLuxMoKaLux Member
    great @Apollo14 it is not that simple, I will have to try your method. Thank you very much for your help. Much appreciated.

    Peace.
Sign In or Register to comment.