Java Clip 播放音频

时间:2024-02-22 21:08:00
package com.xu.music.player.player; import cn.hutool.core.text.CharSequenceUtil; import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.DataLine; import java.io.File; import java.net.URL; /** * Java 音频播放 * * @author hyacinth * @date 2019年10月31日19:06:39 */ public class NPlayer implements Player { private Clip clip; private volatile long position = 0; private volatile boolean paused = false; private NPlayer() { } public static NPlayer createPlayer() { return NPlayer.SingletonHolder.player; } private static class SingletonHolder { private static final NPlayer player = new NPlayer(); } @Override public void load(URL url) throws Exception { load(AudioSystem.getAudioInputStream(url)); } @Override public void load(File file) throws Exception { String name = file.getName(); if (CharSequenceUtil.endWithIgnoreCase(name, ".mp3")) { AudioInputStream stream = new MpegAudioFileReader().getAudioInputStream(file); AudioFormat format = stream.getFormat(); format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), 16, format.getChannels(), format.getChannels() * 2, format.getSampleRate(), false); stream = AudioSystem.getAudioInputStream(format, stream); load(stream); } else if (CharSequenceUtil.endWithIgnoreCase(name, ".flac")) { AudioInputStream stream = AudioSystem.getAudioInputStream(file); AudioFormat format = stream.getFormat(); format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), 16, format.getChannels(), format.getChannels() * 2, format.getSampleRate(), false); stream = AudioSystem.getAudioInputStream(format, stream); load(stream); } else { load(AudioSystem.getAudioInputStream(file)); } } @Override public void load(String path) throws Exception { load(new File(path)); } @Override public void load(AudioInputStream stream) throws Exception { DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(), AudioSystem.NOT_SPECIFIED); clip = (Clip) AudioSystem.getLine(info); clip.addLineListener(event -> { System.out.println(event.getType() + "\t" + event.getFramePosition()); }); clip.open(stream); } @Override public void load(AudioFormat.Encoding encoding, AudioInputStream stream) throws Exception { load(AudioSystem.getAudioInputStream(encoding, stream)); } @Override public void load(AudioFormat format, AudioInputStream stream) throws Exception { load(AudioSystem.getAudioInputStream(format, stream)); } @Override public void pause() { if (clip != null && clip.isRunning()) { paused = true; position = clip.getMicrosecondPosition(); clip.stop(); } } @Override public void resume() { if (clip != null && paused) { paused = false; clip.setMicrosecondPosition(position); clip.start(); } } @Override public void play() { if (clip != null) { clip.start(); clip.drain(); } } @Override public void stop() { if (clip != null) { clip.stop(); clip.close(); } } }