Zurück   Flashforum > Flash > ActionScript > ActionScript 1

Antwort
 
LinkBack Themen-Optionen Ansicht
Alt 07-04-2005, 16:47   #1 (permalink)
// pagebuilder
 
Benutzerbild von cmike
 
Registriert seit: Apr 2002
Beiträge: 124
Exclamation SoundManager Klasse

Hi All

Ich poste an dieser stelle eine Soundmanager Klasse mit erweitertem Soundobjekt zur vereinfachten handhabung verschiedenster sounds und - jetzt kommts - SOUNDQUEUES!

Ich habe eine Weile nach einer solchen Klasse im Forum und im Internet gesucht, jedoch leider nichts brauchbares gefunden. Ich hoffe, dass auch andere diese Klasse verwenden können, um einfacher Sounds in ihren Flash-Projekten zu implementieren und zu verwalten.

Denkbare Anwendungsbereiche sind z.B. Player mit Playlisten (Erstellung der gesamten Playlist mit einem Befehl), Spiele mit Hintergrund- und Ereignissounds, vertonte Websites, usw.

Funktionsübersicht:

- Sounds und Soundqueues mit einem Befehl erstellbar
- Sounds werden automatisch im zentralen Soundmanager registriert
- Zugriff auf einzelne aber auch alle Sounds zusammen möglich
- Fadein und -out funktionen als methoden des Soundobjekts

Die Soundklasse ist noch etwas unausgereift, falls ihr demnach Anregungen oder Verbesserungen habt, lasst es mich wissen. Gerne investiere ich noch etwas Zeit, um den Code zu optimieren. Wenn jemand anderes lust hat, mir dabei zu helfen, wäre ich natürlich auch sehr dankbar

cheers

mike
Angehängte Dateien
Dateityp: zip SoundManager.zip (9,8 KB, 46x aufgerufen)
__________________
// ************************************
// progress means simplifying not complicating
// bkanal
cmike ist offline   Mit Zitat antworten
Alt 07-04-2005, 16:51   #2 (permalink)
// pagebuilder
 
Benutzerbild von cmike
 
Registriert seit: Apr 2002
Beiträge: 124
anschliessend noch den code der beiden klassen zum screenen:

ActionScript:
  1. class SoundManager {
  2.    
  3.     // Variable declaration
  4.    
  5.     var intFadeTime:Number;
  6.     var arrSoundObjects:Array;
  7.     var intIDcount:Number;
  8.     var strName:String;
  9.    
  10.     // FUNCTION: SoundObject
  11.     // constructor method -> setting default Values
  12.    
  13.     function SoundManager(strName) {
  14.         if (strName == null) { strName = random(99999) + getTimer(); }
  15.         this.strName = strName;
  16.         this.intIDcount = -1;
  17.         this.intFadeTime = 2000;
  18.         this.arrSoundObjects = new Array();
  19.     }
  20.    
  21.     // FUNCTION: addSound
  22.     // adds single sounds and sound queues to the library
  23.    
  24.     function addSound(objFiles) {
  25.         if (objFiles != null) {
  26.             this.intIDcount++;
  27.             _root.createEmptyMovieClip("_SOUND_" + this.strName + "_" + this.intIDcount, 100 + this.intIDcount);
  28.             this.arrSoundObjects[this.intIDcount]          = eval("_root._SOUND_" + this.strName + "_" + this.intIDcount);
  29.             this.arrSoundObjects[this.intIDcount].objSound = new SoundObject(this.arrSoundObjects[this.intIDcount]);
  30.             this.arrSoundObjects[this.intIDcount].objSound.newSound(objFiles);
  31.             this.arrSoundObjects[this.intIDcount].objSound.regID(this.intIDcount);
  32.             return this.arrSoundObjects[this.intIDcount].objSound;
  33.         } else {
  34.             trace("SOUND ERROR: parameter ojbFiles is undefined in SoundManager->addSound();");
  35.         }
  36.     }
  37.    
  38.     // FUNCTION: delSound
  39.     // delete sounds according to their ID
  40.    
  41.     function delSound(intID:Number) {
  42.         this.arrSoundObjects[intID].objSound.stop();
  43.         this.arrSoundObjects[intID].removeMovieClip();
  44.         delete this.arrSoundObjects[intID];
  45.         return true;
  46.     }
  47.    
  48.     // FUNCTION: fadeIn
  49.     // fade in all sounds registered in sound manager
  50.    
  51.     function fadeIn() {
  52.         var i;
  53.         for(i=0;i<this.arrSoundObjects.length;i++) {
  54.             this.arrSoundObjects[i].objSound.fade(true, false);
  55.         }
  56.     }
  57.    
  58.     // FUNCTION: fadeOut
  59.     // fade out all sounds registered in sound manager
  60.    
  61.     function fadeOut() {
  62.         var i;
  63.         for(i=0;i<this.arrSoundObjects.length;i++) {
  64.             this.arrSoundObjects[i].objSound.fade(false, true);
  65.         }
  66.     }
  67.    
  68.     // FUNCTION: stop
  69.     // stops all sounds
  70.    
  71.     function stop() {
  72.         var i;
  73.         for(i=0;i<this.arrSoundObjects.length;i++) {
  74.             this.arrSoundObjects[i].objSound.stop();
  75.         }
  76.     }
  77.    
  78.     // FUNCTION: play
  79.     // starts all sounds
  80.    
  81.     function play() {
  82.         var i;
  83.         for(i=0;i<this.arrSoundObjects.length;i++) {
  84.             this.arrSoundObjects[i].objSound.start();
  85.         }
  86.     }
  87.    
  88.     // FUNCTION: pause
  89.     // pause all sounds
  90.    
  91.     function pause() {
  92.         var i;
  93.         for(i=0;i<this.arrSoundObjects.length;i++) {
  94.             this.arrSoundObjects[i].objSound.pause();
  95.         }
  96.     }
  97.    
  98.     // FUNCTION: mute
  99.     // mute or demute all sounds registered in sound manager
  100.     // depending on current status
  101.    
  102.     function mute() {
  103.         var i;
  104.         for(i=0;i<this.arrSoundObjects.length;i++) {
  105.             this.arrSoundObjects[i].objSound.mute();
  106.         }
  107.     }
  108. }

und

ActionScript:
  1. class SoundObject extends Sound {
  2.    
  3.     // Variable declaration
  4.    
  5.     private var intVolumeMax:Number;
  6.     private var intVolumeMin:Number;
  7.     private var intFadeTime:Number;
  8.     private var intFadeID:Number;
  9.     private var intQueueID:Number;
  10.     private var intQueueTrack:Number;
  11.     private var intID:Number;
  12.    
  13.     private var bolFX:Boolean;
  14.     private var bolMute:Boolean;
  15.     private var bolFadeOngoing:Boolean;
  16.    
  17.     private var arrQueue:Array;
  18.    
  19.     public function onFadeComplete():Void { }
  20.    
  21.     // FUNCTION: SoundObject
  22.     // constructor method -> setting default Values
  23.    
  24.     function SoundObject() {
  25.         this.bolFX          = true;
  26.         this.arrQueue       = new Array();
  27.         this.intQueueTrack  = 1;
  28.         this.intVolumeMax   = 100;
  29.         this.intVolumeMin   = 0;
  30.         this.intFadeTime    = 3000;
  31.         this.bolFadeOngoing = false;
  32.         this.bolMute        = false;
  33.     }
  34.    
  35.     // FUNCTION: regID
  36.     // register SoundID in SoundObject for Access over Soundmanager
  37.    
  38.     function regID(ID) {
  39.         this.intID = ID;
  40.     }
  41.    
  42.     // FUNCTION: getID
  43.     // get SoundID for Access over Soundmanager
  44.    
  45.     function getID() {
  46.         return this.intID;
  47.     }
  48.    
  49.     // FUNCTION: setFX
  50.     // turn soundFX on or off
  51.    
  52.     function setFX(FX) {
  53.         this.bolFX = FX;
  54.     }
  55.    
  56.     // FUNCTION: getFX
  57.     // get soundFX status
  58.    
  59.     function getFX() {
  60.         return this.bolFX;
  61.     }
  62.    
  63.     // FUNCTION: observeQueue
  64.     // observe playing sound and fadeout before track ends
  65.    
  66.     function observeQueue() {
  67.         if (this.arrQueue.length > 1) {
  68.             if (this.position > this.duration - this.intFadeTime - 500 && this.position != this.duration) {
  69.                 clearInterval(this.intQueueID);
  70.                 this.fade(false);
  71.             }
  72.         } else {
  73.             clearInterval(this.intQueueID);
  74.         }
  75.     }
  76.  
  77.     // FUNCTION: setVolumeMax
  78.     // set the max Volume (default 100)
  79.    
  80.     function setVolumeMax(intVolume) {
  81.         this.intVolumeMax = intVolume;
  82.     }
  83.    
  84.     // FUNCTION: setVolumeMin
  85.     // set the min volume (default 0)
  86.    
  87.     function setVolumeMin(intVolume) {
  88.         this.intVolumeMin = intVolume;
  89.     }
  90.    
  91.     // FUNCTION: getVolumeMax()
  92.     // returns intVolumeMax
  93.    
  94.     function getVolumeMax() {
  95.         return this.intVolumeMax;
  96.     }
  97.    
  98.     // FUNCTION: getVolumeMin()
  99.     // returns intVolumeMin
  100.    
  101.     function getVolumeMin() {
  102.         return this.intVolumeMin;
  103.     }
  104.    
  105.     // FUNCTION: newSound
  106.     // adds a new sound or soundqueue (depending
  107.     // on input tpye) to the soundobject
  108.     // WARNING: overwrites old sounds in same
  109.     // object!
  110.    
  111.     function newSound(objFiles) {
  112.         this.arrQueue      = new Array();
  113.         this.intQueueTrack = 1;
  114.         if (objFiles instanceof Array) {
  115.             this.arrQueue        = objFiles;
  116.             this.onSoundComplete = function() {
  117.                 this.nextSound();
  118.             }
  119.         } else {
  120.             this.arrQueue[0]     = objFiles;
  121.             this.onSoundComplete = function() { }
  122.         }
  123.     }
  124.    
  125.     // FUNCTION: prevSound
  126.     // toggle previous soundtrack in queue
  127.    
  128.     function prevSound() {
  129.         if (0 < this.intQueueTrack) {
  130.             this.intQueueTrack--;
  131.         } else {
  132.             this.intQueueTrack = this.arrQueue.length;
  133.         }
  134.         this.play();
  135.         if (this.bolFX == true) {
  136.             this.setVolume(0);
  137.             this.fade(true);
  138.         }
  139.     }
  140.    
  141.     // FUNCTION: nextSound
  142.     // toggle next soundtrack in queue
  143.    
  144.     function nextSound() {
  145.         if (this.arrQueue.length > this.intQueueTrack) {
  146.             this.intQueueTrack++;
  147.         } else {
  148.             this.intQueueTrack = 1;
  149.         }
  150.         this.play();
  151.         if (this.bolFX == true) {
  152.             this.setVolume(0);
  153.             this.fade(true);
  154.         }
  155.     }
  156.    
  157.     // FUNCTION: play
  158.     // extended sound playing function
  159.    
  160.     function play(intOffset:Number, intRepeat:Number) {
  161.         if (intOffset == null) { intOffset = 0; }
  162.         if (intRepeat == null) { intRepeat = 1; }
  163.         this.start(0);
  164.         this.stop();
  165.         clearInterval(this.intQueueID);
  166.         this.intQueueID = setInterval(this, "observeQueue", 500);
  167.         this.loadSound(this.arrQueue[intQueueTrack-1], true);
  168.         this.start(intOffset, intRepeat);
  169.     }
  170.    
  171.     // FUNCTION: mute
  172.     // mute soundobject
  173.    
  174.     function mute(bolMute:Boolean) {
  175.         if (bolMute == null) {
  176.             if (this.bolMute == true && bolFadeOngoing != true) {
  177.                 this.bolMute = false;
  178.                 this.setVolume(this.intVolumeMax);
  179.             } else {
  180.                 this.bolMute = true;
  181.                 this.setVolume(0);
  182.             }
  183.         } else {
  184.             this.bolMute = bolMute;
  185.             if (this.bolFadeOngoing != true) {
  186.                 if (bolMute == true) {
  187.                     this.setVolume(0);
  188.                 } else {
  189.                     this.setVolume(this.intVolumeMax);
  190.                 }
  191.             }
  192.         }
  193.     }
  194.    
  195.     // FUNCTION: fadeIn
  196.     // fade in sound dynamically from zero
  197.    
  198.     function fadeIn() {
  199.         this.setVolume(0);
  200.         this.fade(true);
  201.     }
  202.    
  203.     // FUNCTION: fadeOut
  204.     // fade out sound dynamically from intVolumeMax
  205.    
  206.     function fadeOut() {
  207.         this.setVolume(this.intVolumeMax);
  208.         this.fade(false);
  209.     }
  210.    
  211.     // FUNCTION: setFadeTime
  212.     // set fading time
  213.    
  214.     function setFadeTime(intFadeTime:Number) {
  215.         this.intFadeTime = intFadeTime;
  216.     }
  217.    
  218.     // FUNCTION: getFadeTime
  219.     // get fading time back
  220.    
  221.     function getFadeTime() {
  222.         return this.intFadeTime;
  223.     }
  224.    
  225.     // FUNCTION: fade
  226.     // initialise fading process
  227.    
  228.     function fade(bolMode:Boolean, bolMute:Boolean, intTime:Number) {
  229.         if (this.bolMute != true || bolMute == false) {
  230.             this.bolFadeOngoing = true;
  231.             if (bolMute != null) {
  232.                 this.mute(bolMute);
  233.             }
  234.             if (intTime == null) {
  235.                 intTime = this.intFadeTime;
  236.             }
  237.             var intSteps   = Math.round(intTime / 50);
  238.             clearInterval(this.intFadeID);
  239.             this.intFadeID = setInterval(this, "aniFade", intSteps, bolMode);
  240.         }
  241.     }
  242.    
  243.     // FUNCTION: aniFade
  244.     // animates fading process by drawing seperate steps to output channel
  245.    
  246.     function aniFade(bolMode) {
  247.         if (this == null) {
  248.             clearInterval(this.intFadeID);
  249.         } else if (bolMode == true && this.bolMute != true) {
  250.             this.setVolume(this.getVolume() + 2);
  251.             if (this.getVolume() >= this.intVolumeMax) {
  252.                 this.setVolume(this.intVolumeMax);
  253.                 clearInterval(this.intFadeID);
  254.                 this.bolFadeOngoing = false;
  255.                 this.onFadeComplete();
  256.             }
  257.         } else {
  258.             this.setVolume(this.getVolume() - 2);
  259.             if (this.getVolume() <= this.intVolumeMin) {
  260.                 this.setVolume(this.intVolumeMin);
  261.                 clearInterval(this.intFadeID);
  262.                 this.bolFadeOngoing = false;
  263.                 this.onFadeComplete();
  264.             }
  265.         }
  266.     }
  267. }
__________________
// ************************************
// progress means simplifying not complicating
// bkanal

Geändert von cmike (08-04-2005 um 12:58 Uhr) Grund: code update
cmike ist offline   Mit Zitat antworten
Alt 20-05-2006, 15:13   #3 (permalink)
Trennschleifer
 
Benutzerbild von bobo_k1
 
Registriert seit: Apr 2004
Ort: Hamburg
Beiträge: 1.282
das DIng such ich eigentlich - leider funktioniert die KLasse nicht
bobo_k1 ist offline   Mit Zitat antworten
Antwort

Lesezeichen

Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks sind an
Pingbacks sind an
Refbacks sind an



Alle Zeitangaben in WEZ +1. Es ist jetzt 07:17 Uhr.

Domains, Webhosting & Vserver von Host Europe
Unterstützt das Flashforum!
Adobe User Group


Copyright ©1999 – 2012 Marc Thiele