Home Tutorials Forums Articles Blogs Movies Library Employment Press
Home » Actionscripts library »  Sound Object

Next 5

A better control over sound Object

// an empty mc linkage must exist in library
_root.attachMovie( "empty", "empty", 100 )

function SoundObj()
{
	this.count = 0;
	this.addSound = addSound
}

function addSound(sound_id)
{
	_root.empty.duplicateMovieClip(sound_id,this.count);
	obj = _root[sound_id];
	obj.sound = new Sound(obj);
	obj.sound.attachSound(sound_id);
	this.count+=1;
}

XSound = new SoundObj();


// Usage
// XSound.addSound( linkage_name_of_the_sound )
// after that a new object is created names as the
// linkage_name_of_the_sound
// this objcet can accept diferents values
// for the ex: .setVolume(x)
// that only affects to it and not to
// all sounds
// 
XSound.addSound("track1");
XSound.addSound("track2");

// to start track1
// track1.sound.start()

Posted by: Pedro Aguilar | website http://www.xperiments.com
A fader prototype
// Credit to Michael Tapson (earlier post) for most of the development of this
// script.  I have modified it to be a part of the Sound object's prototype,
// and thus, it will work with any sound clip you have.


Sound.prototype.fadeup = function ( fadespeed, maxLevel ) {
        var SndIntervalID;
        clearInterval ( SndIntervalID );
        SndIntervalID = setInterval ( this, "upOne", fadespeed, maxLevel );
        
        // BUMP VOLUME UP
        Sound.prototype.upOne = function ( maxLevel ) {
                varSnd = this.getVolume();
                if ( varSnd < maxLevel ){
                        this.setVolume ( varSnd + 1 );
                }else{
                        clearInterval ( SndIntervalID );
                }
                updateAfterEvent();
        }
}

Sound.prototype.fadedown = function ( fadespeed, minLevel ) {
        var SndIntervalID;
        clearInterval ( SndIntervalID );
        SndIntervalID = setInterval ( this, "downOne", fadespeed, minLevel );
        
        // BUMP VOLUME DOWN
        Sound.prototype.downOne = function ( minLevel ) {
                varSnd = this.getVolume();
                if (varSnd > minLevel){
                        this.setVolume ( varSnd - 1 );
                }else{
                        clearInterval ( SndIntervalID );
                }
                updateAfterEvent();
        }
}

// Usage:
//      mySound.fadeDown(fadeSpeed, minLevel)
//      mysound.fadeUp(fadeSpeed, maxLevel)
//
// Parameters:
//        fadeSpeed - A number higher than 1 that determines the length of
//                    time in which the sound will fade in or out.  The
//                    higher the number, the longer the fade.
//        minLevel -  A number between 0 and 100.  This is the lowest volume
//                    to which the volume will fade.  (Only in fadeDown)
//        maxLevel -  A number between 0 and 100.  This is the highest volume
//                    to which the volume will fade. (Only in fadeUp)
//
// Example:
//        s = new Sound();
//        s.attachSound("linkedSound");
//        s.fadeDown(20, 0);

Posted by: J. Tomasino | website http://www.aloneone.com
Attach a sound to a certain event
You could use the sound object as follows:
1) Set your sound up in frame 1:
s = new Sound();
s.attachSound("buzzSound");
2) Place an action that test a condition on an MC:
onClipEvent (enterFrame) {
        if (_root.threeOfAKind==true) {
                _root.s.start();
        } else {
                _root.s.stop();
        }
}
3) Finally you'll have to set an action that sets 'threeOfAKind' to first true and then back to false. You do this because the action is place in an onClipEvent (enterFrame) so the test is re-initiated every frame. As an example:
on (press) {
        _root.threeOfAKind=true;
}
on (release) {
        _root.threeOfAKind=false;
}
If you can't manage it with an onClipEvent then place your actions in a control MC.
Posted by: No name | website http://
Attached sounds and the loadMovie action
Attached sounds do not play if the SWF containing the sound is loaded using the loadMovie action.
For example, the following ActionScript attaches and plays a sound asset which has been exported with the linkage name "mySound":
soundContainer = new Sound();
soundContainer.attachSound("mySound");
soundContainer.start();
This script will function properly except when the SWF containing it is loaded into another movie by a loadMovie action. In this case, the loaded movie loses it's connection to the linked sound.

Solution
In the original FLA file, add the keyword 'this' to the new Sound constructor, as follows:
soundContainer = new Sound(this);
soundContainer.attachSound("mySound");
soundContainer.start();
Export a new SWF with this structure. The script will now function properly, regardless of the movie's hierarchical position.
Posted by: No name | website http://
Control Sound with AS,2 text fields& a button:P
//s=your sound
s= new Sound(this);s.attachSound("yoursoundslinkagename");
//now, for the controling vars
_root.setPan(number);
_root.setVolume(number);
//now for using them-make a button
//put in button sctions:
on(press){
        s.setPan(Math.random()*100);
}
//sets random pan
//or
on(press){
        s.setVolume(Math.random()*100);
}
//sets random volume
//or both
on(press){
        s.setVolume(Math.random()*100);
        s.setPan(Math.random()*100);
}
//or to set it to a set number-make an input text field
//and give it the Var: "_root.txt"
on(press){
        s.setPan(_root.txt);
}
//or
on(press){
        s.setVolume(_root.txt);
}
//or, make 2 boxes of input text, and give one the Var:"_root.volume" and the other "_root.pan"
on(press){
        s.setPan(_root.pan);
        s.setVolume(_root.volume);
}
//thats all folks!

Posted by: Alcatraz5 | website http://www.newgrounds.com

Next 5

Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.