Jump to content

Detectar cuando un sound llega a su fin


aka Blue

Recommended Posts

Estoy creando un sistema de reproducción de canciones para mi servidor de testeos y me gustaría saber (ya que nunca he jugado a fondo con el sound) cómo hacer que se detecte cuando una canción o sound llega a su fin, para así cambiar una variable global. 

Gracias de antemano!

Link to comment

Lo que se me ocurrio ahora seria usar un onClientRender:

local sound = playSound("sound.mp3")
local soundMaxPosition = math.floor(getSoundLength(sound))

function soundRender()
    local soundPosition = math.floor(getSoundPosition(sound))
    if soundPosition >= soundMaxPosition then
        outputChatBox("Sound finished!")
        removeEventHandler("onClientRender", root, soundRender)
    end
end
addEventHandler("onClientRender", root, soundRender)

 

Edited by TRtam
getSoundLength*
Link to comment

Gracias a ambos. De hecho @TRtam, lo tenía en un onClientRender para mostrar un dx la posición, pasa que hacia el mismo check que me has recomendado y el texto desaparecía. Voy a probarlo y comento.

Lo tengo así y cuando la inicio, acaba:

local sound = nil
local soundMaxPos = nil

addCommandHandler( "yt",
	function( cmd, link )
		if link then
			if sound == nil then
				sound = playSound("https://www.youtubeinmp3.com/fetch/?video="..link) 
				outputChatBox( "Reproduciendo la cancion", 0, 150, 0 )
				soundMaxPos = math.floor( getSoundLength( sound ) )
			else
				if isElement( sound ) then stopSound( sound ) sound = nil else sound = nil soundMaxPos = nil end
				sound = playSound("https://www.youtubeinmp3.com/fetch/?video="..link) 
				soundMaxPos = math.floor( getSoundLength( sound ) )
			end
		else
			outputChatBox( "Introduce un link para reproducir", 255, 0, 0 )
			if isElement( sound ) then stopSound( sound ) sound = nil else sound = nil soundMaxPos = nil end
		end
	end
)

addEventHandler( "onClientRender", root,
	function( )
		if sound then
			if math.floor( getSoundPosition( sound ) ) >= soundMaxPos then
				outputChatBox( "Canción acabada", 0, 150, 0 )
				if isElement( sound ) then stopSound( sound ) end
				sound = nil
			end
		end
	end
)

 

Link to comment

Ya que estas haciendo un stream de la canción (reproduciendo la canción por una pagina, creo que es así como funciona) tenes que usar onClientSoundStream

local sound = nil
local soundMaxPos = nil

addCommandHandler( "yt",
	function( cmd, link )
		if link then
			if sound == nil then
				playSound("https://www.youtubeinmp3.com/fetch/?video="..link) 
			else
				if isElement( sound ) then 
					stopSound( sound ) 
					sound = nil 
				else 
					sound = nil 
					soundMaxPos = nil 
				end
				playSound("https://www.youtubeinmp3.com/fetch/?video="..link) 
			end
		else
			outputChatBox( "Introduce un link para reproducir", 255, 0, 0 )
			if isElement( sound ) then stopSound( sound ) sound = nil else sound = nil soundMaxPos = nil end
		end
	end
)

addEventHandler( "onClientRender", root,
	function( )
		if sound then
			if math.floor( getSoundPosition( sound ) ) >= soundMaxPos then
				outputChatBox( "Canción acabada", 0, 150, 0 )
				if isElement( sound ) then stopSound( sound ) end
				sound = nil
			end
		end
	end
)

addEventHandler("onClientSoundStream", resourceRoot,
	function(suc, length)
		if not suc then
			return 
		end
		sound = source
		soundMaxPos = math.floor(length)
        outputChatBox( "Reproduciendo la cancion", 0, 150, 0)
	end
)

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...