Multimedia interaction application BASICS (10)

Source: Internet
Author: User

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.

  1. Package
  2. {Import flash. Media. soundchannel;
  3. Import flash. Media. sound;
  4. Import flash.net. URLRequest;
  5. Import flash. Events .*;
  6. Public Class Music extends sound
  7. {
  8. Private VaR _ sound: sound;
  9. Private var timenums: int;
  10. Private var channel: soundchannel;
  11. Private var isfinish: Boolean = false;
  12. Public Function music ()
  13. {
  14. _ Sound = new sound ();
  15. }
  16. Public Function loadmusic (songpath: String = NULL): void
  17. {
  18. _ Sound. Load (New URLRequest (songpath ));
  19. _ Sound. addeventlistener (event. Complete, loadcomplete); // scheduling of sound Loading
  20. _ Sound. addeventlistener (ioerrorevent. io_error, loaderror); // load error Scheduling
  21. }
  22. Public Function loadcomplete (E: Event): void
  23. {
  24. Startmusic ();
  25. }
  26. Public Function startmusic (timenums: Int = 0): void
  27. {// Play music
  28. Channel = _ sound. Play (0, timenums); // specifies the number of playback sounds and returns the playback sound of the channel object.
  29. Channel. addeventlistener (event. sound_complete, soundcompletehandler); // schedule the event when the sound is complete
  30. }
  31. Private function soundcompletehandler (E: Event): void
  32. {
  33. Stopmusic ();
  34. This. dispatchevent (new event ("musiccomplete"); // assign a custom completion event after the sound is played.
  35. }
  36. Public Function stopmusic (): void
  37. {// Stop music
  38. Channel. Stop (); // audio stop
  39. }
  40. Private function loaderror (E: ioerrorevent): void
  41. {
  42. Trace ("cuowu ");
  43. }
  44. }
  45. }

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.

  1. Public Function loadcomplete (E: Event): void
  2. {
  3. Isok = true;
  4. }
  5. Public Function startmusic (timenums: Int = 0): void
  6. {If (isok)
  7. {
  8. Channel = _ sound. Play (0, timenums );
  9. Channel. addeventlistener (event. sound_complete, soundcompletehandler );
  10. }
  11. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.