Bekijk de onderstaande video om te zien hoe je onze site als een web app op je startscherm installeert.
Opmerking: Deze functie is mogelijk niet beschikbaar in sommige browsers.
hoe kan ik geluid in een desktop applicatie plaatsen?
Ter informatie, ik gebruik Macintosh(zou niet uit mogen maken)
import java.io.File;
import java.io.IOException;
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 WavPlayer {
private AudioFormat audioFormat;
private AudioInputStream audioInputStream;
private SourceDataLine sourceDataLine;
private boolean stopPlayback = false;
private boolean loop = false;
private String filePath;
public void playAudio(String filePath, boolean loop) {
try {
stopPlayback = false;
this.loop = loop;
this.filePath = filePath;
new PlayThread().start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopAudio() {
stopPlayback = true;
}
class PlayThread extends Thread {
@Override
public void run() {
try {
playFileInLoop();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sourceDataLine != null) {
sourceDataLine.drain();
sourceDataLine.close();
}
}
}
private void playFileInLoop() throws UnsupportedAudioFileException, LineUnavailableException, IOException {
try {
audioInputStream = AudioSystem.getAudioInputStream(new File(filePath));
} catch (IOException ex) {
audioInputStream = AudioSystem.getAudioInputStream(WavPlayer.class.getResourceAsStream(filePath));
}
audioFormat = audioInputStream.getFormat();
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
byte tempBuffer[] = new byte[10000];
int cnt;
while ((cnt = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) != -1
&& !stopPlayback) {
if (cnt > 0) {
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
if(!stopPlayback && loop){
playFileInLoop();
}
}
}
}
We gebruiken essentiële cookies om deze site te laten werken, en optionele cookies om de ervaring te verbeteren.