Stopping Starting sound with soundchannels(solved) (finally)

19 posts

Flag Post

So, Ive read that to be able to stop a sound, you use sound channels. Currently my code looks like this.


var bgm:Sound = new bgmusic();
				var bgChannel = new SoundChannel();
				bgChannel = bgm.play();

Ive been trying to create a function to toggle it when I push the M button, but I always get this error, “Access of undefined property bgChannel”


function togglemusic()
{
	if(muted==true)
	{
		bgChannel.stop();
	}
	else 
	{
		bgChannel.play()
	}
}

I cant figure it out. Any tips?

edit, I just realized I wasnt clear on WHERE i was getting the error. I am getting it here



function togglemusic()
{
	if(muted==true)
	{
		bgChannel.stop();//1120: Access of undefined property bgChannel.

	}
	else 
	{
		bgChannel.play()//1120: Access of undefined property bgChannel.

	}
}

Im not getting it when I first declare var bgChannel, or bgChannel =bgm.play();

 
Flag Post
Originally posted by BluePriest:

So, Ive read that to be able to stop a sound, you use sound channels. Currently my code looks like this.


var bgm:Sound = new bgmusic();
				var bgChannel = new SoundChannel();
				bgChannel = bgm.play();

Try changing this to:


var bgm:Sound = new bgmusic();
var bgChannel:SoundChannel = new SoundChannel();
bgChannel = bgm.play();

But this will start your music instantly after your swf is launched, if you want to start music after M is pressed you should replace bgChannel.play(); with bgChannel = bgm.play(); in your tooglemusic();

 
Flag Post

My music plays fine, I have no problem with that. I have the problem with toggling it on and off after I started the game.
Line 88 1120: Access of undefined property bgChannel.

edit, I just realized I wasnt clear on WHERE i was getting the error. I am getting it here



function togglemusic()
{
	if(muted==true)
	{
		bgChannel.stop();//1120: Access of undefined property bgChannel.

	}
	else 
	{
		bgChannel.play()//1120: Access of undefined property bgChannel.

	}
}

Im not getting it when I first declare var bgChannel, or bgChannel =bgm.play();

 
Flag Post

Please post the whole file – I think you may just be having a variable scope issue.

So for example, declare this at the top of your class:
var bgChannel:SoundChannel

And then just do
bgChannel= bgm.play();

 
Flag Post

package 
{

	
	import flash.media.Sound;
	import flash.media.SoundChannel;

	public class Game extends MovieClip
	{
                import flash.events.Event;
		static var BulletLength:Number;
		static var shrunk:Boolean = false;
		static var grown:Boolean = false;
//even more vars declared, but non relevant to this
function togglemusic()
		{
			if (mute1 == true)
			{

				bgChannel.stop();
			}
			else
			{
				bgChannel.play();
			}
		}
		function Game()
		{
			if (Game.Paused == false)
			{


				var bgm:Sound = new bgmusic();
				var bgChannel:SoundChannel = new SoundChannel();
				bgChannel = bgm.play();
//rest of game including tons of functions (but none that modify sound)

This is my main game class, (I believe its usually normal to just call this class Main, or at least I think someone told me that before) so I removed a lot of data, but everything is in the proper order, just non relevant data left out

 
Flag Post

If you really want the whole code for the class btw…

http://pastebin.com/mZw4DiVa

not gonna fill up this topic with it though

 
Flag Post

oO Why so many static variables? And why imports in the list of variables?

More to the point, try this: http://pastebin.com/fCD1wBVX

Just does what I mentioned above.

 
Flag Post

Well that made an interesting change, now the only error Im getting is in relation to the play command saying
1061: Call to a possibly undefined method play through a reference with static type flash.media:SoundChannel.


function togglemusic(e:Event)
		{
			if (mute1 == true)
			{

				bgChannel.stop();
			}
			else
			{
				bgChannel.play();//<-undefined method play();
			}
		}

I remove the part I pointed out and it works fine, so theres no more errors once thats fixed up. Would that have to do with it being a soundchannel instead of just a sound now?

 
Flag Post

Right, you can’t play a sound from a SoundChannel.

So you have to do something like this again:

var bgm:Sound = new bgmusic();
bgChannel = bgm.play();
 
Flag Post
Originally posted by BluePriest:

Well that made an interesting change, now the only error Im getting is in relation to the play command saying
1061: Call to a possibly undefined method play through a reference with static type flash.media:SoundChannel.


function togglemusic(e:Event)
		{
			if (mute1 == true)
			{

				bgChannel.stop();
			}
			else
			{
				bgChannel.play();//&lt;-undefined method play();
			}
		}

I remove the part I pointed out and it works fine, so theres no more errors once thats fixed up. Would that have to do with it being a soundchannel instead of just a sound now?

Yes you cannot use bgChannel.play(). It must be bgChannel = bgm.play();

 
Flag Post

Ok, so one more question ;) This one is related, but on another subject sorta

I need to control it when I push down the M key, I know how to assign keycodes, and all the like just fine, but I cant directly control the toggelmusic function from my Ship class. I get an error. I can do a bit of a work around by setting the soundchannel so that its not public, and that will allow me to control it, and therefor stop it from my ship class by doing Game.bgChannel.stop(); but is there a workaround I can do to make it start as well? Or would it be better for me to just transfer all my sound logic to the ship class?
edit
I know I could just make a button that could call on the togglemusic, but a keypress is just so much more convenient for the player

 
Flag Post
Originally posted by BluePriest:

I need to control it when I push down the M key, I know how to assign keycodes, and all the like just fine, but I cant directly control the toggelmusic function from my Ship class.

That’s why it makes no sense to listen for the M-press in your ship class.

You want to refactor your code and actually use classes.

 
Flag Post

I created a class specifically for sound.


package 
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.events.MouseEvent;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.ui.Keyboard;
	public class soundButton extends MovieClip
	{
		var mreleased:Boolean = false;
		var mute1:Boolean = true;
		function soundButton()
		{

			var bgChannel:SoundChannel;
			var bgm:Sound = new bgmusic  ;
			bgChannel = bgm.play();
			addEventListener("enterFrame",soundcontrol);


		}
		function soundcontrol(e:Event)
		{

			if (Key.isDown(77))
			{
				if (mreleased)
				{
					togglemusic();
					mute1 = ! mute1;
					this.nextFrame();
				}
				mreleased = false;
			}
			else
			{
				mreleased = true;

			}
		}
		function togglemusic()
		{
			if (mute1 == true)
			{
				trace("muted");
				bgChannel.stop();//TypeError: Error #1009: Cannot access a property or method of a null object reference.
			}
			else
			{
				trace("unmuted");
				var bgChannel:SoundChannel;
				var bgm:Sound = new bgmusic  ;
				bgChannel = bgm.play();
			}
		}
	}

}

however bgchannel.stop() TypeError: Error #1009: Cannot access a property or method of a null object reference. I dont quite understand why bgChannel would be null.

If I change it to


function togglemusic(e:Event)
		{
			if (mute1 == true)
			{
				trace("muted");
				bgChannel.stop();
			}
			else
			{
				trace("unmuted");
				var bgChannel:SoundChannel;
				var bgm:Sound = new bgmusic  ;
				bgChannel = bgm.play();
			}
		}

then it says that there is an incorrect amount of arguments (expecting 1) when It is called upon on the M button press

 
Flag Post

You have a scope issue. You need to declare bgChannel OUTSIDE of your functions so it exists to be referenced by your functions:


public class soundButton extends MovieClip
	{
		var mreleased:Boolean = false;
		var mute1:Boolean = true;
                ______________________________  < ---- here


		function soundButton()
		{

 
Flag Post

The soundButton function loads everything similar to the onload function in as2, so it is technically still accessible by everything since it is loaded in right? I tried your suggestion anyways though,


	public class soundButton extends MovieClip
	{
		var mreleased:Boolean = false;
		var mute1:Boolean = true;
		var bgChannel:SoundChannel;
		function soundButton()
		{

still gives me the exact same error. I have a feeling if I had the e:event in the togglemusic function it would work, I just cant figure out what it means by invalid amount of arguments detected, expecting 1. Let me rephrase that, I get what it means, I dont see where the problem is though.

edit
Ive set it to togglemusic(null) and it gets rid of the incorrect amount of arguments, but it still gives the same error with the bgChannel

 
Flag Post

assuming that bgmusic is linked correctly, this should work:


package 
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.events.MouseEvent;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.ui.Keyboard;
	public class soundButton extends MovieClip
	{
		
	
		
		var bgChannel:SoundChannel = new SoundChannel();
		var bgm:Sound = new bgmusic()  ;
		
		var mreleased:Boolean = false;
		var mute1:Boolean = true;
		
		
		function soundButton()
		{

			
			
			bgChannel = sound.play();
			addEventListener(Event.ENTER_FRAME, soundcontrol);


		}
		
		function soundcontrol(e:Event)
		{

			if (Key.isDown(77))
			{
				if (mreleased)
				{
					togglemusic();
					mute1 = ! mute1;
					this.nextFrame();
				}
				mreleased = false;
			}
			else
			{
				mreleased = true;

			}
		}
		
		
		function togglemusic()
		{
			if (mute1 == true)
			{
				trace("muted");
				bgChannel.stop();//TypeError: Error #1009: Cannot access a property or method of a null object reference.
			}
			else
			{
				trace("unmuted");
				
				
				bgChannel = sound.play();
			}
		}
	}

}

Wrong about the accessibility of variables declared in the constructor functions. They are function variables ONLY. They exist while that function is being run, as soon as your class is constructed it no longer has a reference to bgChannel unless it is declared OUTSIDE of the function, after your class declaration. Variables declared here are class variables and are accessible to all functions within this class. Hope this helps.

 
Flag Post

The only thing wrong with your code was bgChannel = sound.play(); should be bgChannel = bgm.play(); ( as far as making the game run). But I still get the same error for the same error at bgChannel being undefined.

http://i.imgur.com/xPRIw.png is a pic showing bgmusic being linked (if I understand what you are talking about). I believe I did it all correctly.

 
Flag Post

Holy crap I think I fixed it. In the togglemusic section, I just redeclared bgChannel so it looks like this


function togglemusic()
		{
			if (mute1 == true)
			{
				trace("muted");
				var bgChannel:SoundChannel = new SoundChannel();
				bgChannel.stop();
			}
			else
			{
				trace("unmuted");
				
				
				bgChannel = bgm.play();
			}
		}

My speakers are broken, so I cant test to see if the sound is actually getting muted or not until tuesday when a new set comes in. Would anyone be willing to test it?

http://www.fastswf.com/g8ierBI

hit M to see if it mutes. It being muted should correspond with the icon at the top right getting a cross through it. I know its not a perfectly looping sound by any means, I just wanted to get it working before I messed with it some more.

 
Flag Post

Had someone test it for me and said it didnt mute, but instead kept adding the music on every second press. Looked around, and finally figured something out.

import flash.media.SoundMixer;

then instead of bgChannel.stop(); I put in SoundMixer.stopAll(); and that did it. It really is only useful for a mute thing, but thats all Im planning on using it for anyways so it works perfectly.