Muting a streaming audio track

This is probably a newbie question, as I’m on exactly my 4th day of working with Flash MX 2004. So if I’m hopelessly deluded as to the best way to do what I’m attempting, don’t hesitate to tell me (better for me to learn now than later).

The idea is simple: to have a streaming track of music that plays behind the animation, but can be muted (and possibly unmuted) via a button.

I did the following steps:

[list=1]
[]Added the MP3 file in question to the Library
[
]Right clicked on the MP3 in the Library, selected “Linkage…”, and set it to be exported for AS and in the 1st frame, and gave it the identifier “previewaudio”.
[]Created an empty layer in the animation and selected it
[
]Dragged the MP3 from the Library onto the Stage
[]In Properties for that layer, set the Sync type to “Stream”
[
]Created a button on its own layer, in accordance with the MX help file (changed it to a button symbol, added a simple mouseover animation, etc)
[*]Selected the button and added the following ActionScript for it:
[/list]


 on (release) {
     _root.previewaudio.stop();
 }
 

The Mute button does nothing to stop the audio after all of this. (It does stop the audio if I use a “Stop All Audio” behavior instead of the script, but I don’t want to do that because I’d prefer the option of being able to start the audio again later.)

Am I misunderstanding something about how the Identifier field works? If so, how should I use AS to “get at” a streaming audio track that has been added to an animation in this fashion? I’ve read things about creating a sound object entirely through AS instead, but my concern with that is it looks to me like the only way to stream audio that way is to take it from an external file on the web server, and the web hosting provider where I’d like to put up this Flash animation doesn’t support streaming MP3 files.

Any efforts to clear away some of the Flash-induced smoke in my eyes would be vastly appreciated!

  • David

Hi welcome to the forums.

What I would do is check out http://kennybellew.com for a complete guide.

However here’s some code that will help.

Place this in an empty frame on your time line.


mySound = new Sound();
mySound.loadSound("http://yourpath.com/file.mp3, true); // true is to stream

Now to mute the sound you cna use setVolume();


on(press){
mySound.setVolume(0);
}

Of course that goes on a button to mute the sound.

Hope that helps.

Actually I’ve already been there. A great site, to be sure.


  mySound = new Sound();
  mySound.loadSound("http://yourpath.com/file.mp3, true); // true is to stream
  

Well, the above demonstrates my exact concern with this method, you see. This approach seems to rely on the web server to actually stream the MP3, since it is an external file and the streaming request is coming from the client browser (where Flash is running). But my web hosting provider won’t stream MP3s. (Although I haven’t tried it, I also doubt that this will result in the audio being synced to the movie, which I would much prefer in my current case.)

Does this mean that there is no way to programmatically access an audio object that has been dragged into a Flash scene from the Library rather than loaded from an external file? That seems unlikely to me, given the many Flash animations I’ve seen that seem to have embedded, dynamic audio.

Well think about what your saying…

You want to stream an audio track to go with an animation. Now of course if the animation is already downloaded it wil play with or without the audio being downloaded.

How big is your mp3? Maybe you could preload the animation, then preload the audio, then start.

You can attach the mp3 in flash itself, but I have never done it like that, only back in the flash 5 days. You can always do that and just preload everything, but file size will most likely become a major issue.

have you tryed searching the flash forums? Maybe others have asked this question as well.

Hmm. That doesn’t seem to correspond with what I’ve seen thus far. When I drag the audio into a layer of the scene, I can set it to stream and still rely upon it to start and run in sync with the rest of the animation. I don’t know how Flash works it out internally, but I have done this a number of times, and over different connections, whether the SWF has been cached by the browser or not, and it is always in sync when done this way.

How big is your mp3? Maybe you could preload the animation, then preload the audio, then start.
It’s about 50 seconds in length. I’ve already compressed it to mono and 22Khz, but it is still about 180Kb. Combined with the bitmap graphics in the animation, it ends up being about 35 seconds of loading time before the animation starts for dialup users. I.e., too long, even if I were to add a preloader. (The total size of the SWF ends up around 260Kb.)

You can attach the mp3 in flash itself, but I have never done it like that, only back in the flash 5 days. You can always do that and just preload everything, but file size will most likely become a major issue.
When I attach the MP3 directly in the Flash stage and set the object to be streaming, the same animation’s startup time for dialup users is more like 5 seconds, which is acceptable. (Note that the size of the generated SWF does not change very much, so the audio is still technically “embedded”). What I’ve taken away from that is that the Flash player does its own streaming of audio that is embedded in an SWF file (when the streaming option is selected). Now, if the Flash player can also “emulate” streaming of externally loaded MP3 files, even if the host does not stream MP3s, that would be something useful to know. If it can also preserve the desired synchronization of such audio, that would be most unexpected and quite beyond wonderful.

have you tryed searching the flash forums? Maybe others have asked this question as well.
Yep, I have. In addition to text searching, I’ve also visually scanned every thread on the first 20 pages or so of the forum (that’s how I found Kenny’s site, by the way).

(Thanks muchly for taking time to express your thoughts on this, by the way.)

  • David

Ok I looked into this and since your using Flash MX2004 you can make your life easier by using “Behavior’s”

First import your sound to your library.
Here’s what you do:

  1. Open your flash document
  2. Window >> Development Panel >> Behavior’s (Shift + F3)
  3. Click on the + >> Sound >> Load Sound From Library
  4. Enter the linkage name “previewaudio”
  5. Give the sound an Instance of “mySound”

you cannot access the vloume of your sound by using


on(press){
mySound.setVolume(20);
}

Also now the mp3 is in the library.

Hope that helps :slight_smile:

Thank you for the response and for your efforts in looking into this further. It was very helpful, although your latest solution still didn’t seem to actually work for me as such. :slight_smile: I don’t know exactly why … it certainly reads like it ought to provide an instance name for the audio track for use in AS, but even after doing so, no such name appears and hence I couldn’t write any valid AS code using it.

However, your suggestion (in combination with things I read at Kenny’s site) sent me down a path that seems to have worked for me. I created a separate layer for actions, and in it created a global Sound() object at the first frame with no specific sound attached to it, in order to control the volume of all sounds in the animation. (I’d tried this yesterday and couldn’t get it to work, but I must have done something wrong.) This worked with the embedded audio to control the volume, but interestingly, I then found that the embedded audio would not stream anymore, even though I had streaming selected. I finally got it to stream again by removing the linkage I had earlier created in the Library (the one where I exported it for AS and on the first frame … I wonder if it was the latter item that caused the streaming to not work?).

Anyway, once I removed the export options, I was able to drag the audio onto a track, set it to streaming, and then control it with the following global code:


global_sound = new Sound();
global_sound.setVolume(80);
global_muted = false;

this.mute_btn.onPress = function() {
	if (global_muted) {
		global_sound.setVolume(80);
		global_muted = false;
	}
	else {
		global_sound.setVolume(0);
		global_muted = true;
	}
}

Pretty simple, really, once I found the actual route that MX seemed to want me to use for the task.

Thanks again. I’ll continue to be interested in hearing any further comments or conclusions you’d like to make about what I’ve done, as I’m obviously very much in a learning phase with Flash MX right now.

  • David

Odd it didn’t work for you, I’ll check out the file again later on and upload you a working copy, because it’s very simple and usefull.

See I’m not an animator, so when I program sound in flash, I don’t usually have to worry about the issue’s you did.

Anyway glad it’s working, and I’ll prob repost later with some other thoughts, as i’m about to run out now.