Question 1: How can I load external sound and stop and loop playback?
Steps:
Step 1: import a media package
Import flash. Media. sound;
Import flash. Media. soundchannel;
If necessary, you can call soundtransform and other classes to implement other functions.
Step 2: create a sound object and call the load method to load external sound. The load method will cause an error. Therefore, pay attention
Capture and listen for errors to prevent program errors
Step 3: You can specify the number of times the music is played.
- Package
- {Import flash. Media. soundchannel;
- Import flash. Media. sound;
- Import flash.net. URLRequest;
- Import flash. Events .*;
- Public Class Music extends sound
- {
- Private VaR _ sound: sound;
- Private var timenums: int;
- Private var channel: soundchannel;
- Private var isfinish: Boolean = false;
- Public Function music ()
- {
- _ Sound = new sound ();
- }
- Public Function loadmusic (songpath: String = NULL): void
- {
- _ Sound. Load (New URLRequest (songpath ));
- _ Sound. addeventlistener (event. Complete, loadcomplete); // scheduling of sound Loading
- _ Sound. addeventlistener (ioerrorevent. io_error, loaderror); // load error Scheduling
- }
- Public Function loadcomplete (E: Event): void
- {
- Startmusic ();
- }
- Public Function startmusic (timenums: Int = 0): void
- {// Play music
- Channel = _ sound. Play (0, timenums); // specifies the number of playback sounds and returns the playback sound of the channel object.
- Channel. addeventlistener (event. sound_complete, soundcompletehandler); // schedule the event when the sound is complete
- }
- Private function soundcompletehandler (E: Event): void
- {
- Stopmusic ();
- This. dispatchevent (new event ("musiccomplete"); // assign a custom completion event after the sound is played.
- }
- Public Function stopmusic (): void
- {// Stop music
- Channel. Stop (); // audio stop
- }
- Private function loaderror (E: ioerrorevent): void
- {
- Trace ("cuowu ");
- }
- }
- }
The above is a sound class that has been encapsulated by the generation. Of course, we can also enhance this class to implement more functions, such as buffering and sound channel mixing.
To implement the above class, we can
VaR Song: Music = new music ();
Song. loadmusic (audio file path );
If you do not want to play music after loading. We can also modify the program. After loading the program, we can give it a switch.
- Public Function loadcomplete (E: Event): void
- {
- Isok = true;
- }
- Public Function startmusic (timenums: Int = 0): void
- {If (isok)
- {
- Channel = _ sound. Play (0, timenums );
- Channel. addeventlistener (event. sound_complete, soundcompletehandler );
- }
- }
In this way, we can change it to this when the program is called.
VaR Song: Music = new music ();
Song. loadmusic (audio file path );
Song. startmusic ();//
If you want to specify the number of playback times, you can change it
Song. startmusic (input playback times );
Because we want to loop through music, we can listen to events.
Song. addeventlistener ("musiccomplete", finish );
Function finish (E: Event): void
{
Song. startmusic (); // play the music again
}
The classes mentioned above can still be extended. This document is not described here.