Sound Application : Sound « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » SoundScreenshots 
Sound Application
Sound Application
 
/* From http://java.sun.com/docs/books/tutorial/index.html */
/*
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 *
 * -Redistribution in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SoundApplication extends JPanel implements ActionListener,
    ItemListener {
  SoundList soundList;

  String auFile = "spacemusic.au";

  String aiffFile = "flute+hrn+mrmba.aif";

  String midiFile = "trippygaia1.mid";

  String rmfFile = "jungle.rmf";

  String wavFile = "bottle-open.wav";

  String chosenFile;

  AudioClip onceClip, loopClip;

  URL codeBase;

  JComboBox formats;

  JButton playButton, loopButton, stopButton;

  JLabel status;

  boolean looping = false;

  public SoundApplication() {
    String[] fileTypes = auFile, aiffFile, midiFile, rmfFile, wavFile };
    formats = new JComboBox(fileTypes);
    formats.setSelectedIndex(0);
    chosenFile = (Stringformats.getSelectedItem();
    formats.addItemListener(this);

    playButton = new JButton("Play");
    playButton.addActionListener(this);

    loopButton = new JButton("Loop");
    loopButton.addActionListener(this);

    stopButton = new JButton("Stop");
    stopButton.addActionListener(this);
    stopButton.setEnabled(false);

    status = new JLabel(
        "Click Play or Loop to play the selected sound file.");

    JPanel controlPanel = new JPanel();
    controlPanel.add(formats);
    controlPanel.add(playButton);
    controlPanel.add(loopButton);
    controlPanel.add(stopButton);

    JPanel statusPanel = new JPanel();
    statusPanel.add(status);

    add(controlPanel);
    add(statusPanel);

    startLoadingSounds();
  }

  public void itemStateChanged(ItemEvent e) {
    chosenFile = (Stringformats.getSelectedItem();
    soundList.startLoading(chosenFile);
  }

  void startLoadingSounds() {
    //Start asynchronous sound loading.
    try {
      codeBase = new URL("file:" + System.getProperty("user.dir""/");
    catch (MalformedURLException e) {
      System.err.println(e.getMessage());
    }
    soundList = new SoundList(codeBase);
    soundList.startLoading(auFile);
    soundList.startLoading(aiffFile);
    soundList.startLoading(midiFile);
    soundList.startLoading(rmfFile);
    soundList.startLoading(wavFile);
  }

  public void stop() {
    onceClip.stop()//Cut short the one-time sound.
    if (looping) {
      loopClip.stop()//Stop the sound loop.
    }
  }

  public void start() {
    if (looping) {
      loopClip.loop()//Restart the sound loop.
    }
  }

  public void actionPerformed(ActionEvent event) {
    //PLAY BUTTON
    Object source = event.getSource();
    if (source == playButton) {
      //Try to get the AudioClip.
      onceClip = soundList.getClip(chosenFile);
      stopButton.setEnabled(true);
      onceClip.play()//Play it once.
      status.setText("Playing sound " + chosenFile + ".");
      if (onceClip == null) {
        status.setText("Sound " + chosenFile + " not loaded yet.");
      }
      return;
    }

    //START LOOP BUTTON
    if (source == loopButton) {
      loopClip = soundList.getClip(chosenFile);

      looping = true;
      loopClip.loop()//Start the sound loop.
      loopButton.setEnabled(false)//Disable start button.
      stopButton.setEnabled(true);
      status.setText("Playing sound " + chosenFile + " continuously.");
      if (loopClip == null) {
        status.setText("Sound " + chosenFile + " not loaded yet.");
      }
      return;
    }

    //STOP LOOP BUTTON
    if (source == stopButton) {
      if (looping) {
        looping = false;
        loopClip.stop()//Stop the sound loop.
        loopButton.setEnabled(true)//Enable start button.
      else if (onceClip != null) {
        onceClip.stop();
      }
      stopButton.setEnabled(false);
      status.setText("Stopped playing " + chosenFile + ".");
      return;
    }
  }

  public static void main(String s[]) {
    WindowListener l = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    JFrame f = new JFrame("SoundApplication");
    f.addWindowListener(l);
    f.getContentPane().add(new SoundApplication());
    f.setSize(new Dimension(400100));
    f.show();
  }
}

/**
 * Loads and holds a bunch of audio files whose locations are specified relative
 * to a fixed base URL.
 */

class SoundList extends java.util.Hashtable {
  JApplet applet;

  URL baseURL;

  public SoundList(URL baseURL) {
    super(5)//Initialize Hashtable with capacity of 5 entries.
    this.baseURL = baseURL;
  }

  public void startLoading(String relativeURL) {
    new SoundLoader(this, baseURL, relativeURL);
  }

  public AudioClip getClip(String relativeURL) {
    return (AudioClipget(relativeURL);
  }

  public void putClip(AudioClip clip, String relativeURL) {
    put(relativeURL, clip);
  }
}

class SoundLoader extends Thread {
  SoundList soundList;

  URL completeURL;

  String relativeURL;

  public SoundLoader(SoundList soundList, URL baseURL, String relativeURL) {
    this.soundList = soundList;
    try {
      completeURL = new URL(baseURL, relativeURL);
    catch (MalformedURLException e) {
      System.err.println(e.getMessage());
    }
    this.relativeURL = relativeURL;
    setPriority(MIN_PRIORITY);
    start();
  }

  public void run() {
    AudioClip audioClip = Applet.newAudioClip(completeURL);
    soundList.putClip(audioClip, relativeURL);
  }
}


           
         
  
Related examples in the same category
1. Play sound
2. Duke Speaks
3. Duke Speaks Test
4. Sound Applet
5. Sound player
6. Simple program to try out the new Sound stuff in JDK1.2
7. An example of loading and playing a sound using a ClipAn example of loading and playing a sound using a Clip
8. An example of playing a sound with an echo filter
9. The Filter3dTest class demonstrates the Filter3d functionality
10. Sound Manager TestSound Manager Test
11. An example that plays a Midi sequence
12. Determining When a Sampled Audio Player Has Finished Playing
13. Setting the Volume of a Sampled Audio Player
14. Determining the Position of a Sampled Audio Player
15. Load audio file From URL
16. Playing Streaming Sampled Audio
17. Determining the File Format of a Sampled Audio File
18. Loading and Playing Sampled Audio
19. Determining the Position of a Midi Sequencer
20. Continuously Playing a Sampled Audio File
21. Load image and sound from Jar file
22. Determine the duration of a Midi audio file
23. Playing Streaming Midi Audio
24. Capturing Audio with Java Sound API
25. A simple player for sampled sound files.
26. Load and play Midi audio
27. Play a streaming Midi audio
28. Determining When a Midi Audio Player Has Finished Playing
29. Float Control Component
30. This is a simple program to record sounds and play them back
31. Determining the Duration of a Midi Audio File
32. Loading and Playing Midi Audio
33. Determining the File Format of a Midi Audio File
34. Play an audio file from a JAR file
35. Setting the Volume of Playing Midi Audio
36. Make your own Java Media Player to play media files
37. Determining the Encoding of a Sampled Audio File
38. Determining the Duration of a Sampled Audio File
39. audio sound: implements java.applet.AudioClip
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.