/*
 * [TestAudioClip.java]
 *
 * Summary: Test ability of AudioClip to play various types of sound file.
 *
 * Copyright: (c) 2009-2017 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.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * 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;

import static java.lang.System.*;

@SuppressWarnings( { "UnusedDeclaration" } )
enum SoundType
    {
        AIFF,
        AU,
        MIDI,
        MP3,
        WAV,
        WMA;

    /**
     * get corresponding URL for the resource e.g.
     * file://localhost/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;
        }
    }

/**
 * 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<SoundType> choose;

    /**
     * Called by the browser or Applet viewer to inform
     * this Applet that it is being reclaimed and that it should destroy
     * any resources that it has allocated.
     */
    @Override
    public void destroy()
        {
        choose = null;
        play = null;
        }

    /**
     * Called by the browser or Applet viewer to inform
     * this Applet that it has been loaded into the system.
     */
    @Override
    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 = choose.getSelectedValue().getResource();
                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 );
        this.validate();
        this.setVisible( true );
        }
    }