Be a Supporter!

Audio Wont' Loop More Than Twice

  • 316 Views
  • 5 Replies
New Topic Respond to this Topic
blakemoso
blakemoso
  • Member since: Apr. 2, 2011
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Audio Wont' Loop More Than Twice May. 10th, 2012 @ 07:48 PM Reply

Im very new to using audio in my flash projects, and quite frankly I'm finding it dreadful!

My biggest problem right now is that I need my song to loop repeatedly... I got this code set up so far, which seems like it should work... And it sort of does... but only once, the song plays twice on repeat, then stops. How can I get it to keep going?

var track1:Sound = new mainTheme();
var channel1:SoundChannel = new SoundChannel();
channel1 = track1.play();

channel1.addEventListener(Event.SOUND_COMPLETE, loopTrack1);
function loopTrack1(e:Event):void{
    channel1 = track1.play();
}

Thanks!


BBS Signature
MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Audio Wont' Loop More Than Twice May. 10th, 2012 @ 09:43 PM Reply

Eh.

soundControl = sound.play(0, int.MAX_VALUE);

I know it's lazy, but there's a point where things aren't worth bothering with.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
Krazzygamer3y2
Krazzygamer3y2
  • Member since: Jan. 1, 2012
  • Offline.
Forum Stats
Member
Level 01
Animator
Response to Audio Wont' Loop More Than Twice May. 22nd, 2012 @ 03:30 AM Reply

Actually, put the sound on the specified frame & on the bottom are actions. If you click on the frame that has the sound on it. On the bottom shows a variety of options. Go to the box saying repeat & click it, there will be a loop command. Once you do that, put a stop command, test your movie & hey! Presto!!!

If it doesn't make sense. I'll explain again.

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Audio Wont' Loop More Than Twice May. 22nd, 2012 @ 11:26 AM Reply

At 5/22/12 03:30 AM, Krazzygamer3y2 wrote: Actually, put the sound on the specified frame & on the bottom are actions. If you click on the frame that has the sound on it. On the bottom shows a variety of options. Go to the box saying repeat & click it, there will be a loop command. Once you do that, put a stop command, test your movie & hey! Presto!!!

If it doesn't make sense. I'll explain again.

But then you can't start and stop the sound exactly when you need to, hence he's using code.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to Audio Wont' Loop More Than Twice May. 22nd, 2012 @ 11:50 PM Reply

I decided to give this a go, and as with you, my track was only looping once. Firstly from the initial call of my function that played the track, and secondly because of the listener. Removing and re-hooking the event listener did the trick.

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

var channel:SoundChannel = new SoundChannel();
var track:Sound = new myLoop();

playTrack();

function playTrack(e:Event = null):void
{
	if(channel.hasEventListener(Event.SOUND_COMPLETE))
		   channel.removeEventListener(Event.SOUND_COMPLETE, playTrack);

	channel = track.play();
	channel.addEventListener(Event.SOUND_COMPLETE, playTrack, false, 0, true);
}

Alternatively, if you don't need it looping indefinitely, you can also do:

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

var channel:SoundChannel = new SoundChannel();
var track:Sound = new myLoop();

channel = track.play(0, int.MAX_VALUE);

This will loop the track 2147483647 times, which, practically is indefinite. This is a cleaner and my preferred way of doing it, but either works fine.

blakemoso
blakemoso
  • Member since: Apr. 2, 2011
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Audio Wont' Loop More Than Twice May. 23rd, 2012 @ 03:23 AM Reply

Yes, I did end up using max value to get it to loop, as it does seem a lot simpler than pointlessly trying to get it to really loop infinitely!

Thanks to everyone for the help!


BBS Signature