Today, a flash friend asked how to control the sound problem in AS3, using the following small example to illustrate:
/*
As3sound.as
*/
Package {
Import Flash.display.Sprite;
Import flash.events.*;
Import Flash.media.Sound;
Import Flash.media.SoundChannel;
Import Flash.net.URLRequest;
Import Flash.utils.Timer;
Import Flash.text.TextField;
Import flash.text.TextFieldAutoSize;
Import Flash.filters.DropShadowFilter;
public class As3sound extends Sprite {
private var url:string = "Http://XXX.com/music/XXX.mp3";
private Var Soundfactory:sound;
private Var Channel:soundchannel;
private Var Positiontimer:timer;
private Var Play_btn:sprite;
private Var Stop_btn:sprite;
private var d_filtersropshadowfilter=new dropshadowfilter (5,45,0x000000,80,8,8);
Used to record whether the music is now paused
private var Bsoundstop:boolean = false;
Public Function As3sound () {
var Sxl_txt:textfield = new TextField ();
sxl_txt.text= "How to control the playback or stopping of sound in CS4";
Sxl_txt.autosize=textfieldautosize.left;
SXL_TXT.X=STAGE.STAGEWIDTH/2-SXL_TXT.WIDTH/2;
sxl_txt.y=20;
AddChild (Sxl_txt);
var mp3_request:urlrequest = new URLRequest (URL);
Soundfactory = new Sound ();
After the data has been successfully loaded
Soundfactory.addeventlistener (Event.complete, Completehandler);
When there is ID3 data available for MP3 sound
Soundfactory.addeventlistener (EVENT.ID3, Id3handler);
When you load a music error
Soundfactory.addeventlistener (Ioerrorevent.io_error, Ioerrorhandler);
Music Loading ...
Soundfactory.addeventlistener (progressevent.progress, Progresshandler);
Soundfactory.load (mp3_request);
Channel = Soundfactory.play ();
Music playback Complete
Channel.addeventlistener (Event.sound_complete, Soundcompletehandler);
Monitor the playback progress of music with a timer
Positiontimer = new Timer (1000);
Positiontimer.addeventlistener (Timerevent.timer, Positiontimerhandler);
Positiontimer.start ();
Create a button to play the music
PLAY_BTN = new Sprite ();
Play_btn.graphics.beginFill (0XFFCC32);
Play_btn.graphics.drawRoundRect (0, 0, 70, 18, 10, 10);
Play_btn.graphics.endFill ();
var Play_txt:textfield = new TextField ();
Play_txt.text = "Play";
play_txt.x=18;
play_btn.x=50;
play_btn.y=100;
Play_txt.selectable = false;
Play_btn.addchild (Play_txt);
Play_btn.filters=[d_filters];
Play_btn.addeventlistener (Mouseevent.click, Soundplay);
AddChild (PLAY_BTN);
Create a button to stop the music
STOP_BTN = new Sprite ();
Stop_btn.graphics.beginFill (0XFFCC32);
Stop_btn.graphics.drawRoundRect (0, 0, 70, 18, 10, 10);
Stop_btn.graphics.endFill ();
stop_btn.x=130;
stop_btn.y=100;
var Stop_txt:textfield = new TextField ();
stop_txt.x=18;
Stop_txt.text = "Pause";
Stop_txt.selectable = false;
Stop_btn.addchild (Stop_txt);
Stop_btn.filters=[d_filters];
Stop_btn.addeventlistener (Mouseevent.click, soundstop);
AddChild (STOP_BTN);
}
Monitor the playback progress of music
Private Function Positiontimerhandler (event:timerevent): void {
var ybf:int = channel.position.toFixed (0);
var zcd:int = soundfactory.length;
var bfs:int = Math.floor (ybf/zcd*100);
Trace ("Total music length:" +zcd, "music has played:" +YBF, "The playback Progress is:" +bfs+ "%");
}
When loading music completes
Private Function Completehandler (event:event): void {
Trace ("Load Music Complete:" + event);
}
When there is ID3 data available for MP3 sound
Private Function Id3handler (event:event): void {
Trace ("The ID3 information of the music is as follows:");
For (var s in soundfactory.id3) {
Trace ("", S, ":", Soundfactory.id3[s]);
}
Trace ("About ID3 information, see Sound class--> attribute-->id3");
}
When you load a music error
Private Function Ioerrorhandler (event:event): void {
Trace ("Load music error, error message as follows:" + event);
Positiontimer.stop ();
}
When you load music
Private Function Progresshandler (eventrogressevent): void {
var yjz:int = event.bytesloaded;
var zcd:int = event.bytestotal;
var bfs:int = Math.floor (yjz/zcd*100);
Trace ("Total music length:" +zcd, "Loaded:" +YJZ, "Loading progress is:" +bfs+ "%");
}
Music playback Complete
Private Function Soundcompletehandler (event:event): void {
Trace ("Music playback Complete:" + event);
Positiontimer.stop ();
}
Click the Play button event
Private Function Soundplay (event:mouseevent): void {
if (bsoundstop) {
Bsoundstop = false;
Channel = Soundfactory.play (channel.position.toFixed (0));
}
}
Click the Stop button event
Private Function Soundstop (event:mouseevent): void {
if (!bsoundstop) {
Bsoundstop = true;
Channel.stop ();
}
}
}
}