|
|
AudioChannel 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: |
/*
* AudioChannel.java
*
* This file is part of the Java Sound Examples.
*/
/*
* Copyright (c) 1999, 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.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;
// IDEA: can this class be derived from AudioStream??
public class AudioChannel
extends Thread {
private static final boolean DEBUG = true;
private static final int BUFFER_SIZE = 16384;
private List m_audioStreamQueue;
private SourceDataLine m_line;
private byte[] m_dataArray;
/*
* Uses the passed Mixer.
*/
public AudioChannel(SourceDataLine line) {
super("AudioChannel");
/* Setting this thread to daemon means that this thread
doesn't prevent the VM from exiting even if it is
still running.
*/
setDaemon(true);
// TODO: check if priority makes sense
setPriority(9);
m_line = line;
m_audioStreamQueue = new ArrayList();
// TODO: make size configurable
int nBufSizeInFrames = 0;
if (m_line.getBufferSize() > 0) {
nBufSizeInFrames = m_line.getBufferSize() / 2;
} else {
nBufSizeInFrames = 4096;
}
m_dataArray = new byte[nBufSizeInFrames];
}
public AudioFormat getFormat() {
return m_line.getFormat();
}
public boolean addAudioInputStream(AudioInputStream audioStream) {
if (DEBUG) {
out("AudioChannel.addAudioInputStream(): called.");
}
if (!getFormat().matches(audioStream.getFormat())) {
if (DEBUG) {
out("AudioChannel.addAudioInputStream(): audio formats do not match, trying to convert.");
}
AudioInputStream asold = audioStream;
audioStream = AudioSystem.getAudioInputStream(getFormat(), asold);
if (audioStream == null) {
out("### AudioChannel.addAudioInputStream(): could not convert.");
return false;
}
if (DEBUG) {
out(" converted");
}
}
synchronized (m_audioStreamQueue) {
m_audioStreamQueue.add(audioStream);
m_audioStreamQueue.notifyAll();
}
if (DEBUG) {
out("AudioChannel.addAudioInputStream(): enqueued " + audioStream);
}
return true;
}
// TODO: termination of loop
public void run() {
if (DEBUG) {
out("AudioChannel.run(): starting");
}
while (true) {
AudioInputStream audioStream = null;
synchronized (m_audioStreamQueue) {
while (m_audioStreamQueue.size() == 0) {
try {
m_audioStreamQueue.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
audioStream = (AudioInputStream) m_audioStreamQueue.remove(0);
}
if (DEBUG) {
out("AudioChannel.run(): playing " + audioStream);
}
int nBytesRead;
while (true) {
try {
nBytesRead = audioStream.read(m_dataArray);
if (nBytesRead == -1) {
// m_line.write(null, 0, 0);
break;
}
int nBytesWritten = m_line.write(m_dataArray, 0, nBytesRead);
// Contract.check(nBytesWritten == nBytesRead);
}
catch (IOException e) {
e.printStackTrace();
break;
}
}
}
}
public void closeChannel() {
// TODO:
}
public void startChannel() {
// TODO:
m_line.start();
super.start();
}
// should not block, but trigger the termination
public void stopChannel() {
// TODO: do some mystery to
// a) stop the line (without interupting current plays)
// b) stop the Thread
}
private static void out(String strMessage) {
System.out.println(strMessage);
}
}
/*** AudioChannel.java ***/
|
|
|
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 ]
|
|