123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- /*
- * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.audio;
-
- import javax.sound.sampled.AudioInputStream;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.AudioFormat;
- import javax.sound.sampled.SourceDataLine;
- import javax.sound.sampled.DataLine;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.applet.AudioClip;
- import java.applet.Applet;
-
- /**
- * The AudioPlayer handles the playing of the audio
- *
- * @author Shane "Dataforce" Mc Cormack
- * @version $Id: AudioPlayer.java 969 2007-04-30 18:38:20Z ShaneMcC $
- */
- public final class AudioPlayer implements Runnable {
- /** The AudioType enum */
- private enum AudioType { WAV, OTHER; }
-
- /** The file object of the file to play */
- final File myFile;
-
- /**
- * Create the AudioPlayer
- *
- * @param file The file to play
- */
- public AudioPlayer(final File file) {
- myFile = file;
- }
-
- /**
- * Play this AudioPlayer.
- */
- public void play() {
- new Thread(this).start();
- }
-
- /**
- * Run this AudioPlayer (Should not be invoked directly).
- */
- public void run() {
- final AudioType type = getAudioType(myFile);
- switch (type) {
- case WAV:
- playWav();
- break;
- default:
- break;
- }
- }
-
- /**
- * Check if this File is a supported file type
- *
- * @param file the File to check
- * @return true if playable, else false.
- */
- public static boolean isValid(final File file) {
- final AudioType type = getAudioType(file);
- return (type != AudioType.OTHER);
- }
-
- /**
- * Get the AudioType of a given file
- *
- * @param file the File to check
- * @return AudioType for this file.
- */
- public static AudioType getAudioType(final File file) {
- AudioType type;
- try {
- AudioSystem.getAudioInputStream(file);
- type = AudioType.WAV;
- } catch (Exception e) {
- type = AudioType.OTHER;
- }
- return type;
- }
-
-
- /**
- * Play the file as a wav file, using the Applet class.
- * (This code seems to work better than the non-applet version, but can't play
- * streams)
- */
- private void playWav() {
- try {
- final AudioClip ac = (new Applet()).newAudioClip(myFile.toURI().toURL());
- if (ac != null) { ac.play(); }
- } catch (Exception e) { /* Bad File, can't play */ }
- }
-
- /**
- * Play the file as a wav file.
- * Based on http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml
- */
- private void oldPlayWav() {
- final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb
- AudioInputStream audioInputStream = null;
- try {
- audioInputStream = AudioSystem.getAudioInputStream(myFile);
- } catch (Exception e) { return; }
- AudioFormat format = audioInputStream.getFormat();
-
- SourceDataLine auline = null;
- DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
-
- try {
- auline = (SourceDataLine) AudioSystem.getLine(info);
- auline.open(format);
- } catch (Exception e) { return; }
-
- auline.start();
- int nBytesRead = 0;
- byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
-
- try {
- while (nBytesRead != -1) {
- nBytesRead = audioInputStream.read(abData, 0, abData.length);
- int offset = 0;
- while (offset < nBytesRead) {
- offset += auline.write(abData, offset, nBytesRead-offset);
- }
- }
- } catch (Exception e) {
- /** Do Nothing */
- } finally {
- auline.drain();
- auline.close();
- }
- try {
- audioInputStream.close();
- } catch (Exception e) { }
- }
- }
|