Simple Audio player

Source: Internet
Author: User

Note: Button pictures and audio files (*.wav) need to go online to download the replacement (here is not easy to upload), what is the problem, you can leave a message ...

Import Java.applet.applet;import java.applet.audioclip;import Java.awt.color;import Java.awt.font;import Java.awt.list;import Java.awt.event.itemevent;import Java.awt.event.itemlistener;import java.awt.event.MouseEvent ; Import Java.awt.event.mouselistener;import Java.net.malformedurlexception;import Java.net.url;import Javax.swing.imageicon;import Javax.swing.jbutton;import Javax.swing.jframe;import Javax.swing.JLabel;public class Musicplayer extends JFrame {JLabel Songnamelabel = null;//with label display status//Four Play Function key button JButton btnlast = null;//previous song JButton BTNPL ay = null; Play/stop JButton btnnext = null; Next song JButton btnloop = null; Loop//Show song list JLabel Listlabel = null; List songslist = null; Audioclip[] songs;//The audio file in the array AudioClip currentsong;//the currently selected audio file//In this case, the song name is written dead. The students themselves can be extended: dynamically loaded by the user from the file dialog box string[] strsongnames = {"Song1.wav", "Song2.wav", "Song3.wav", "Song4.wav", "Song5.wav", " Song6.wav "}; String Strcurrentsongname; The name of the currently selected play song int index = 0; Records the ordinal of the currently selected song (the song that is currently playing), and the default first BooleanIsplayorstop = true;//record playback status (play/stop), default to play Boolean isloop = false; Record loop state, default to not loop play thread playerthread; Audio playback thread public Musicplayer () {super ("Music player"); This.setlayout (null); This.setbounds (300, 50, 310, 500); This.setdefaultcloseoperation (jframe.exit_on_close);//Tags: My music player/song name Songnamelabel = new JLabel (); Font songnamefont = new Font ("italic", Font.Italic), Songnamelabel.setfont (Songnamefont); Songnamelabel.settext (" My music player "); Songnamelabel.setbounds (ten, six, +); This.getcontentpane (). Add (Songnamelabel);//Four Play function button// Constructs four buttons btnlast = new JButton (); btnplay = new JButton (); btnnext = new JButton (); btnloop = new JButton ();//Set location and size btnlast.se Tbounds (Ten, 5, +), Btnplay.setbounds (190, 70,), Btnnext.setbounds (n, N, +); 0, 40);//Set Picture Btnlast.seticon (New ImageIcon ("1.png")), Btnplay.seticon (New ImageIcon ("2.png")), Btnnext.seticon (new ImageIcon ("3.png")); Btnloop.seticon (New ImageIcon ("4.png"));//Add monitoring to the button MouseListener MLC = new Mouselistenerc ();// Mouse Listener ObjectBtnlast.addmouselistener (MLC); Btnplay.addmouselistener (MLC); Btnnext.addmouselistener (MLC); Btnloop.addmouselistener (MLC);//Add to Frame Getcontentpane (). Add (Btnlast); Getcontentpane (). Add (Btnplay); Getcontentpane (). Add (Btnnext); Getcontentpane (). Add (Btnloop);//The following set song playlist//Set the playlist label Listlabel = new JLabel ("playlist "); Listlabel.setbounds (10, 120, 100, 30); Font listlabelfont = new Font ("Arial", Font.Bold,), Listlabel.setfont (Listlabelfont); This.getcontentpane (). Add ( Listlabel)///Set playlist control Songslist = new list (); Songslist.setbounds (max., +); Songslist.setbackground ( Color.cyan); songs = new audioclip[10];//loads the song according to the file name in for (int i = 0; i < strsongnames.length; i++) {Songs[i] = Loadso und (Strsongnames[i]);//load Audio file Songslist.add (strsongnames[i]);//Add song name to List}songslist.additemlistener (new ItemListener () {@Overridepublic void itemstatechanged (ItemEvent e) {currentsong.stop (); index = Songslist.getselectedindex ();p layerthread = new Thread (new Musicrun ());p Layerthread.start ();}); This.getcontentpaNE (). Add (songslist);p layerthread = new Thread (new Musicrun ());p Layerthread.start (); this.setvisible (true);} Private class Musicrun implements Runnable {@Overridepublic void run () {Currentsong = Songs[index];if (isloop) {Currentson G.loop ();} Strcurrentsongname = Strsongnames[index];songnamelabel.settext ("Playing:" + strcurrentsongname); SongsList.select ( index);//Select the current Play song entry in the playlist Btnplay.seticon (New ImageIcon ("5.png"));//Toggle to "Stop" icon if (isplayorstop) {Currentsong.play () ;}}} Private class Mouselistenerc implements MouseListener {@Overridepublic void mouseclicked (MouseEvent e) {JButton btn = (jbu Tton) E.getsource (); Currentsong.stop (); if (btn = = btnplay) {isplayorstop =!isplayorstop;} else if (btn = = btnlast) {Index --;if (Index < 0) {index = strsongnames.length-1;} Isplayorstop = True;isloop = false;}  else if (btn = = btnnext) {index++;index = index% Strsongnames.length;isplayorstop = True;isloop = false;} else if (btn = = Btnloop) {isloop =!isloop;} if (isplayorstop) {playerthread = new Thread(New Musicrun ());p layerthread.run ();} else {songnamelabel.settext ("Stop Playing:" + strcurrentsongname); Btnplay.seticon (New ImageIcon ("2.png");}} @Overridepublic void mousepressed (MouseEvent e) {} @Overridepublic void mousereleased (MouseEvent e) {} @Overridepublic void mouseentered (MouseEvent e) {} @Overridepublic void mouseexited (MouseEvent e) {}}public audioclip loadsound (String filename) {URL url = null;try {url = new URL ("file:" + filename);} catch (Malformedurlexception e) {e.printstacktrace ();} return Applet.newaudioclip (URL);} public static void Main (string[] args) {Musicplayer MP = new Musicplayer ();}}

Simple Audio player

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.