package com.mindprod.example;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JList;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
/**
* Test ability of AudioClip to play various types of sound file.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2007-07-08
* @since 2007-07-08
*/
public class TestAudioClip extends JApplet
{
@SuppressWarnings( { "FieldCanBeLocal" } )
private JButton play;
private JList choose;
/**
* Get the applet started.
*/
public void init()
{
choose = new JList( SoundType.values() );
choose.setSelectedIndex( 0 );
play = new JButton( "Play" );
play.addActionListener( new ActionListener()
{
/**
* Invoked when button pressed
*/
public void actionPerformed( ActionEvent e )
{
final URL url = ( ( SoundType ) choose.getSelectedValue() ).getResource();
System.out.println( url );
final AudioClip clip = getAudioClip( url );
clip.play();
}
} );
Container contentPane = getContentPane();
contentPane.setLayout( new BorderLayout() );
contentPane.add( choose, BorderLayout.NORTH );
contentPane.add( play, BorderLayout.SOUTH );
}
}
@SuppressWarnings( { "UnusedDeclaration" } )
enum SoundType
{
AIFF,
AU,
MIDI,
MP3,
WAV,
WMA;
/**
* get corresponding URL for the resource e.g. file:/E:/intellij/j6/out/production/j6m/com/mindprod/example/sound/test.au
*
* @return URL of the resource in the jar or external sound dir
*/
URL getResource()
{
final String sound = "sound/test." + toString().toLowerCase();
final URL u = TestAudioClip.class.getResource( sound );
if ( u == null )
{
throw new IllegalArgumentException( sound + " missing resource" );
}
return u;
}
}