Friday, 4 April 2014

So close

This code was so close to what I needed, what this does is it autoplays the song when the page opens and when you click on the pause button it pauses. However I do not want this autoplay functionality. I want the sound to play when you click the play button. Good to know for the future though.


import flash.media.SoundChannel;
import flash.events.MouseEvent;
import flash.events.Event;

var isPlaying:Boolean = true;
var lastposition:Number = 0;

var mysound:music = new music();

var soundchannel:SoundChannel = new SoundChannel ();

soundchannel = mysound.play(0,0);

soundchannel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

function onPlaybackComplete(event:Event):void
{
lastposition = 0;
soundchannel.stop();
btn.btn_pause.visible = false;
trace('finished');
isPlaying=false;

}

btn.addEventListener(MouseEvent.CLICK, playsound);
function playsound(event:MouseEvent):void
{

if(! isPlaying)
{
soundchannel = mysound.play(lastposition,0);
btn.btn_pause.visible = true;
isPlaying = true;
}

else
{
lastposition = soundchannel.position;
soundchannel.stop();
btn.btn_pause.visible = false;
trace(lastposition.toFixed(0), mysound.length.toFixed
(0));
isPlaying = false;
}

soundchannel.addEventListener(Event.SOUND_COMPLETE,
onPlaybackComplete);

function onPlaybackComplete(event:Event):void
{
lastposition = 0;
soundchannel.stop();
btn.btn_pause.visible = false;
trace('finished');
isPlaying=false;
}
}

No comments:

Post a Comment