Skip to main content
February 16, 2013
Answered

looping sound problem

  • February 16, 2013
  • 1 reply
  • 666 views

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);

           

           

        }

}

This topic has been closed for replies.
Correct answer Nabren

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

1 reply

Nabren
Inspiring
February 16, 2013

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

February 16, 2013

Thank you

I dont have pakage so i cant add  public function

Nabren
NabrenCorrect answer
Inspiring
February 16, 2013

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