// 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( );
java 播放声音文件
最新推荐文章于 2024-05-27 21:09:01 发布