读书人

java 播发声音文件

发布时间: 2013-11-08 17:51:58 作者: rapoo

java 播放声音文件

// 1. Access the audio file as a stream
??? AudioInputStream stream = AudioSystem.getAudioInputStream(
??????????????????????????????????? getClass( ).getResource(fnm) );
??

??? // 2. Get the audio format for the data in the stream
??? AudioFormat format = stream.getFormat( );

??? // 3. Gather information for line creation
??? DataLine.Info info = new DataLine.Info(Clip.class, format);

??? // 4. Create an empty clip using that line information
??? Clip clip = (Clip) AudioSystem.getLine(info);

??? // 5. Start monitoring the clip's line events
??? clip.addLineListener(this);

??? // 6. Open the audio stream as a clip; now it's ready to play
??? clip.open(stream);
??? stream.close( );

读书人网 >编程

热点推荐