Hello, i’m getting stuck on the play pause. When i pause the first song it does it right, and play it again it works fine. When i click for the next song and pause again, and re-play it, it plays the previous song instead of the current song. I think i’m not storing my position variable correctly. Now I got really confused, here is my code… please help out.
thanks.
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.events.ErrorEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.media.SoundLoaderContext;
import flash.net.URLRequest;
import flash.utils.Timer;
import flash.net.URLLoader;
import flash.xml.XMLDocument;
import flash.geom.Rectangle;
import flash.filters.GlowFilter;
public class AudioPlayer extends MovieClip
{
private var getMusic:URLRequest;
private var music:Sound = new Sound();
private var soundChannel:SoundChannel;
private var currentSound:Sound = music;
private var currentIndex:Number = 0;
private var songPlaying:Boolean = false;
private var xml:XML;
private var songlist:XMLList;
private var loader:URLLoader = new URLLoader(); //create a new URLLoader Object
public function AudioPlayer():void {
//back and forward buttons
back_mc.addEventListener(MouseEvent.CLICK, back);
forward_mc.addEventListener(MouseEvent.CLICK, forward);
//play pause buttons
play_mc.addEventListener(MouseEvent.CLICK, playSong);
pause_mc.addEventListener(MouseEvent.CLICK, pauseSong);
play_mc.visible = false;
//playlist button
playlist_mc.addEventListener(MouseEvent.CLICK, playlist);
//load XML
loader.addEventListener(Event.COMPLETE, whenLoaded); //add an event listener to that object
loader.load(new URLRequest("songs.xml")); //Requests our xml file that contains our song data
}
public function whenLoaded(e:Event):void {
xml = new XML(e.target.data);
songlist = xml.song; //accesses our song tag in our xml file
getMusic = new URLRequest(songlist[0].url);//get music from songlist
music.load(getMusic);//load music
soundChannel = music.play();//plays the music
currSong_txt.text = songlist[0].title; //gets song name from xml
currBand_txt.text = songlist[0].artist; //gets artist name
soundChannel.addEventListener(Event.SOUND_COMPLETE, forward);//runs the next song function when a song completes
}
public function pauseSong(e:Event):void {
var pausePosition:int = soundChannel.position;
currentIndex = soundChannel.position;
soundChannel.stop();
songPlaying = false;
play_mc.visible = true;
pause_mc.visible = false;
trace("pause " + currentIndex)
}
public function playSong(e:Event):void {
var pausePosition:int = soundChannel.position;
soundChannel = music.play(pausePosition);
//soundChannel.soundTransform = songVolume;
songPlaying = true;
soundChannel.addEventListener(Event.SOUND_COMPLETE, forward);
play_mc.visible = false;
pause_mc.visible = true;
trace("play " + currentIndex)
}
public function back(e:Event):void {
if (currentIndex > 0)
{
currentIndex--;
}
else
{
currentIndex = songlist.length() - 1;
}
var nextReq:URLRequest = new URLRequest(songlist[currentIndex].url);
var prevTitle:Sound = new Sound(nextReq);
soundChannel.stop();
currSong_txt.text = songlist[currentIndex].title;
currBand_txt.text = songlist[currentIndex].artist;
soundChannel = prevTitle.play();
songPlaying = true;
currentSound = prevTitle;
soundChannel.addEventListener(Event.SOUND_COMPLETE, forward);
}
public function forward(e:Event):void {
if (currentIndex < (songlist.length() - 1))
{
currentIndex++;
}
else
{
currentIndex = 0;
}
var nextReq:URLRequest = new URLRequest(songlist[currentIndex].url);
var nextTitle:Sound = new Sound(nextReq);
soundChannel.stop();
currSong_txt.text = songlist[currentIndex].title;
currBand_txt.text = songlist[currentIndex].artist;
soundChannel = nextTitle.play();
songPlaying = true;
currentSound = nextTitle;
soundChannel.addEventListener(Event.SOUND_COMPLETE, forward);
}
public function playlist(e:Event):void {
//someFunction
}
}
}
sorry for the long lines of code, but i want to make sure to include everything.
i’m having trouble on the pause and play function! :stare: