/*
 * @(#)TestAudioClip.java
 *
 * Summary: Test ability of AudioClip to play various types of sound file.
 *
 * Copyright: (c) 2009 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.6+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  1.0 2007-07-08
 */
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
    {
    // ------------------------------ FIELDS ------------------------------

    @SuppressWarnings( { "FieldCanBeLocal" } )
    private JButton play;

    private JList choose;

    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------
    /**
     * 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;

    // -------------------------- OTHER METHODS --------------------------

    /**
     * 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();
        // this is an unofficial API. You will get a warning message:
        // [warning: sun.audio.AudioPlayer is Sun proprietary that may be removed in a future release]

        final URL u = TestAudioClip.class.getResource( sound );
        if ( u == null )
            {
            throw new IllegalArgumentException( sound + " missing resource" );
            }
        return u;
        }
    }