A tutti i webmaster piace inserire nei propri siti creati su commissione un player musicale che possa tenere impegnati con un pò di musica i visitatori senza farli annoiare, non tutti sanno cosa fare..

Un sottofondo musicale è un effetto cui davvero non si può rinunciare: dà piacere alle orecchie dei vostri utenti e rende il sito molto più accattivante.
E' una cosa davvero semplice e veloce inserire un player: create due pulsanti e chiamateli “Disattiva” e “Attiva” (ad esempio); usate lo snippet che segue e assicuratevi che i file .fla e quelli .as siano nella stessa cartella.

[php]package {
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundLoaderContext;
import flash.events.IOErrorEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.SoundChannel;
import flash.media.SoundTransform;

public class MainSoundplayer extends Sprite
{
private var snd:Sound = new Sound();
private var channel:SoundChannel;
private var trackTransform:SoundTransform = new SoundTransform(1.0);
private var muteBtn:Mute = new Mute();
private var unmuteBtn:Unmute = new Unmute();

public function MainSoundplayer() {
addEventListener(MouseEvent.CLICK,processStageClicks);
setupButtons();
loadSound();
}

function processStageClicks(evt:MouseEvent):void
{
switch(evt.target.name)
{
case "unmute":
unmuteSound();
break;
case "mute":
muteSound();
break;
default:
return;
}
}

function setupButtons():void
{
unmuteBtn.visible = false;
unmuteBtn.name = "unmute";
muteBtn.name = "mute";
addChild(unmuteBtn);
addChild(muteBtn);
}

function loadSound():void
{
var snd:Sound = new Sound();
var req:URLRequest = new URLRequest("http://mcshare.myblog.it/media/02/00/5297c6b1fb603a0768135788b1f99407.mp3");
var context:SoundLoaderContext = new SoundLoaderContext(3000, false);

snd.load(req, context);
channel = snd.play();
channel.soundTransform = trackTransform;
snd.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
}

function unmuteSound():void
{
trackTransform.volume = 1;
channel.soundTransform = trackTransform;
unmuteBtn.visible = false;
muteBtn.visible = true;
}

function muteSound():void
{
trackTransform.volume=0;
channel.soundTransform = trackTransform;
unmuteBtn.visible = true;
muteBtn.visible = false;
}

private function errorHandler(errorEvent:IOErrorEvent):void {
trace("The sound could not be loaded: " + errorEvent.text);
}
}
}[/php]

Per qualsiasi problema o domande, lasciate un commento. Alla prossima!