Jump to content


Photo
- - - - -

Preloader + External Sound File in ActionScript 3


  • Please log in to reply
No replies to this topic

#1 Mighty Tom

Mighty Tom

    Forum Newcomer

  • Members
  • Pip
  • 3 posts
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 15 May 2009 - 08:30 PM

I've got this single-button Flash interface for playing a sound file. It's designed to be used multiple times on the same page, with the external location of the sound file being specified in a flashvar. Trouble is, it doesn't play anything until the sound file is completely loaded and there's nothing to clue the user in on that fact. I worked through a preloader tutorial, but it only tracks the loading progress of the Flash movie itself, rather than anything it's pulling in externally. How do I get it to listen to the loading progress of the sound file instead of the movie?

Here's my code:

// ActionScript 3.0

// Preloader:

this.addEventListener(Event.ENTER_FRAME, loading);

function loading(e:Event):void {
	var total:Number = this.stage.loaderInfo.bytesTotal;
	var loaded:Number = this.stage.loaderInfo.bytesLoaded;
	
	bar_mc.scaleX = loaded/total;
//	loader_txt.text = Math.floor((loaded/total)*100)+"%";
	
	if (total == loaded) {
		play();
		this.removeEventListener(Event.ENTER_FRAME, loading);
	}
}



// Button:

//Declared Vars
var playStatus:Boolean = false
var url:String;
var sound:Sound = new Sound();
var channel:SoundChannel;
var positionTimer:Timer;
var request:URLRequest;

//inti get the FlashVar
function inti():void {
	try {
		var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters; // this gets all the FlashVars
		var fileUrl = paramObj.fileUrl; // the name of the Flashvar in this example is fileUrl the says get the value of fileUrl from the flashvars
		
		//Check to see if the Flash var has returned a value for fileUrl
		if(fileUrl != null && fileUrl != ""){
			url = fileUrl;
			request = new URLRequest(url);
			runApp();
		} else {
			trace("No value for fileUrl");
		}
	} catch (error:Error) {
		trace("There was an error loading flashvars.");
	}
}

//Run App
function runApp():void{

	sound.addEventListener(Event.COMPLETE, completeHandler);
	sound.addEventListener(Event.ID3, id3Handler);
	sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
	sound.addEventListener(ProgressEvent.PROGRESS, progressHandler);
		
	try {
		sound.load(request);
	} catch (error:Error) {
		trace("Failed to load audio file");
	}
}

function positionTimerHandler(event:TimerEvent):void {
	trace("positionTimerHandler: " + channel.position.toFixed(2));
}

function completeHandler(event:Event):void {
	trace("completeHandler: " + event);
	bigRed.addEventListener(MouseEvent.CLICK, clickHandler);
	
	channel = sound.play();
	channel.stop();
	
	positionTimer = new Timer(1000);
	positionTimer.addEventListener(TimerEvent.TIMER, positionTimerHandler);
	positionTimer.start();
}

function id3Handler(event:Event):void {
	trace("id3Handler: " + event);
	trace(event.currentTarget.id3.comment);
	trace(event.currentTarget.id3.album);
	trace(event.currentTarget.id3.genre);
	trace(event.currentTarget.id3.songName);
	trace(event.currentTarget.id3.artist);
	trace(event.currentTarget.id3.track);
	trace(event.currentTarget.id3.year);
	//text_txt.text = event.currentTarget.id3.songName;
}

function ioErrorHandler(event:Event):void {
	trace("ioErrorHandler: " + event);
	positionTimer.stop();
}

function progressHandler(event:ProgressEvent):void {
	trace("progressHandler: " + event);
}

function soundCompleteHandler(event:Event):void {
	trace("soundCompleteHandler: " + event);
	playStatus = false;
	channel.stop();
	channel = sound.play();
	channel.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
	channel.stop();	
}

function clickHandler(e:MouseEvent):void {
	var pausePosition:int = channel.position;
	if (playStatus == false) {
		playStatus = true;
		channel = sound.play(pausePosition);
		channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
	} else {
		playStatus = false;
		channel.stop();
		channel = sound.play();
		channel.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
		channel.stop();	
	}
	trace("playStatus " + playStatus);
}

inti();





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users