import java.io.*;
import javax.sound.sampled.*;

import io.ResourceFinder;


/**
 * A simple application that can be used to present
 * sampled auditory content that is stored in a file
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class ClipPlayer
{
    /**
     * The entry point of the application
     *
     * args[0] should contain the file to load and present
     *
     * @param args  The command-line arguments
     */
    public static void main(String[] args) throws Exception
    {
       AudioFormat        format;       
       AudioInputStream   stream;   
       Clip               clip;       
       DataLine.Info      info;       
       InputStream        is;
       ResourceFinder     finder;


       if ((args != null) && (!args[0].equals("")))
       {
//[step1.
          // Get the resource
          finder = ResourceFinder.createInstance();
          is     = finder.findInputStream("/"+args[0]);

          // Create an AudioInputStream from the InputStream
          stream = AudioSystem.getAudioInputStream(is);
//]step1.
//[step2.
          // Get the AudioFormat for the File
          format = stream.getFormat();

          // Create an object that contains all relevant 
          // information about a DataLine for this AudioFormat
          info = new DataLine.Info(Clip.class, format);
//]step2.
//[step3.
          // Create a Clip (i.e., a pre-loaded Line)
          clip = (Clip)AudioSystem.getLine(info);
//]step3.
//[step4.
          // Tell the Clip to acquire any required system 
          // resources and become operational
          clip.open(stream);
//]step4.
//[step5.
          // Present the Clip (without blocking the 
          // thread of execution)
          clip.start();
//]step5.
          System.out.println("Press [Enter] to exit...");          
          System.in.read();
       }
       else
       {
          System.out.println("You forgot the file name");
       }
    }
    
}
