Java applet for playing music

Source: Internet
Author: User
Code:
  1. Import java. Applet .*;
  2. Import java. AWT. event .*;
  3. Import java. AWT .*;
  4. @ Suppresswarnings ("serial ")
  5. Public Class Music extends Applet
  6. Implements actionlistener {
  7. Audioclip audio;
  8. Button btexit, btopen, btplay, btloop, btstop;
  9. /**
  10. *
  11. Method Description: Initialize the Applet
  12. *
  13. Input parameters:
  14. *
  15. Return type:
  16. */
  17. Public void Init (){
  18. // Load the sound file
  19. Audio = getaudioclip (getdocumentbase (), "music. Au"); // Add the name of the song you added to the new folder, in the AU format.
  20. // Create button
  21. Setlayout (New flowlayout ());
  22. Btplay = new button ("play ");
  23. Btplay. addactionlistener (this );
  24. Btloop = new button ("loop ");
  25. Btloop. addactionlistener (this );
  26. Btstop = new button ("stop ");
  27. Btstop. addactionlistener (this); // Add a listener event to the play button
  28. // Add the button to the Applet
  29. Add (btplay );
  30. Add (btloop );
  31. Add (btstop );
  32. }
  33. Public void actionreceivmed (actionevent e ){
  34. // If the play button is clicked
  35. If (E. getsource () = btplay ){
  36. Play ();
  37. }
  38. // If you click the loop button
  39. If (E. getsource () = btloop ){
  40. Loop ();
  41. }
  42. // If the stop button is clicked
  43. If (E. getsource () = btstop ){
  44. Stop ();
  45. }
  46. }
  47. /**
  48. *
  49. Method Description: playback sound
  50. *
  51. Input parameters:
  52. *
  53. Return type:
  54. */
  55. Public void play (){
  56. If (audio! = NULL) Stop ();
  57. Audio. Play ();
  58. }
  59. /**
  60. *
  61. Method Description: loop playback sound
  62. *
  63. Input parameters:
  64. *
  65. Return type:
  66. */
  67. Public void loop (){
  68. If (audio! = NULL)
  69. Audio. Loop ();
  70. }
  71. /**
  72. *
  73. Method Description: Stop playing sound
  74. *
  75. Input parameters:
  76. *
  77. Return type:
  78. */
  79. Public void stop (){
  80. If (audio! = NULL)
  81. Audio. Stop ();
  82. }
  83. }

 

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.