Jump to content

Getting sound to Stop()


Defranco
 Share

Recommended Posts

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

Link to comment
Share on other sites

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

---

Scott

 

Using Windows 7 Ultimate 64 bit/Core I7-2700K @ 4312mhz/24G RAM/Nvidia GTX 1060

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...