Copy link to clipboard
Copied
i have this code
when i run it ; it plays only one time
its background music and i need to play it ina loop
what i need to add to my code?
thank you
my code:
import flash.media.SoundChannel;
import flash.events.MouseEvent;
import flash.events.Event;
var isPlaying:Boolean = true;
var lastposition:Number = 0;
var mysound:mew = new mew();
var soundchannel:SoundChannel = new SoundChannel();//So now that we can play the sound, how do we stop it? You can NOT stop the sound using the Sound class (e.g. mysound.stop(); will not work). Another class is used for that - the SoundChannel class - which has a stop() method that will let you stop a sound's playback.
soundchannel = mysound.play(0,0);// playing sound in the channel
soundchannel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete1);
function onPlaybackComplete1(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;
soundchannel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete1);
}
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;
soundchannel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}
}
No sorry for the confusion I was pasting the API reference.
You just need to change this line in your code:
soundchannel = mysound.play(0, 0);// playing sound in the channel
to this:
soundchannel = mysound.play(0, int.MAX_VALUE);// playing sound in the channel
or anywhere where you call play() just pass int.MAX_VALUE for the second arg
Copy link to clipboard
Copied
public function play(startTime:Number = 0, loops:int = 0, sndTransform:flash.media:SoundTransform = null):SoundChannel
When you call play - just passs it a really big number for the loop count
soundchannel = mysound.play(0, int.MAX_VALUE);// playing sound in the channel
Copy link to clipboard
Copied
Thank you
I dont have pakage so i cant add public function
Copy link to clipboard
Copied
No sorry for the confusion I was pasting the API reference.
You just need to change this line in your code:
soundchannel = mysound.play(0, 0);// playing sound in the channel
to this:
soundchannel = mysound.play(0, int.MAX_VALUE);// playing sound in the channel
or anywhere where you call play() just pass int.MAX_VALUE for the second arg
Copy link to clipboard
Copied
Thank you very very much
it's ok now
Find more inspiration, events, and resources on the new Adobe Community
Explore Now