A simple Java player

Source: Internet
Author: User
Import java.applet.*;
Import java.awt.*;
Import java.awt.event.*;
Import java.net.*;
Import javax.swing.*;
Import Java.io.File;
    Class Audioplaydemo extends JFrame implements ActionListener {Boolean looping = false;
    File File1 = new file ("1.wav");
    AudioClip Sound1;
    AudioClip Chosenclip;
    JButton PlayButton = new JButton ("Play");
    JButton Loopbutton = new JButton ("Loop play");
    JButton Stopbutton = new JButton ("Stop");
    JLabel status = New JLabel ("Choose Playback file");
    JPanel ControlPanel = new JPanel ();
    Container Container = Getcontentpane ();
            Public Audioplaydemo () {try {sound1 = Applet.newaudioclip (File1.tourl ());
        Chosenclip = Sound1;
            catch (OutOfMemoryError e) {System.out.println ("memory Overflow");
        E.printstacktrace ();
        catch (Exception e) {e.printstacktrace ();
        } playbutton.addactionlistener (this);
        Loopbutton.addactionlistener (this); Stopbutton.addactionlistEner (this);
        Stopbutton.setenabled (FALSE);
        Controlpanel.add (PlayButton);
        Controlpanel.add (Loopbutton);
        Controlpanel.add (Stopbutton);
        Container.add (ControlPanel, Borderlayout.center);
        Container.add (status, Borderlayout.south);
        SetSize (300, 130);
        SetVisible (TRUE); Setdefaultcloseoperation (Jframe.exit_on_close);
        Exit the program try {jbinit () while closing the window;
        catch (Exception ex) {ex.printstacktrace ();
        } public void actionperformed (ActionEvent event) {if (Chosenclip = null) {Status.settext ("sound is not loaded");
    Return Object Source = Event.getsource ();
        Gets the user wash-activated button if (Source = = PlayButton) {stopbutton.setenabled (true);
        Loopbutton.setenabled (TRUE);
        Chosenclip.play ();
    Status.settext ("being played");
        } if (Source = = Loopbutton) {looping = true;
        Chosenclip.loop ();
        Loopbutton.setenabled (FALSE);Stopbutton.setenabled (TRUE);
    Status.settext ("being looped");
            } if (Source = = Stopbutton) {if (looping) {looping = false;
            Chosenclip.stop ();
        Loopbutton.setenabled (TRUE);
        else {chosenclip.stop ();
        } stopbutton.setenabled (FALSE);
      Status.settext ("Stop playing");
    } public static void Main (String s[]) {new Audioplaydemo ();
 
  private void Jbinit () throws Exception {}}


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.