Jump to content
Search Community

[SOLVED]TypeError: Error #1034 in MP3 code.

UbiAssassin test
Moderator Tag

Recommended Posts

I received this error when trying to make a PauseButton work with my cycling MP3's.

TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::MouseEvent@2b17b8e1 to com.greensock.events.LoaderEvent.

 

My code is working as I want it to work (First song plays, waits, second song plays, waits, do it again on and on), but I would also like the PauseButton functionality to work as well. Everytime I click on the PauseButton I get the error. I am very new to AS3, so it is probably a simple thing that I have done wrong. Happy Holidays everyone!!!

 

Code:

 

package
{
import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

public class MainSound extends MovieClip
{

	//private var song1:MP3Loader = new MP3Loader("http://levelforge.com/08 Hunted (Instrumental).mp3", { name: "Hunted", autoPlay: false, volume: .35, estimatedBytes: 9500 } );
	//private var song2:MP3Loader = new MP3Loader("http://levelforge.com/WITHOUT INST.mp3", { name: "Without", autoPlay: false, volume: .35, estimatedBytes: 9500 } );
	//private var queue:LoaderMax = new LoaderMax( { name: "mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler } );

	public function MainSound()
	{
		var pauseButton:NavButtonScript;
		var song1:MP3Loader = new MP3Loader("http://levelforge.com/08 Hunted (Instrumental).mp3", {name: "Hunted", autoPlay: true, volume: .35, estimatedBytes: 9500});
		var song2:MP3Loader = new MP3Loader("http://levelforge.com/WITHOUT INST.mp3", {name: "Without", autoPlay: false, volume: .35, estimatedBytes: 9500});
		var queue:LoaderMax = new LoaderMax({onComplete: init});

		queue.append(song1);
		queue.append(song2);
		queue.load();
		playSong1();
		song1.addEventListener(Event.SOUND_COMPLETE, done);
		song2.addEventListener(Event.SOUND_COMPLETE, done2);

		pauseButton = new NavButtonScript();
		pauseButton.x = 28;
		pauseButton.y = 115;
		pauseButton.setLabel("Pause");
		addChild(pauseButton);
		pauseButton.addEventListener(MouseEvent.CLICK, toggleSong);

		function toggleSong(e:LoaderEvent):void
		{
			song1.soundPaused = !song1.soundPaused;
		}
		function init(e:LoaderEvent):void
		{
			trace("soundsComplete");
			pauseButton.addEventListener(MouseEvent.CLICK, startSequence);
		}
		function startSequence(e:LoaderEvent):void
		{
			TweenLite.killTweensOf(playSong2);
			song2.pauseSound();
			song1.gotoSoundTime(0, true);
		}
		function done(e:Event):void
		{
			trace("done playing song1 now wait");
			TweenLite.delayedCall(1, playSong2);
		}
		function done2(e:Event):void
		{
			trace("done playing song2 now wait");
			TweenLite.delayedCall(1, playSong1);
		}
		function playSong1():void
		{
			trace("Let's play song1");
			song1.gotoSoundTime(0, true);
			song1.playSound();
		}

		function playSong2():void
		{
			trace("let's play the next song");
			song2.gotoSoundTime(0, true);
			song2.playSound();
		}

	}
}
}

 

Your tools and forums are awesome btw! Thanks for putting these amazing tools out there for us!

Link to comment
Share on other sites

Update:

 

I managed to get the first song to pause, even though I still get this error in my Flash Output window:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::MouseEvent@2bf218e1 to com.greensock.events.LoaderEvent.

 

Code Update:

 

package
{
import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

public class MainSound extends MovieClip
{
	private var pauseButton:NavButtonScript;

	public function MainSound()
	{
		var song1:MP3Loader = new MP3Loader("http://levelforge.com/08 Hunted (Instrumental).mp3", {name: "Hunted", autoPlay: true, volume: .35, estimatedBytes: 9500});
		var song2:MP3Loader = new MP3Loader("http://levelforge.com/WITHOUT INST.mp3", {name: "Without", autoPlay: false, volume: .35, estimatedBytes: 9500});
		var queue:LoaderMax = new LoaderMax({onComplete: init});

		queue.append(song1);
		queue.append(song2);
		queue.load();
		playSong1();
		song1.addEventListener(Event.SOUND_COMPLETE, done);
		song2.addEventListener(Event.SOUND_COMPLETE, done2);

		pauseButton = new NavButtonScript();
		pauseButton.x = 28;
		pauseButton.y = 115;
		pauseButton.setLabel("Pause");
		addChild(pauseButton);
		pauseButton.addEventListener(MouseEvent.CLICK, pauseSong);

		function pauseSong(e:MouseEvent):void
		{
			song1.soundPaused = !song1.soundPaused;
		}			
                                               function init(e:LoaderEvent):void
		{
			trace("soundsComplete");
			pauseButton.addEventListener(MouseEvent.CLICK, startSequence);
		}
		function startSequence(e:LoaderEvent):void
		{
			TweenLite.killTweensOf(playSong2);
			song2.pauseSound();
			song1.gotoSoundTime(0, true);
		}
		function done(e:Event):void
		{
			trace("done playing song1 now wait");
			TweenLite.delayedCall(1, playSong2);
		}
		function done2(e:Event):void
		{
			trace("done playing song2 now wait");
			TweenLite.delayedCall(1, playSong1);
		}
		function playSong1():void
		{
			trace("Let's play song1");
			song1.gotoSoundTime(0, true);
			song1.playSound();
		}

		function playSong2():void
		{
			trace("let's play the next song");
			song2.gotoSoundTime(0, true);
			song2.playSound();
		}

	}
}
}

 

The problem with my current setup is that I can pause the first song, but I cannot pause the second song. If I add this code:

 

function pauseSong(e:MouseEvent):void
{
song1.soundPaused = !song1.soundPaused;
               song2.soundPaused = !song2.SoundPaused;
}

 

It will pause the first song, start the second. Hit the button again it pauses the second song and starts the first.

Link to comment
Share on other sites

i see a few problems.

 

first the error:

 

function init(e:LoaderEvent):void
        {
           trace("soundsComplete");

//you are setting an EventListener for a MouseEvent


        pauseButton.addEventListener(MouseEvent.CLICK, startSequence);
        }


// this next function is EXPECTING a LoaderEvent -- Change it to MouseEvent


 function startSequence(e:LoaderEvent):void
        {
           TweenLite.killTweensOf(playSong2);
           song2.pauseSound();
           song1.gotoSoundTime(0, true);
        }

if you fix the error, your pause button may start to work as expected.

i can't really follow where you are as you state that the pause button doesn't work, but then you said that

song1.soundPaused = !song1.soundPaused;
song2.soundPaused = !song2.SoundPaused;

 

this part is working as expected.

 

also I noticed that you define ALL your functions nested INSIDE of mainSound()

public function MainSound(){
//some vars and function calls
function (){}
function (){}
}

 

but you probably want:

 

public function MainSound(){
//some vars and function calls
}

function (){}
function (){}

 

 

once your error is fixed, let us know how the pause function is working or not working.

Link to comment
Share on other sites

Excellent. Thank you for the fast response. I am sorry about the misleading post. The pause button is actually working, but only if I list it to pause song1. If I list it to pause both song1 and song2, it will pause one and start the other instead of pausing all sound.

 

I will make those changes that you listed, and I will probably no longer have this issue :). I really appreciate the help!

Link to comment
Share on other sites

Thank you for finding those errors for me and giving me advice. I have managed to set up my code to allow for two songs to play one after another infinitely, pause either one and unpause them at any time, stop them, and restart the entire sequence. The next thing I will be looking into is a way to control the volume with a slider of some sort. I am really a newbie to all of this, but I am really enjoying AS3 and especially the Greensock functionality. I am having a ball re-building my website from the ground up. Thanks for all of of the hard work that you guys have put into Greensock! Here is my working code:

 

package
{
import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

public class MainSound extends MovieClip
{
	// ***Site music variables.***

	private var pauseButton:NavButtonScript;
	private var stopButton:NavButtonScript;
	private var playButton:NavButtonScript;
	private var song1:MP3Loader = new MP3Loader("http://levelforge.com/08 Hunted (Instrumental).mp3", {name: "Hunted", autoPlay: false, volume: .35, estimatedBytes: 9500});
	private var song2:MP3Loader = new MP3Loader("http://levelforge.com/WITHOUT INST.mp3", {name: "Without", autoPlay: false, volume: .35, estimatedBytes: 9500});
	private var queue:LoaderMax = new LoaderMax({onComplete: init});
	private var queue2:LoaderMax = new LoaderMax({onComplete: init2});

	public function MainSound()
	{

		queue.append(song1);
		queue.load();
		playSong1();
		song1.addEventListener(Event.SOUND_COMPLETE, done);
		song2.addEventListener(Event.SOUND_COMPLETE, done2);

		// *** Music buttons.***

		pauseButton = new NavButtonScript();
		pauseButton.x = 28;
		pauseButton.y = 115;
		pauseButton.setLabel("Pause");
		addChild(pauseButton);
		TweenMax.to(pauseButton, 1, {x: 38, y: 200});
		pauseButton.addEventListener(MouseEvent.CLICK, pauseSong1);

		stopButton = new NavButtonScript();
		stopButton.x = 28;
		stopButton.y = 115;
		stopButton.setLabel("Stop");
		addChild(stopButton);
		TweenMax.to(stopButton, 1, {x: 38, y: 250});
		stopButton.addEventListener(MouseEvent.CLICK, stopAllSongs);

		playButton = new NavButtonScript();
		playButton.x = 28;
		playButton.y = 115;
		playButton.setLabel("Play");
		addChild(playButton);
		TweenMax.to(playButton, 1, {x: 38, y: 300});
	}

	// *** Music control functions.***

	function pauseSong1(e:MouseEvent):void
	{
		song1.soundPaused = !song1.soundPaused;
	}

	function pauseSong2(e:MouseEvent):void
	{
		song2.soundPaused = !song2.soundPaused;
	}

	function init(e:LoaderEvent):void
	{
		trace("sound1Complete");
		pauseButton.addEventListener(MouseEvent.CLICK, startSequence);
	}

	function init2(e:LoaderEvent):void
	{
		trace("sound2Complete");
		pauseButton.addEventListener(MouseEvent.CLICK, startSequence2);
	}

	function startSequence(e:MouseEvent):void
	{
		TweenLite.killTweensOf(playSong2);
		song2.pauseSound();
	}

	function startSequence2(e:MouseEvent):void
	{
		TweenLite.killTweensOf(playSong1);
		song1.pauseSound();
	}

	// ***Functions for Switching Songs.***

	function done(e:Event):void
	{
		trace("done playing song1 now wait");
		TweenLite.delayedCall(1, playSong2);
		queue2.append(song2);
		queue2.load();
		pauseButton.removeEventListener(MouseEvent.CLICK, startSequence);
		pauseButton.removeEventListener(MouseEvent.CLICK, pauseSong1);
		pauseButton.addEventListener(MouseEvent.CLICK, pauseSong2);
	}

	function done2(e:Event):void
	{
		trace("done playing song2 now wait");
		TweenLite.delayedCall(1, playSong1);
		queue.append(song1);
		queue.load();
		pauseButton.removeEventListener(MouseEvent.CLICK, startSequence2);
		pauseButton.removeEventListener(MouseEvent.CLICK, pauseSong2);
		pauseButton.addEventListener(MouseEvent.CLICK, pauseSong1);
	}

	// ***Functions for playing Song1 and Song2.***

	function playSong1():void
	{
		trace("Let's play song1");
		song1.gotoSoundTime(0, true);
		song1.playSound();
	}

	function playSong2():void
	{
		trace("let's play the next song");
		song2.gotoSoundTime(0, true);
		song2.playSound();
	}

	// ***Functions to Stop and Restart the Music Sequence.***

	function stopAllSongs(e:MouseEvent):void
	{
		trace("All Songs Stop.");
		TweenLite.killTweensOf(playSong1);
		song1.pauseSound();
		TweenLite.killTweensOf(playSong2);
		song2.pauseSound();
		pauseButton.removeEventListener(MouseEvent.CLICK, pauseSong1);
		pauseButton.removeEventListener(MouseEvent.CLICK, pauseSong2);
		pauseButton.removeEventListener(MouseEvent.CLICK, startSequence);
		pauseButton.removeEventListener(MouseEvent.CLICK, startSequence2);
		playButton.addEventListener(MouseEvent.CLICK, restartAllSongs);
	}

	function restartAllSongs(e:MouseEvent):void
	{
		trace("Restart the Music!");
		playButton.removeEventListener(MouseEvent.CLICK, restartAllSongs);
		song1.gotoSoundTime(0, true);
		song1.playSound();
		pauseButton.addEventListener(MouseEvent.CLICK, pauseSong1);
	}
}
}

Link to comment
Share on other sites

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