jodeman
Legacy Member
Yo allemaal,
Heb nog nooit gewerkt met geluid en ik krijg het maar niet goed. Gewoon geluid afspelen lukt me wel maar ik wil nu in plaats van twee geluiden achter elkaar afspelen, twee geluiden door mekaar afspelen. Heb alles threaded gemaakt maar nog stees werkt het niet. Kan iemand mij verder helpen?
Thanks.
SoundController.java
SoundThread.java
Run.java
Heb nog nooit gewerkt met geluid en ik krijg het maar niet goed. Gewoon geluid afspelen lukt me wel maar ik wil nu in plaats van twee geluiden achter elkaar afspelen, twee geluiden door mekaar afspelen. Heb alles threaded gemaakt maar nog stees werkt het niet. Kan iemand mij verder helpen?
Thanks.
SoundController.java
Code:
package core;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
public class SoundController implements Runnable {
private static final int defaultThreadAmount = 5;
private Hashtable<String,String> sounds;
private ArrayList<SoundThread> soundThreads;
private String soundPath;
private AudioFormat format;
private byte[] samples;
/**
* Construcor for soundcontroller
*/
public SoundController() {
sounds = new Hashtable<String,String>();
soundThreads = new ArrayList<SoundThread>();
soundPath = "";
format = null;
loadThreads(defaultThreadAmount);
}
/**
* Play sound with existing tag
* @param tag
*/
public void play(String tag) {
String filename = (String) sounds.get(tag);
loadSound(filename);
play();
}
/**
* Plays a sound on a soundThread that isn't occupied
* if all soundThreads are used, the sound isn't played
*/
public void play() {
run();
}
/**
* Implementing runnable interface
*/
public void run() {
for (SoundThread st : soundThreads) {
if (!st.isRunning()) {
st.setSamples(samples);
st.setFormat(format);
st.run();
break;
}
}
}
/**
* Loads a maximum of soundThreads, this means a maximum size
* of sounds played simultaneously
* @param size is the amount of soundThreads
*/
public void loadThreads(int size) {
for (int i = 0; i < size; i++) {
soundThreads.add(new SoundThread());
}
}
/**
* Clear all soundThreads the controller owns
*/
public void clearThreads() {
for (SoundThread st : soundThreads) {
st.clear();
}
}
/**
* Add a sound with a specified tag
* @param tag represents the key name to the sound
* @param path represents the path to that particular sound
*/
public void addSound(String tag, String path) {
sounds.put(tag,path);
}
/**
* Play a sound that's already in the controller
* @param key
*/
public void loadSound(String filename) {
try {
AudioInputStream stream =
AudioSystem.getAudioInputStream(
new File(soundPath + filename));
format = stream.getFormat();
samples = getSamples(stream);
}
catch (UnsupportedAudioFileException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Gets the samples from an AudioInputStream as an array of bytes.
* @param audioStream
* @return array of bytes
*/
private byte[] getSamples(AudioInputStream audioStream) {
int length = (int) (audioStream.getFrameLength() * format
.getFrameSize());
byte[] samples = new byte[length];
DataInputStream is = new DataInputStream(audioStream);
try {
is.readFully(samples);
} catch (IOException ex) {
ex.printStackTrace();
}
return samples;
}
public String getSoundPath() {
return soundPath;
}
public void setSoundPath(String soundPath) {
this.soundPath = soundPath;
}
}
SoundThread.java
Code:
package core;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
public class SoundThread implements Runnable {
private AudioFormat format;
private byte[] samples;
private boolean running;
/**
* Constructor that initializes all the elements necessary
*/
public SoundThread() {
this.clear();
}
/**
* Plays the specified ByteArrayInputStream
*/
public void play() {
running = true;
InputStream source = new ByteArrayInputStream(samples);
int bufferSize = format.getFrameSize()
* Math.round(format.getSampleRate() / 10);
byte[] buffer = new byte[bufferSize];
SourceDataLine line;
try {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, bufferSize);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
return;
}
line.start();
try {
int numBytesRead = 0;
while (numBytesRead != -1) {
numBytesRead = source.read(buffer, 0, buffer.length);
if (numBytesRead != -1) {
line.write(buffer, 0, numBytesRead);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
line.drain();
line.close();
running = false;
}
/**
* Specify samples for SoundThread
* @param samples
*/
public void setSamples(byte[] samples) {
this.samples = samples;
}
/**
* Specify format for SoundThread
* @param samples
*/
public void setFormat(AudioFormat format) {
this.format = format;
}
/**
* Specify samples for SoundThread
* @param samples
*/
public void clear() {
this.format = null;
this.samples = null;
this.running = false;
}
/**
* Runnable interface implemented
*/
public void run() {
play();
}
/**
* Returns wether the SoundThread is being used or not
* @return running or not running
*/
public boolean isRunning() {
return running;
}
}
Run.java
Code:
package core;
public class Run {
public static void main(String[] args) {
SoundController sc = new SoundController();
sc.setSoundPath("core/");
sc.loadSound("click.wav");
sc.play();
sc.play();
}
}
].
!