001: /*
002: * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
003: *
004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005: * royalty free, license to use, modify and redistribute this
006: * software in source and binary code form,
007: * provided that i) this copyright notice and license appear on all copies of
008: * the software; and ii) Licensee does not utilize the software in a manner
009: * which is disparaging to Silvere Martin-Michiellot.
010: *
011: * This software is provided "AS IS," without a warranty of any kind. ALL
012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024: *
025: * This software is not designed or intended for use in on-line control of
026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027: * the design, construction, operation or maintenance of any nuclear
028: * facility. Licensee represents and warrants that it will not use or
029: * redistribute the Software for such purposes.
030: *
031: */
032:
033: package com.db.media.audio;
034:
035: import java.io.*;
036: import javax.sound.sampled.*;
037:
038: // This code is repackaged after the code from Florian Bomers & Matthias Pfisterer from JavaSound Examples
039: // Site http://www.jsresources.org/examples/
040: // Email tritonus-user@lists.sourceforge.net
041:
042: //Plays a single audio file.
043: //Only PCM encoded files are supported. A-law, U-law,
044: //ADPCM, mp3 and other compressed data formats are not supported.
045:
046: public class SimpleAudioPlayer {
047:
048: private static final int EXTERNAL_BUFFER_SIZE = 128000;
049:
050: public static void main(String[] args) {
051:
052: if (args.length != 1) {
053: System.out.println("SimpleAudioPlayer: usage:");
054: System.out.println("\tjava SimpleAudioPlayer <soundfile>");
055: System.exit(1);
056: }
057:
058: String strFilename = args[0];
059: File soundFile = new File(strFilename);
060:
061: AudioInputStream audioInputStream = null;
062: try {
063: audioInputStream = AudioSystem
064: .getAudioInputStream(soundFile);
065: } catch (Exception e) {
066: e.printStackTrace();
067: System.exit(1);
068: }
069:
070: AudioFormat audioFormat = audioInputStream.getFormat();
071:
072: SourceDataLine line = null;
073: DataLine.Info info = new DataLine.Info(SourceDataLine.class,
074: audioFormat);
075: try {
076: line = (SourceDataLine) AudioSystem.getLine(info);
077: line.open(audioFormat);
078: } catch (Exception e) {
079: e.printStackTrace();
080: System.exit(1);
081: }
082:
083: line.start();
084:
085: int nBytesRead = 0;
086: byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
087: while (nBytesRead != -1) {
088: try {
089: nBytesRead = audioInputStream.read(abData, 0,
090: abData.length);
091: } catch (IOException e) {
092: e.printStackTrace();
093: }
094: if (nBytesRead >= 0) {
095: int nBytesWritten = line.write(abData, 0, nBytesRead);
096: }
097: }
098:
099: line.drain();
100: line.close();
101:
102: System.exit(0);
103: }
104:
105: }
|