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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2006-2014 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.AudioSystem;
  29. import javax.sound.sampled.UnsupportedAudioFileException;
  30. /**
  31. * The AudioPlayer handles the playing of the audio
  32. */
  33. public final class AudioPlayer implements Runnable {
  34. /** The AudioType enum */
  35. public enum AudioType {
  36. WAV, INVALID
  37. }
  38. /** The file object of the file to play */
  39. final File myFile;
  40. /**
  41. * Create the AudioPlayer
  42. *
  43. * @param file The file to play
  44. */
  45. public AudioPlayer(final File file) {
  46. myFile = file;
  47. }
  48. /**
  49. * Play this AudioPlayer.
  50. */
  51. public void play() {
  52. new Thread(this).start();
  53. }
  54. /**
  55. * Run this AudioPlayer (Should not be invoked directly).
  56. */
  57. @Override
  58. public void run() {
  59. final AudioType type = getAudioType(myFile);
  60. switch (type) {
  61. case WAV:
  62. playWav();
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. /**
  69. * Check if this File is a supported file type
  70. *
  71. * @param file the File to check
  72. *
  73. * @return true if playable, else false.
  74. */
  75. public static boolean isValid(final File file) {
  76. final AudioType type = getAudioType(file);
  77. return (type != AudioType.INVALID);
  78. }
  79. /**
  80. * Get the AudioType of a given file
  81. *
  82. * @param file the File to check
  83. *
  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 | IOException e) {
  92. type = AudioType.INVALID;
  93. }
  94. return type;
  95. }
  96. /**
  97. * Play the file as a wav file, using the Applet class. (This code seems to work better than the
  98. * non-applet version, but can't play streams)
  99. */
  100. private void playWav() {
  101. try {
  102. final AudioClip ac = Applet.newAudioClip(myFile.toURI().toURL());
  103. if (ac != null) {
  104. ac.play();
  105. }
  106. } catch (MalformedURLException e) {
  107. }
  108. }
  109. }