File Source: AudioPlayer.java

         /* 
    P/P   *  Method: com.dmdirc.addons.audio.AudioPlayer__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.addons.audio;
    24  
    25  import java.io.File;
    26  import java.io.IOException;
    27  import java.applet.AudioClip;
    28  import java.applet.Applet;
    29  
    30  import javax.sound.sampled.UnsupportedAudioFileException;
    31  import javax.sound.sampled.AudioSystem;
    32  
    33  import java.net.MalformedURLException;
    34  
    35  /**
    36   * The AudioPlayer handles the playing of the audio
    37   *
    38   * @author Shane "Dataforce" Mc Cormack
    39   */
    40  public final class AudioPlayer implements Runnable {
    41  
    42      /** The AudioType enum */
        	 /* 
    P/P 	  *  Method: com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init
        	  * 
        	  *  Postconditions:
        	  *    $VALUES == &new AudioPlayer$AudioType[](AudioPlayer$AudioType__static_init#3)
        	  *    INVALID == &new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#2)
        	  *    $VALUES[1] == &new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#2)
        	  *    WAV == &new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#1)
        	  *    $VALUES[0] == &new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#1)
        	  *    new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#1) num objects == 1
        	  *    new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#2) num objects == 1
        	  *    new AudioPlayer$AudioType[](AudioPlayer$AudioType__static_init#3) num objects == 1
        	  *    $VALUES.length == 2
        	  */
    43  	public static enum AudioType { WAV, INVALID; }
    44  	
    45  	/** The file object of the file to play */
    46  	final File myFile;
    47  	
    48  	/**
    49  	 * Create the AudioPlayer
    50  	 *
    51  	 * @param file The file to play
    52  	 */
        	 /* 
    P/P 	  *  Method: void com.dmdirc.addons.audio.AudioPlayer(File)
        	  * 
        	  *  Postconditions:
        	  *    this.myFile == file
        	  *    init'ed(this.myFile)
        	  */
    53  	public AudioPlayer(final File file) {
    54  		myFile = file;
    55  	}
    56  	
    57  	/**
    58  	 * Play this AudioPlayer.
    59  	 */
    60  	public void play() {
        		 /* 
    P/P 		  *  Method: void play()
        		  */
    61  		new Thread(this).start();
    62  	}
    63  	
    64  	/**
    65  	 * Run this AudioPlayer (Should not be invoked directly).
    66  	 */
    67      @Override
    68  	public void run() {
        		 /* 
    P/P 		  *  Method: void run()
        		  * 
        		  *  Preconditions:
        		  *    (soft) init'ed(com.dmdirc.addons.audio.AudioPlayer$1__static_init.new int[](AudioPlayer$1__static_init#1)[...])
        		  *    (soft) this.myFile != null
        		  * 
        		  *  Presumptions:
        		  *    com.dmdirc.addons.audio.AudioPlayer_AudioType:ordinal(...)@70 in {0,1}
        		  * 
        		  *  Test Vectors:
        		  *    com.dmdirc.addons.audio.AudioPlayer$1__static_init.new int[](AudioPlayer$1__static_init#1)[...]: {1}, {-231..0, 2..232-1}
        		  */
    69  		final AudioType type = getAudioType(myFile);
        		 /* 
    P/P 		  *  Method: com.dmdirc.addons.audio.AudioPlayer$1__static_init
        		  * 
        		  *  Preconditions:
        		  *    (soft) init'ed(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init.new AudioPlayer$AudioType[](AudioPlayer$AudioType__static_init#3)[...])
        		  * 
        		  *  Presumptions:
        		  *    com.dmdirc.addons.audio.AudioPlayer_AudioType:ordinal(...)@70 in {0,1}
        		  *    com.dmdirc.addons.audio.AudioPlayer_AudioType:ordinal(...)@70 - values(...).length in range
        		  * 
        		  *  Postconditions:
        		  *    new int[](AudioPlayer$1__static_init#1) num objects == 1
        		  */
    70  		switch (type) {
    71  			case WAV:
    72  				playWav();
    73  				break;
    74  			default:
    75  				break;
    76  		}
    77  	}
    78  	
    79  	/**
    80  	 * Check if this File is a supported file type
    81  	 *
    82  	 * @param file the File to check
    83  	 * @return true if playable, else false.
    84  	 */
    85  	public static boolean isValid(final File file) {
        		 /* 
    P/P 		  *  Method: bool isValid(File)
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  */
    86  		final AudioType type = getAudioType(file);
    87  		return (type != AudioType.INVALID);
    88  	}
    89  	
    90  	/**
    91  	 * Get the AudioType of a given file
    92  	 *
    93  	 * @param file the File to check
    94  	 * @return AudioType for this file.
    95  	 */
    96  	public static AudioType getAudioType(final File file) {
    97  		AudioType type;
    98  		try {
        			 /* 
    P/P 			  *  Method: AudioPlayer$AudioType getAudioType(File)
        			  * 
        			  *  Postconditions:
        			  *    return_value == One-of{&com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init.new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#1), &com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init.new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#2)}
        			  *    return_value in Addr_Set{&com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init.new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#1),&com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init.new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#2)}
        			  */
    99  			AudioSystem.getAudioInputStream(file);
   100  			type = AudioType.WAV;
   101  		} catch (UnsupportedAudioFileException e) {
   102  			type = AudioType.INVALID;
   103  		} catch (IOException e) {
   104  			type = AudioType.INVALID;
   105  		}
   106  		return type;
   107  	}
   108  	
   109  	
   110  	/**
   111  	 * Play the file as a wav file, using the Applet class.
   112  	 * (This code seems to work better than the non-applet version, but can't play
   113  	 * streams)
   114  	 */
   115  	private void playWav() {
   116  		try {
        			 /* 
    P/P 			  *  Method: void playWav()
        			  * 
        			  *  Preconditions:
        			  *    (soft) this.myFile != null
        			  * 
        			  *  Presumptions:
        			  *    java.io.File:toURI(...)@117 != null
        			  * 
        			  *  Test Vectors:
        			  *    java.applet.Applet:newAudioClip(...)@117: Addr_Set{null}, Inverse{null}
        			  */
   117  			final AudioClip ac = Applet.newAudioClip(myFile.toURI().toURL());
   118  			if (ac != null) { ac.play(); }
   119  		} catch (MalformedURLException e) { }
   120  	}
   121  }








SofCheck Inspector Build Version : 2.17854
AudioPlayer.java 2009-Jun-25 01:54:24
AudioPlayer.class 2009-Sep-02 17:04:14
AudioPlayer$1.class 2009-Sep-02 17:04:14
AudioPlayer$AudioType.class 2009-Sep-02 17:04:14