Jump to content
Search Community

Pausing Loaded MP3? [Solved: Scope Issue]

stevenp test
Moderator Tag

Recommended Posts

Nub of the gist: How can I access (and set) play position of an MP3 and duration of an MP3? I figure that ought to get me pointed to the right time zone from here.

 

I found a thread where Jack helped someone with his code to load video, start playing when X were loaded, and loop around again:

 

http://forums.greensock.com/topic/3072-video-loader-playlist/?hl=playlist

 

I modified that to work with MP3s, but I wanted to have button functionality. The first file plays, and I was able to get "next" functionality to work (using the existing code) for all five files. For the life of me, I have no clue how to pause a loaded sound. I have literally tried everything I could think of, to no avail. I literally had twenty tabs open looking at documentation and forum questions/answers.

 

I have built mp3 players (from tutorials) where I set intervals every X milliseconds to check for position, and then stopping the sound and starting at _that_ position. I can get my head around that. I clearly cannot get my head around MP3Loader and how it handles sound properties.

 

So, here goes. This is the code I have:

import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
import com.greensock.loading.*;
import com.greensock.loading.display.*;
import com.greensock.events.*;

var queue:LoaderMax;
var preloadCount:uint = 0;
var curSoundIndex:int = -1;
var mp3Array:Array = ["song01.mp3", "song02.mp3", "song03.mp3", "song04.mp3", "song05.mp3"  ];

initPreload();

function initPreload():void {
	queue = new LoaderMax({name:"mainQueue", onChildComplete:childCompleteHandler, maxConnections:1});
	for (var i:int = 0; i < mp3Array.length; i++) {
		queue.append(new MP3Loader(mp3Array[i], {name:"mp3"+i, autoPlay:false}));
	}
	queue.load();
}

// make sure one song is loaded before playing back
function childCompleteHandler(event:LoaderEvent):void {
	preloadCount++;
	if (preloadCount == 1) {
		playAudioFile(0);
	}
}

function playAudioFile(index:uint):void {
if (curSoundIndex != -1) {
		var old:MP3Loader = LoaderMax.getLoader(mp3Array[curSoundIndex]) as MP3Loader;
		old.removeEventListener(MP3Loader.SOUND_COMPLETE, soundCompleteHandler);
		TweenLite.to(old, 1, {volume:0, onComplete:old.pauseSound}); 
	}
	curSoundIndex = index;
	var sound:MP3Loader = LoaderMax.getLoader(mp3Array[index]) as MP3Loader;
	sound.addEventListener(MP3Loader.SOUND_COMPLETE, soundCompleteHandler);
	sound.gotoSoundTime(0, true);
	TweenLite.to(sound, 1, {volume:1});
}

function soundCompleteHandler(event:LoaderEvent):void {
	var nextIndex:int = curSoundIndex + 1;
	trace(nextIndex);
	if (nextIndex >= mp3Array.length) {
		nextIndex = 0;
	}
	playAudioFile(nextIndex);
}

// button logic
btn_play.addEventListener(MouseEvent.CLICK, functionPlayButton);
btn_pause.addEventListener(MouseEvent.CLICK, functionPauseButton);
btn_next.addEventListener(MouseEvent.CLICK, functionNextSongButton);
btn_resume.addEventListener(MouseEvent.CLICK, functionResumeSongButton);
btn_stop.addEventListener(MouseEvent.CLICK, functionStopButton);

function functionPlayButton(e:MouseEvent):void{
	trace("play button");
	}

function functionNextSongButton(e:MouseEvent):void{
	trace("next button");
	var nextIndex:int = curSoundIndex + 1;
	if (nextIndex >= mp3Array.length) {
		nextIndex = 0;
		}
		playAudioFile(nextIndex);
	}	

function functionPauseButton(e:MouseEvent):void{
	trace("pause button");
	}	

function functionResumeSongButton(e:MouseEvent):void{
	trace("resume button");
	}	

function functionStopButton(e:MouseEvent):void{
	trace("stop button");
	}	

I get the array, the index of the array, and how to cycle through the (in this instance) five MP3s. I haven't been able to plumb the depths of this package.

 

 

Link to comment
Share on other sites

For the life of me, I have no clue how to pause a loaded sound.

 

 

 

 

 

pauseSound() should do the trick http://api.greensock.com/as/com/greensock/loading/MP3Loader.html#pauseSound()

 

I have built mp3 players (from tutorials) where I set intervals every X milliseconds to check for position, and then stopping the sound and starting at _that_ position. 

 

 

MP3Loader's have a soundTime property which gives you the current playhead position in seconds: http://api.greensock.com/as/com/greensock/loading/MP3Loader.html#soundTime

Link to comment
Share on other sites

Thank you Carl.

 

Yes, that seemed to be what I should be calling. I tried putting that everywhere to no avail:

function functionPauseButton(e:MouseEvent):void{
    trace("pause button");
    sound.pauseSound();
    }    

 ... which yields:

 

1120: Access of undefined property sound.

 

 

If this is  a scope issue, my understanding is that I cannot declare global variables in AS3, so ... how do I cross this river?

 

I was able to load a single mp3 and play, pause, resume, and stop using SoundChannel (and no greensock classes), but Id really like to get my head around LoaderMax through MP3Loader if possible. How do I access soundPause (or soundTime and volume) globally?

 

 

Thanks and happy weekend,

Steve

Link to comment
Share on other sites

Yup, it looks very much like a scope issue.

If you declare a variable in a function you will not be able to access it from other functions.

 

The trick is just to declare the variable outside your function, but assign it a value from inside a function.

//declare variable outside any functions
var sound:MP3Loader;

function playAudioFile(index:uint):void {
if (curSoundIndex != -1) {
var old:MP3Loader = LoaderMax.getLoader(mp3Array[curSoundIndex]) as MP3Loader;
old.removeEventListener(MP3Loader.SOUND_COMPLETE, soundCompleteHandler);
TweenLite.to(old, 1, {volume:0, onComplete:old.pauseSound}); 
}
curSoundIndex = index;

//new DON'T use var here, just sound
sound = LoaderMax.getLoader(mp3Array[index]) as MP3Loader;

sound.addEventListener(MP3Loader.SOUND_COMPLETE, soundCompleteHandler);
sound.gotoSoundTime(0, true);
TweenLite.to(sound, 1, {volume:1});
}


function functionPauseButton(e:MouseEvent):void{
trace("pause button");
sound.pauseSound()
} 
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...