Example usage for java.applet AudioClip play

List of usage examples for java.applet AudioClip play

Introduction

In this page you can find the example usage for java.applet AudioClip play.

Prototype

void play();

Source Link

Document

Starts playing this audio clip.

Usage

From source file:NoisyButton.java

public static void main(String[] args) throws Exception {
    java.io.File file = new java.io.File("bark.aiff");
    AudioClip sound = Applet.newAudioClip(file.toURL());
    sound.play();
}

From source file:MainClass.java

public static void main(String[] args) {
    try {//from ww  w .  j  a  v  a 2s . c  o  m
        URL url = new URL("file:youraudiofile.wav");
        AudioClip ac = Applet.newAudioClip(url);
        ac.play();

        System.out.println("Press any key to exit.");
        System.in.read();
        ac.stop();
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Play.java

public static void main(String args[]) {
    try {/*from w w w. j a  va2  s.  c om*/
        // Loop
        URL url = new URL("http://java.sun.com/applets/other/Hangman/audio/whoopy.au");
        AudioClip clip = Applet.newAudioClip(url);
        clip.loop();
        Thread.sleep(5000);
        //Play
        File file = new File("bark.wav");
        clip = Applet.newAudioClip(file.toURL());
        clip.play();
        Thread.sleep(500);
        System.exit(0);
    } catch (InterruptedException e) {
    } catch (MalformedURLException e) {
    }
}

From source file:MainClass.java

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Audio")) {
        String soundName = getParameter("audio");

        if (soundName != null) {
            AudioClip ac = getAudioClip(getDocumentBase(), soundName);

            ac.play();
        }/*from  w  w w  . j  a  v a2 s  .  c  om*/

        return;
    }

    try {
        URL u = new URL("http://www.java2s.com");
        getAppletContext().showDocument(u);
    } catch (MalformedURLException exc) {
        System.out.println(e);
    }
}

From source file:com.openbravo.pos.printer.TicketParser.java

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {

    switch (m_iOutputType) {
    case OUTPUT_NONE:
        if ("play".equals(qName)) {
            try {
                AudioClip oAudio = Applet
                        .newAudioClip(getClass().getClassLoader().getResource(text.toString()));
                oAudio.play();
            } catch (Exception fnfe) {
                //throw new ResourceNotFoundException( fnfe.getMessage() );
            }// w  w w .  j av  a  2 s. co  m
            text = null;
        }
        break;
    case OUTPUT_TICKET:
        if ("image".equals(qName)) {
            try {
                // BufferedImage image = ImageIO.read(getClass().getClassLoader().getResourceAsStream(m_sText.toString()));
                BufferedImage image = m_system.getResourceAsImage(text.toString());
                if (image != null) {
                    m_oOutputPrinter.printImage(image);
                }
            } catch (Exception fnfe) {
                //throw new ResourceNotFoundException( fnfe.getMessage() );
            }
            text = null;
        } else if ("barcode".equals(qName)) {
            m_oOutputPrinter.printBarCode(bctype, bcposition, text.toString());
            text = null;
        } else if ("text".equals(qName)) {
            if (m_iTextLength > 0) {
                switch (m_iTextAlign) {
                case DevicePrinter.ALIGN_RIGHT:
                    m_oOutputPrinter.printText(m_iTextStyle,
                            DeviceTicket.alignRight(text.toString(), m_iTextLength));
                    break;
                case DevicePrinter.ALIGN_CENTER:
                    m_oOutputPrinter.printText(m_iTextStyle,
                            DeviceTicket.alignCenter(text.toString(), m_iTextLength));
                    break;
                default: // DevicePrinter.ALIGN_LEFT
                    m_oOutputPrinter.printText(m_iTextStyle,
                            DeviceTicket.alignLeft(text.toString(), m_iTextLength));
                    break;
                }
            } else {
                m_oOutputPrinter.printText(m_iTextStyle, text.toString());
            }
            text = null;
        } else if ("line".equals(qName)) {
            m_oOutputPrinter.endLine();
        } else if ("ticket".equals(qName)) {
            m_oOutputPrinter.endReceipt();
            m_iOutputType = OUTPUT_NONE;
            m_oOutputPrinter = null;
        }
        break;
    case OUTPUT_DISPLAY:
        if ("line".equals(qName)) { // line 1 or 2 of the display
            if (m_sVisorLine1 == null) {
                m_sVisorLine1 = m_sVisorLine.toString();
            } else {
                m_sVisorLine2 = m_sVisorLine.toString();
            }
            m_sVisorLine = null;
        } else if ("line1".equals(qName)) { // linea 1 del visor
            m_sVisorLine1 = m_sVisorLine.toString();
            m_sVisorLine = null;
        } else if ("line2".equals(qName)) { // linea 2 del visor
            m_sVisorLine2 = m_sVisorLine.toString();
            m_sVisorLine = null;
        } else if ("text".equals(qName)) {
            if (m_iTextLength > 0) {
                switch (m_iTextAlign) {
                case DevicePrinter.ALIGN_RIGHT:
                    m_sVisorLine.append(DeviceTicket.alignRight(text.toString(), m_iTextLength));
                    break;
                case DevicePrinter.ALIGN_CENTER:
                    m_sVisorLine.append(DeviceTicket.alignCenter(text.toString(), m_iTextLength));
                    break;
                default: // DevicePrinter.ALIGN_LEFT
                    m_sVisorLine.append(DeviceTicket.alignLeft(text.toString(), m_iTextLength));
                    break;
                }
            } else {
                m_sVisorLine.append(text);
            }
            text = null;
        } else if ("display".equals(qName)) {
            m_printer.getDeviceDisplay().writeVisor(m_iVisorAnimation, m_sVisorLine1, m_sVisorLine2);
            m_iVisorAnimation = DeviceDisplayBase.ANIMATION_NULL;
            m_sVisorLine1 = null;
            m_sVisorLine2 = null;
            m_iOutputType = OUTPUT_NONE;
            m_oOutputPrinter = null;
        }
        break;
    case OUTPUT_FISCAL:
        if ("fiscalreceipt".equals(qName)) {
            m_printer.getFiscalPrinter().endReceipt();
            m_iOutputType = OUTPUT_NONE;
        } else if ("line".equals(qName)) {
            m_printer.getFiscalPrinter().printLine(text.toString(), m_dValue1, m_dValue2, attribute3);
            text = null;
        } else if ("message".equals(qName)) {
            m_printer.getFiscalPrinter().printMessage(text.toString());
            text = null;
        } else if ("total".equals(qName)) {
            m_printer.getFiscalPrinter().printTotal(text.toString(), m_dValue1);
            text = null;
        }
        break;
    }
}

From source file:Animator.java

/**
 * Run the animation. This method is called by class Thread.
 * //w w w.  j av a  2  s  .c  o m
 * @see java.lang.Thread
 */
public void run() {
    Thread me = Thread.currentThread();
    URL badURL;

    me.setPriority(Thread.MIN_PRIORITY);

    if (!loaded) {
        try {
            // ... to do a bunch of loading.
            if (startUpImageURL != null) {
                tellLoadingMsg(startUpImageURL, imageLabel);
                startUpImage = getImage(startUpImageURL);
                tracker.addImage(startUpImage, STARTUP_ID);
                tracker.waitForID(STARTUP_ID);
                if (tracker.isErrorID(STARTUP_ID)) {
                    loadError(startUpImageURL, "start-up image");
                }
                Dimension size = getImageDimensions(startUpImage);
                resize(size.width, size.height);
                repaint();
            }

            if (backgroundImageURL != null) {
                tellLoadingMsg(backgroundImageURL, imageLabel);
                backgroundImage = getImage(backgroundImageURL);
                tracker.addImage(backgroundImage, BACKGROUND_ID);
                tracker.waitForID(BACKGROUND_ID);
                if (tracker.isErrorID(BACKGROUND_ID)) {
                    loadError(backgroundImageURL, "background image");
                }
                updateMaxDims(getImageDimensions(backgroundImage));
                repaint();
            }

            // Fetch the animation frames
            if (!fetchImages(images)) {
                // Need to add method to MediaTracker to return
                // files that caused errors during loading.
                loadError("an image", imageLabel);
                return;
            }

            if (soundtrackURL != null && soundtrack == null) {
                tellLoadingMsg(soundtrackURL, imageLabel);
                soundtrack = getAudioClip(soundtrackURL);
                if (soundtrack == null) {
                    loadError(soundtrackURL, "soundtrack");
                    return;
                }
            }

            if (sounds != null) {
                badURL = fetchSounds(sounds);
                if (badURL != null) {
                    loadError(badURL, soundLabel);
                    return;
                }
            }

            clearLoadingMessage();

            offScrImage = createImage(maxWidth, maxHeight);
            offScrGC = offScrImage.getGraphics();
            offScrGC.setColor(Color.white);

            resize(maxWidth, maxHeight);
            loaded = true;
            error = false;
        } catch (Exception e) {
            error = true;
            e.printStackTrace();
        }
    }

    if (userPause) {
        return;
    }

    if (repeat || frameNum < images.size()) {
        startPlaying();
    }

    try {
        if (images.size() > 1) {
            while (maxWidth > 0 && maxHeight > 0 && engine == me) {
                if (frameNum >= images.size()) {
                    if (!repeat) {
                        return;
                    }
                    setFrameNum(0);
                }
                repaint();

                if (sounds != null) {
                    AudioClip clip = (AudioClip) sounds.get(frameNumKey);
                    if (clip != null) {
                        clip.play();
                    }
                }

                try {
                    Integer pause = null;
                    if (durations != null) {
                        pause = (Integer) durations.get(frameNumKey);
                    }
                    if (pause == null) {
                        Thread.sleep(globalPause);
                    } else {
                        Thread.sleep(pause.intValue());
                    }
                } catch (InterruptedException e) {
                    // Should we do anything?
                }
                setFrameNum(frameNum + 1);
            }
        }
    } finally {
        stopPlaying();
    }
}

From source file:de.tor.tribes.ui.windows.ClockFrame.java

public synchronized void playSound(String pSound) {
    Clip clip = null;/* w w  w  .ja v a  2s  .c  om*/
    AudioClip ac = null;
    try {
        if (org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS) {
            clip = AudioSystem.getClip();
            BufferedInputStream bin = new BufferedInputStream(
                    ClockFrame.class.getResourceAsStream("/res/" + pSound + ".wav"));
            AudioInputStream inputStream = AudioSystem.getAudioInputStream(bin);
            clip.open(inputStream);
            clip.start();
        } else {
            ac = Applet.newAudioClip(ClockFrame.class.getResource("/res/" + pSound + ".wav"));
            ac.play();
        }
    } catch (Exception e) {
        logger.error("Failed to play sound", e);
    }
    try {
        Thread.sleep(2500);
    } catch (Exception ignored) {
    }

    try {
        if (clip != null) {
            clip.stop();
            clip.flush();
            clip = null;
        }

        if (ac != null) {
            ac.stop();
            ac = null;
        }
    } catch (Exception ignored) {
    }
}

From source file:org.trianacode.taskgraph.util.FileUtils.java

/**
 * Plays a Triana system sound file by loading it across the internet (if Triana is an applet)or local disk.  It
 * auto-detects what protocol it should use. It looks in the %TRIANA%/system/sounds directory for the sound file
 * called "audioFile"//from   ww w  .j  a  v a 2 s  . c o m
 */
public static void playSystemAudio(String audioFile) {
    final AudioClip audio = getSystemAudioClip(audioFile);

    audio.play();

    /*        Thread t = new Thread( new Runnable() {   // set off in a different thread
    public void run() {
        audio.play();
        }
    });
            
            t.start(); */
}

From source file:tvbrowser.extras.reminderplugin.ReminderPlugin.java

/**
 * Plays a sound./*from w  w  w  .  j  a v  a 2  s  .co m*/
 *
 * @param fileName
 *          The file name of the sound to play.
 * @return The sound Object.
 */
public static Object playSound(final String fileName) {
    try {
        if (StringUtils.endsWithIgnoreCase(fileName, ".mid")) {
            final Sequencer sequencer = MidiSystem.getSequencer();
            sequencer.open();

            final InputStream midiFile = new FileInputStream(fileName);
            sequencer.setSequence(MidiSystem.getSequence(midiFile));

            sequencer.start();

            new Thread("Reminder MIDI sequencer") {
                @Override
                public void run() {
                    setPriority(Thread.MIN_PRIORITY);
                    while (sequencer.isRunning()) {
                        try {
                            Thread.sleep(100);
                        } catch (Exception ee) {
                            // ignore
                        }
                    }

                    try {
                        sequencer.close();
                        midiFile.close();
                    } catch (Exception ee) {
                        // ignore
                    }
                }
            }.start();

            return sequencer;
        } else {
            final AudioInputStream ais = AudioSystem.getAudioInputStream(new File(fileName));

            final AudioFormat format = ais.getFormat();
            final DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

            if (AudioSystem.isLineSupported(info)) {
                final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);

                line.open(format);
                line.start();

                new Thread("Reminder audio playing") {
                    private boolean stopped;

                    @Override
                    public void run() {
                        byte[] myData = new byte[1024 * format.getFrameSize()];
                        int numBytesToRead = myData.length;
                        int numBytesRead = 0;
                        int total = 0;
                        int totalToRead = (int) (format.getFrameSize() * ais.getFrameLength());
                        stopped = false;

                        line.addLineListener(new LineListener() {
                            public void update(LineEvent event) {
                                if (line != null && !line.isRunning()) {
                                    stopped = true;
                                    line.close();
                                    try {
                                        ais.close();
                                    } catch (Exception ee) {
                                        // ignore
                                    }
                                }
                            }
                        });

                        try {
                            while (total < totalToRead && !stopped) {
                                numBytesRead = ais.read(myData, 0, numBytesToRead);

                                if (numBytesRead == -1) {
                                    break;
                                }

                                total += numBytesRead;
                                line.write(myData, 0, numBytesRead);
                            }
                        } catch (Exception e) {
                        }

                        line.drain();
                        line.stop();
                    }
                }.start();

                return line;
            } else {
                URL url = new File(fileName).toURI().toURL();
                AudioClip clip = Applet.newAudioClip(url);
                clip.play();
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        if ((new File(fileName)).isFile()) {
            URL url;
            try {
                url = new File(fileName).toURI().toURL();
                AudioClip clip = Applet.newAudioClip(url);
                clip.play();
            } catch (MalformedURLException e1) {
            }
        } else {
            String msg = mLocalizer.msg("error.1", "Error loading reminder sound file!\n({0})", fileName);
            JOptionPane.showMessageDialog(UiUtilities.getBestDialogParent(MainFrame.getInstance()), msg,
                    Localizer.getLocalization(Localizer.I18N_ERROR), JOptionPane.ERROR_MESSAGE);
        }
    }
    return null;
}