I'm lost on this and am going to lose the account if I can't find a solution!!! I started a Flash website for a band. They have several samples of music from their CD which they want to play on the website. I've got them to play as button events but that's a problem in itself! They keep overlapping as I can't get one instance to stop and start another. I can get them to play a single time, so if someone selects a sample, they'll just have to let it play through and then select a second one...I can live with that but would prefer the samples to stop playing when another is selected.

Here's my biggest problem though; there's a background sound that plays when the swf file is loaded and repeats itself indefinitely. This is what the client wants. I want the bg music to stop playing when a sample from the CD is played but I can't, for the life of me, get it stop playing when the sample is selected! I'm trying it as actionscript but not even sure if that is correct, I again can get one to play but it won't stop anything else from playing so everything just overlaps everything else!

Can I control a timeline sound (i.e. a background sound) with a button sound or event?
Here's the sample of a single button:

on (release) {
var mySound:Sound= new Sound();
mySound.loadSound("Judge Judy (1m 26s).mp3" , true);
mySound.onLoad = function() {
mySound.start();}
}

How do I stop the background sound when this button is selected? If I duplicate the button instance but give it a new name, can I then stop it somehow when another is selected?

Sorry, I'm really a HTML/ASP designer and this flash stuff is really foreign to me!

Dani AI

Generated

Good catch — the tutorial helps. For : the reason things overlap is that each click creates a new Sound instance while your background sound is placed on the timeline (timeline sounds are not easily controlled from button code). The simplest, most reliable fix in AS2 is to manage the background and samples with Sound objects (or with library linkage + attachSound) and to reuse a single sample player so you stop any playing sample before starting the next one.

The basic pattern (AS2) is: create one background Sound and one sample Sound on the main timeline, stop the background before you play a sample, and restart the background when the sample finishes. Example:

var bgSound:Sound = new Sound();
bgSound.loadSound("bg.mp3", true);
bgSound.start(0, 9999); // loop background

var samplePlayer:Sound = new Sound();

function playSample(url:String) {
    samplePlayer.stop();     // stop any current sample
    bgSound.stop();          // silence background
    samplePlayer.loadSound(url, true);
    samplePlayer.start(0, 1); // play once
    samplePlayer.onSoundComplete = function() {
        bgSound.start(0, 9999); // resume background when done
    };
}

// example button hookup
btn1.onRelease = function() { playSample("sample1.mp3"); };

If your background is currently a frame sound, remove it from the timeline and either load it with a Sound object (above) or give the library sound a linkage ID and use attachSound("linkageID") so code can stop/start it. Do not create a new Sound inside each button handler — reuse samplePlayer so clicks do not spawn overlapping streams.

Troubleshooting notes: avoid odd characters/spaces in filenames or URL-encode them; use the loops argument in start() to control repeat; test in the Flash authoring player (not just the browser) while developing. Note: Flash Player has reached end-of-life (Dec 31, 2020), so for new projects move to HTML5/JS audio.

Recommended Answers

All 2 Replies

Here is nice tutorial

Great tut. I think I already found out some things on it that should def help. I appreciate the point in the right direction.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.