|
|
AudioChannelPlayer Java code example - Click here to copy ->>>
Can't find what you're looking for? Try our search:
|
|
Really working examples categorized by API, package, class. You can compile and run our examples right away!
Not from source code for Java projects - only working examples! Copy, compile and run!
|
| Code: |
/*
* AudioChannelPlayer.java
*
* This file is part of the Java Sound Examples.
*/
/*
* Copyright (c) 2000 by Matthias Pfisterer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
|<--- this code is formatted to fit into 80 columns --->|
*/
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.BooleanControl;
/**
* AudioChannelPlayer
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* OLD DO*****ENTATION:
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* AudioChannel -- playing sound files in a sequence
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* AudioChannel manages a queue of AudioInputStreams to send
* <p/>
* them to the same line. In the constructor of
* <p/>
* AudioChannel, an AudioFormat has to be passed. A Line is
* <p/>
* retrieved for this format. AudioInputStreams having a
* <p/>
* different format are automatically converted during
* <p/>
* playback. This class is in an experimental state. Please
* <p/>
* report problems.
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* AudioChannelPlayer is a command-line program that shows
* <p/>
* how to use AudioChannel. It plays all sound files whose
* <p/>
* names are given as command line arguments in sequence.
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* <p/>
* Source code: AudioChannel.java,
* <p/>
* Source code: AudioChannelPlayer.java
*/
public class AudioChannelPlayer {
private static final boolean DEBUG = true;
private static final int BUFFER_SIZE = 16384;
public static void main(String[] args)
{
// TODO: set AudioFormat after the first soundfile
AudioFormat audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
44100.0F, 16, 2, 4, 44100.0F, true);
SourceDataLine line = null;
try
{
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(audioFormat, line.getBufferSize());
}
catch (LineUnavailableException e)
{
e.printStackTrace();
}
line.start();
AudioChannel channel = new AudioChannel(line);
channel.start();
for (int nArgPos = 0; nArgPos < args.length; nArgPos++)
{
if (args[nArgPos].startsWith("-s"))
{
String strDuration = args[nArgPos].substring(2);
int nDuration = Integer.parseInt(strDuration);
handleSilence(nDuration, channel);
}
else
{
handleFile(args[nArgPos], channel);
}
}
// TODO: instead of waiting a fixed amount of time, wait until the queue of AudioChannel is empty.
try
{
Thread.sleep(10000);
}
catch (InterruptedException e)
{
}
}
private static void handleFile(String strFilename, AudioChannel channel)
{
File audioFile = new File(strFilename);
AudioInputStream audioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputStream(audioFile);
}
catch (Exception e)
{
/*
* In case of an exception, we dump the exception
* including the stack trace to the console output.
* Then, we exit the program.
*/
e.printStackTrace();
System.exit(1);
}
if (audioInputStream != null)
{
boolean bSuccessfull = channel.addAudioInputStream(audioInputStream);
if (! bSuccessfull)
{
out("Warning: could not enqueue AudioInputStream; presumably formats don't match!");
}
}
}
private static void handleSilence(int nDuration, AudioChannel channel)
{
}
private static void out(String strMessage)
{
System.out.println(strMessage);
}
}
|
|
|
References.
The list of classes which were used on this page you can find below. The
links to Java API contain official SUN documentation about all used classes.
[ Go Back ]
|
|