Defranco Posted February 27, 2017 Share Posted February 27, 2017 **should have put this in programming I am having a bit of difficulty getting a sound file to stop playing. I've converted the sound wav file to a source, set the source to loop, set volume, and I can get it to play on-key press no problem But I can't get it stop on keypress again. This is just 1 variation of the method I've tried. I've tried moving around so everything is in its own script, I've tried parts of it in updateworld, tried it in update physics, I just can't get it to stop. --Toggle the torch on and off self.sound.FLsound = Sound:Load("Sound/Torch/Torch2.wav") if window:KeyHit(Key.F) then self.sound.torch1:Play() self.FLsource = Source:Create() self.FLsource:SetSound(self.sound.FLsound) self.FLsource:SetVolume(0.2) self.FLsource:SetLoopMode(true) if self.torch1:Hidden() then self.torch1:Show() self.FLsource:Play() else self.torch1:Hide() self.FLsource:Stop() end end Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted February 27, 2017 Share Posted February 27, 2017 You are playing the sound instead of the source. Remove that line. Using either updateworld or updatephysics won't make a difference for this code. You can also request the state of the source with GetState() self.source:GetState()==Source.Playing Quote Link to comment Share on other sites More sharing options...
Jazz Posted February 27, 2017 Share Posted February 27, 2017 What Aggror said. Also, I check if the source exists before trying to play it, just in case. if window:KeyHit(Key.F) then self.FLsource = Source:Create() self.FLsource:SetSound(self.sound.FLsound) self.FLsource:SetVolume(0.2) self.FLsource:SetLoopMode(true) end if self.torch1:Hidden() then self.torch1:Show() --check if source exists if self.FLsource ~= nil then --if it's not playing, play it if self.FLsource:GetState() == Source.Stopped then self.FLsource:Play() end end else self.torch1:Hide() --check if source exists if self.FLsource ~= nil then --if it's playing, stop it if self.FLsource:GetState() == Source.Playing then self.FLsource:Stop() end end end Quote --- Scott Using Windows 7 Ultimate 64 bit/Core I7-2700K @ 4312mhz/24G RAM/Nvidia GTX 1060 Link to comment Share on other sites More sharing options...
Defranco Posted February 27, 2017 Author Share Posted February 27, 2017 The first sound line (not using source) is a on-button sound effect self.sound.torch1:Play() its about a 1 second sound effect Whereas the one i'm trying to use in the source is a loop effect that keeps playing until the action is turned off I was playing around with if self.FLsource:GetState() == Source.Playing then self.FLsource:Stop() But it has no effect and won't stop. I've even put in system:prints right in there to test, and the first prints go off, but the sound keeps on playing and the second system print doesn't register. Basically I have, if window:KeyHit(Key.F) then self.FLsource = Source:Create() self.FLsource:SetSound(self.sound.FLsound) self.FLsource:SetVolume(0.2) self.FLsource:SetLoopMode(true) if self.torch1:Hidden() then self.torch1:Show() --check if source exists if self.FLsource ~= nil then --if it's not playing, play it if self.FLsource:GetState() == Source.Stopped then self.FLsource:Play() System:Print("Sound On") end end else self.torch1:Hide() --check if source exists if self.FLsource ~= nil then --if it's playing, stop it if self.FLsource:GetState() == Source.Playing then self.FLsource:Stop() System:Print("Sound Off") end end end end Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted February 27, 2017 Share Posted February 27, 2017 What is self.torch1:Hidden() returning the second time you press F? What happens when you replace the else segment with the following. Does it get printed? else System:Print("torch is not hidden") self.torch1:Hide() if self.FLsource ~= nil then System:Print("source is not nil") if self.FLsource:GetState() == Source.Playing then self.FLsource:Stop() System:Print("Sound Off") end end end Quote Link to comment Share on other sites More sharing options...
Defranco Posted February 27, 2017 Author Share Posted February 27, 2017 if window:KeyHit(Key.F) then self.FLsource = Source:Create() self.FLsource:SetSound(self.sound.FLsound) self.FLsource:SetVolume(0.2) self.FLsource:SetLoopMode(true) if self.torch1:Hidden() then self.torch1:Show() --check if source exists if self.FLsource ~= nil then --if it's not playing, play it if self.FLsource:GetState() == Source.Stopped then self.FLsource:Play() if self.FLsource:GetState() == Source.Playing then System:Print("Sound Is On") end System:Print("Sound Is Still On") end end else self.torch1:Hide() System:Print("Hide Light Works") --check if source exists if self.FLsource ~= nil then System:Print("Check 1 This works too") if self.FLsource:GetState() == Source.Playing then System:Print("Check 2 Doesn't work") self.FLsource:Stop() System:Print("Sound Off Doesn't work") end end end end Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted February 27, 2017 Share Posted February 27, 2017 Try creating the source in the start function instead of the updateworld function. You are creating the source every time you press F. So when you try to stop it, it might not know what to do as it is not playing either. As a matter of fact, it is not doing anything at all. Quote Link to comment Share on other sites More sharing options...
Defranco Posted February 27, 2017 Author Share Posted February 27, 2017 Seems like it cant get past the "if self.FLsource:GetState()== Source.Playing then" that shows up after the Else When I pres F the 2nd time -- it hides the torch It prints: System:Print("Hide Light Works") and System:Print("Check 1 This works too") Thats as far as it goes. When I do System:Print(self.FLsource:GetState()) It returns 1 before the else statement and returns 0 after the else statement Quote Link to comment Share on other sites More sharing options...
Defranco Posted February 27, 2017 Author Share Posted February 27, 2017 I think its working. I moved just the sound file for the torch and put it under self.sound={} (which is in function start) And it works under it, odd. I didn't think it had to be part of the table Edit: I had originally had tried it both in the Function UpdatePhysics as well as at the top of the Function Start (above self.sound={}) moving it a few lines down seems to be working. I'm not sure I understand the logic..... Is it because the table starts with self.sound and the line calling the sound file was called self.sound.FLsound? Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted February 27, 2017 Share Posted February 27, 2017 Can you post your original complete script? I think the declaration of the table might be the thing that is causing the issue but without seeing the entire code I am just guessing. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.