You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AudioPlayer.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2006-2008 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. package com.dmdirc.addons.audio;
  23. import javax.sound.sampled.AudioInputStream;
  24. import javax.sound.sampled.AudioSystem;
  25. import javax.sound.sampled.AudioFormat;
  26. import javax.sound.sampled.SourceDataLine;
  27. import javax.sound.sampled.DataLine;
  28. import java.io.File;
  29. import java.io.FileNotFoundException;
  30. import java.applet.AudioClip;
  31. import java.applet.Applet;
  32. /**
  33. * The AudioPlayer handles the playing of the audio
  34. *
  35. * @author Shane "Dataforce" Mc Cormack
  36. * @version $Id: AudioPlayer.java 969 2007-04-30 18:38:20Z ShaneMcC $
  37. */
  38. public final class AudioPlayer implements Runnable {
  39. /** The AudioType enum */
  40. private enum AudioType { WAV, OTHER; }
  41. /** The file object of the file to play */
  42. final File myFile;
  43. /**
  44. * Create the AudioPlayer
  45. *
  46. * @param file The file to play
  47. */
  48. public AudioPlayer(final File file) {
  49. myFile = file;
  50. }
  51. /**
  52. * Play this AudioPlayer.
  53. */
  54. public void play() {
  55. new Thread(this).start();
  56. }
  57. /**
  58. * Run this AudioPlayer (Should not be invoked directly).
  59. */
  60. public void run() {
  61. final AudioType type = getAudioType(myFile);
  62. switch (type) {
  63. case WAV:
  64. playWav();
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. /**
  71. * Check if this File is a supported file type
  72. *
  73. * @param file the File to check
  74. * @return true if playable, else false.
  75. */
  76. public static boolean isValid(final File file) {
  77. final AudioType type = getAudioType(file);
  78. return (type != AudioType.OTHER);
  79. }
  80. /**
  81. * Get the AudioType of a given file
  82. *
  83. * @param file the File to check
  84. * @return AudioType for this file.
  85. */
  86. public static AudioType getAudioType(final File file) {
  87. AudioType type;
  88. try {
  89. AudioSystem.getAudioInputStream(file);
  90. type = AudioType.WAV;
  91. } catch (Exception e) {
  92. type = AudioType.OTHER;
  93. }
  94. return type;
  95. }
  96. /**
  97. * Play the file as a wav file, using the Applet class.
  98. * (This code seems to work better than the non-applet version, but can't play
  99. * streams)
  100. */
  101. private void playWav() {
  102. try {
  103. final AudioClip ac = (new Applet()).newAudioClip(myFile.toURI().toURL());
  104. if (ac != null) { ac.play(); }
  105. } catch (Exception e) { /* Bad File, can't play */ }
  106. }
  107. /**
  108. * Play the file as a wav file.
  109. * Based on http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml
  110. */
  111. private void oldPlayWav() {
  112. final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb
  113. AudioInputStream audioInputStream = null;
  114. try {
  115. audioInputStream = AudioSystem.getAudioInputStream(myFile);
  116. } catch (Exception e) { return; }
  117. AudioFormat format = audioInputStream.getFormat();
  118. SourceDataLine auline = null;
  119. DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
  120. try {
  121. auline = (SourceDataLine) AudioSystem.getLine(info);
  122. auline.open(format);
  123. } catch (Exception e) { return; }
  124. auline.start();
  125. int nBytesRead = 0;
  126. byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
  127. try {
  128. while (nBytesRead != -1) {
  129. nBytesRead = audioInputStream.read(abData, 0, abData.length);
  130. int offset = 0;
  131. while (offset < nBytesRead) {
  132. offset += auline.write(abData, offset, nBytesRead-offset);
  133. }
  134. }
  135. } catch (Exception e) {
  136. /** Do Nothing */
  137. } finally {
  138. auline.drain();
  139. auline.close();
  140. }
  141. try {
  142. audioInputStream.close();
  143. } catch (Exception e) { }
  144. }
  145. }