PDA

View Full Version : วิธีเล่นไฟล์เสียง (Play Audio in Applications java)



asylu3
10-12-2003, 05:25 PM
การเล่นเสียงใน Java นั้นถ้าเป็น java applet นั้นง่ายแสนง่ายแต่ว่าสำหรับแบบ application แล้วยุ่งยากพอสมควรเพราะจากตัวอย่างที่ sun ยกตัวอย่างไว้ค่อนข้างซับซ้อน ผมจึงพยายามเขียนตัวที่ง่ายๆขึ้นมากให้ลองเอาไปใช้กัน

เวลาเอาไปใช้จริงให้เปลี่ยนชื่อ file ตามต้องการนะครับ

Let's Check out this source code


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();
}

};

asylu3
10-12-2003, 05:44 PM
การเล่นเสียงแบบต่อเนื่อง



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();
}

};