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

你可能感兴趣的:(java)