Untuk menjalankannya tinggal membuat objek di class lain dengan perintah :
new dengarWav("alamat_asli.wav").start();
WARNING :
- hanya support file .wav. Format .mp3 atau .ogg tidak bisa.
- kalau dijalankan langsung dari NetBeans tidak bisa. Pasti bilangnya : file tidak ditemukan.
- jadi jalankan melalui konsol dengan perintah : java namaClass.... OK!
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.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class dengarWav extends Thread {
private String filename;
private Position curPosition;
private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb
enum Position {
LEFT, RIGHT, NORMAL
};
public dengarWav(String wavfile) {
filename = wavfile;
curPosition = Position.NORMAL;
}
public dengarWav(String wavfile, Position p) {
filename = wavfile;
curPosition = p;
}
@Override
public void run() {
File soundFile = new File(filename);
if (!soundFile.exists()) {
System.err.println("File WAV ilang : " + filename);
return;
}
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (UnsupportedAudioFileException e1) {
//e1.printStackTrace();
return;
} catch (IOException e1) {
//e1.printStackTrace();
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) {
//e.printStackTrace();
return;
} catch (Exception e) {
//e.printStackTrace();
return;
}
if (auline.isControlSupported(FloatControl.Type.PAN)) {
FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);
if (curPosition == Position.RIGHT)
pan.setValue(1.0f);
else if (curPosition == Position.LEFT)
pan.setValue(-1.0f);
}
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
auline.write(abData, 0, nBytesRead);
}
} catch (IOException e) {
//e.printStackTrace();
return;
} finally {
auline.drain();
auline.close();
}
}
}
6 komentar:
untuk format suara yang ada ga pak???
untuk suara pemanggilan nomor antrian ada gk gan???
gan, itu masukin.nya
new dengarWav("alamat_asli.wav").start();
di bagian mana ya ?
Kang mas. bisa minta program jadinya ? buat di otak - atik. terima kasih..
Disini mungkin kang Neztya Ardama Azulla :
File soundFile = new File(filename);
if (!soundFile.exists()) {
System.err.println("File WAV ilang : " + filename);
return;
}
Posting Komentar