วิธีเล่นไฟล์เสียง (Play Audio in Applications java)
การเล่นเสียงใน Java นั้นถ้าเป็น java applet นั้นง่ายแสนง่ายแต่ว่าสำหรับแบบ application แล้วยุ่งยากพอสมควรเพราะจากตัวอย่างที่ sun ยกตัวอย่างไว้ค่อนข้างซับซ้อน ผมจึงพยายามเขียนตัวที่ง่ายๆขึ้นมากให้ลองเอาไปใช้กัน
เวลาเอาไปใช้จริงให้เปลี่ยนชื่อ file ตามต้องการนะครับ
Let's Check out this source code
[src]
import sun.audio.*; //import the sun.audio package
import java.io.*;
public class KengSound
{
KengSound()
{
try{
InputStream in = new FileInputStream("flute+hrn+mrmba.aiff");
AudioStream sound = new AudioStream(in);
AudioPlayer.player.start(sound );
} catch(IOException e ) {
}
}
public static void main(String args[])
{
new KengSound();
}
};[/src]
Re: วิธีเล่นไฟล์เสียง (Play Audio in Applications java)
การเล่นเสียงแบบต่อเนื่อง
[src]
import sun.audio.*; //import the sun.audio package
import java.io.*;
public class KengSound
{
KengSound()
{
try{
InputStream in = new FileInputStream("bottle-open.wav");
AudioStream as = new AudioStream (in);
AudioData data = as.getData();
ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);
AudioPlayer.player.start(cas);
// AudioPlayer.player.stop(cas); //ยกเลิกบรรทัดนี้ เพื่อหยุดเสียง
} catch(IOException e ) {
}
}
public static void main(String args[])
{
new KengSound();
}
};
[/src]