Jump to content

playSound() - How to take value from...


novo

Recommended Posts

You can, however, create a sound "manager" resource. You create all the functions for playing sounds there and save the sound element in a variable inside that resource and then export all functions. Then you create a function to get current sound element.

Here's an example:

Audio resource > Sound.lua:

local pSound = nil 
  
SoundPlay = function( path, loop ) 
    pSound = playSound( path, loop ); 
    if ( pSound ) then 
        return true 
    end 
    return false 
end 
  
SoundStop = function() 
    destroyElement( pSound ); 
    if ( not isElement( pSound ) ) then 
        return true 
    end 
    return false 
end 
  
SoundGetCurrent = function() 
    return pSound 
end 

Sound resource > meta.xml:

<meta> 
    <script src="Sound.lua" type="client" /> 
     
    <export function="SoundPlay" type="client"/> 
    <export function="SoundStop" type="client"/> 
    <export function="SoundGetCurrent" type="client"/> 
</meta> 

Your resource:

exports.[[YOUR_AUDIO_RESOURCE_NAME]]:SoundPlay( "sound1.mp3", false ); 
-- ... 
local currentSound = exports.[[YOUR_AUDIO_RESOURCE_NAME]]:SoundGetCurrent(); 
-- ... 
exports.[[YOUR_AUDIO_RESOURCE_NAME]]:SoundStop(); 
  
--or 
  
exports["[[YOUR_AUDIO_RESOURCE_NAME]]"]:SoundPlay( "sound1.mp3", false ); 
-- ... 
local currentSound = exports["[[YOUR_AUDIO_RESOURCE_NAME]]"]:SoundGetCurrent(); 
-- ... 
exports["[[YOUR_AUDIO_RESOURCE_NAME]]"]:SoundStop(); 

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...