# Java PlaySound
播放 mp3 音乐,需要添加 mp3plugin.jar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.jbn.example.other;
import javax.sound.sampled.*; import java.io.IOException; import java.io.InputStream;
public class PlaySound { public static void main(String ... args){ try { Clip clip = AudioSystem.getClip(); InputStream inputStream = PlaySound.class.getClassLoader().getResourceAsStream("sound/snake.wav"); AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputStream); clip.open(audioInputStream); clip.start(); clip.loop(Clip.LOOP_CONTINUOUSLY); Thread.sleep(100000); clip.stop(); } catch (LineUnavailableException | UnsupportedAudioFileException | IOException | InterruptedException e) { e.printStackTrace(); } } }
|