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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2010 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 java.io.File;
  24. import java.io.IOException;
  25. import java.applet.AudioClip;
  26. import java.applet.Applet;
  27. import javax.sound.sampled.UnsupportedAudioFileException;
  28. import javax.sound.sampled.AudioSystem;
  29. import java.net.MalformedURLException;
  30. /**
  31. * The AudioPlayer handles the playing of the audio
  32. *
  33. * @author Shane "Dataforce" Mc Cormack
  34. */
  35. public final class AudioPlayer implements Runnable {
  36. /** The AudioType enum */
  37. public static enum AudioType {
  38. WAV, INVALID;
  39. }
  40. /** The file object of the file to play */
  41. final File myFile;
  42. /**
  43. * Create the AudioPlayer
  44. *
  45. * @param file The file to play
  46. */
  47. public AudioPlayer(final File file) {
  48. myFile = file;
  49. }
  50. /**
  51. * Play this AudioPlayer.
  52. */
  53. public void play() {
  54. new Thread(this).start();
  55. }
  56. /**
  57. * Run this AudioPlayer (Should not be invoked directly).
  58. */
  59. @Override
  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.INVALID);
  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 (UnsupportedAudioFileException e) {
  92. type = AudioType.INVALID;
  93. } catch (IOException e) {
  94. type = AudioType.INVALID;
  95. }
  96. return type;
  97. }
  98. /**
  99. * Play the file as a wav file, using the Applet class.
  100. * (This code seems to work better than the non-applet version, but can't play
  101. * streams)
  102. */
  103. private void playWav() {
  104. try {
  105. final AudioClip ac = Applet.newAudioClip(myFile.toURI().toURL());
  106. if (ac != null) {
  107. ac.play();
  108. }
  109. } catch (MalformedURLException e) {
  110. }
  111. }
  112. }