Potreboval by som do mojho programu pridat zvuky. Teda aby sa pri istej akcii prehral jeden zvuk (a vzdy pri tej akcii aby sa prehral)
Nasiel som an nete neico, ale vsetko bolo pre Applet a ja to potrebujem pre obycajnu Java aplikaciu nie Applet. Najlepsie nejaky jednoduchy priklad programu, ktory prehra zvuk zeby som si to podla toho spravil.
Alebo nasiel som nieco kde je ale spraveny loop a prehrava sa to tym padom stale dookola a to nechcem.
Diki
JAVA - zvuky
-
ultimate222
Light Star
- Príspevky: 263
- Registrovaný: 10 júl 2011, 14:17
- Bydlisko: Zvolen
Re: JAVA - zvuky
ked som to pred dvoma rokmi potreboval, tak som pouzil take nieco:
a metodu pre prehratie zvuku
Kód: Vybrať všetko
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class SoundPlayer extends Thread{
private InputStream fileStream;
private final int BUFFER_SIZE = 524288; // 128Kb
/**
* Create new instance
* @param wavfile the sound stream to playing
*/
public SoundPlayer(InputStream wavfile){
fileStream = wavfile;
}
@Override
public void run(){
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(fileStream);
}
catch (UnsupportedAudioFileException e) {
System.err.println("SoundPlayer: UnsupportedAudioException - "+e.getMessage());
return;
}
catch (IOException e) {
System.err.println("SoundPlayer: IOException - "+e.getMessage());
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 (LineUnavailableException e) {
System.err.println(e.getMessage());
return;
}
catch (Exception e) {
System.err.println(e.getMessage());
return;
}
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[BUFFER_SIZE];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0) {
auline.write(abData, 0, nBytesRead);
}
}
}
catch (IOException e) {
System.err.println(e.getMessage());
return;
}
finally {
auline.drain();
auline.close();
}
}
}Kód: Vybrať všetko
private void playSound(String pathSound){
try {
BufferedInputStream sound = new BufferedInputStream(getClass().getResource(pathSound).openStream());
Thread thread = new SoundPlayer(sound);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}-
ultimate222
Light Star
- Príspevky: 263
- Registrovaný: 10 júl 2011, 14:17
- Bydlisko: Zvolen
Re: JAVA - zvuky
dik, vyskusam to .. no myslel som ze to bude nieco jednoduchsie
Re: JAVA - zvuky
ak ti stačí prehrávať .wav a .mid skús toto:
Kód: Vybrať všetko
public void hudba() {
AudioPlayer ap = AudioPlayer.player;
AudioStream as;
AudioData ad;
AudioDataStream zvuk = null;
try {
as = new AudioStream(new FileInputStream("cesta_k_suboru"));
ad = as.getData();
zvuk = new AudioDataStream(ad);
} catch (Exception e) {
}
ap.start(zvuk);
}