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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  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.applet.Applet;
  24. import java.applet.AudioClip;
  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.net.MalformedURLException;
  28. import javax.sound.sampled.AudioInputStream;
  29. import javax.sound.sampled.AudioSystem;
  30. import javax.sound.sampled.UnsupportedAudioFileException;
  31. /**
  32. * The AudioPlayer handles the playing of the audio
  33. */
  34. public final class AudioPlayer implements Runnable {
  35. /** The AudioType enum */
  36. public enum AudioType {
  37. WAV, INVALID
  38. }
  39. /** The file object of the file to play */
  40. final File myFile;
  41. /**
  42. * Create the AudioPlayer
  43. *
  44. * @param file The file to play
  45. */
  46. public AudioPlayer(final File file) {
  47. myFile = file;
  48. }
  49. /**
  50. * Play this AudioPlayer.
  51. */
  52. public void play() {
  53. new Thread(this).start();
  54. }
  55. /**
  56. * Run this AudioPlayer (Should not be invoked directly).
  57. */
  58. @Override
  59. public void run() {
  60. final AudioType type = getAudioType(myFile);
  61. switch (type) {
  62. case WAV:
  63. playWav();
  64. break;
  65. default:
  66. break;
  67. }
  68. }
  69. /**
  70. * Check if this File is a supported file type
  71. *
  72. * @param file the File to check
  73. *
  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. *
  85. * @return AudioType for this file.
  86. */
  87. public static AudioType getAudioType(final File file) {
  88. try (AudioInputStream unused = AudioSystem.getAudioInputStream(file)) {
  89. return AudioType.WAV;
  90. } catch (UnsupportedAudioFileException | IOException e) {
  91. return AudioType.INVALID;
  92. }
  93. }
  94. /**
  95. * Play the file as a wav file, using the Applet class. (This code seems to work better than the
  96. * non-applet version, but can't play streams)
  97. */
  98. private void playWav() {
  99. try {
  100. final AudioClip ac = Applet.newAudioClip(myFile.toURI().toURL());
  101. if (ac != null) {
  102. ac.play();
  103. }
  104. } catch (MalformedURLException e) {
  105. }
  106. }
  107. }