📄 pcmplayer.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package org.serain.shmily.player;import java.awt.event.*;import java.io.*;import javax.sound.sampled.*;import javax.swing.*;/** * * @author Administrator */public class PcmPlayer { Timer timer; Clip clip; boolean playing=false; int audioLength; int audioPosition=0; public PcmPlayer(File f) throws Exception{ AudioInputStream ain=AudioSystem.getAudioInputStream(f); try{ AudioFormat format=ain.getFormat(); DataLine.Info info=new DataLine.Info(Clip.class,format); if(!AudioSystem.isLineSupported(info)){ AudioFormat pcm=new AudioFormat(format.getSampleRate(), 16,format.getChannels(), true, false); ain=AudioSystem.getAudioInputStream(pcm,ain); format=ain.getFormat(); info=new DataLine.Info(Clip.class,format); } clip=(Clip)AudioSystem.getLine(info); clip.open(ain); }finally{ ain.close(); } audioLength=(int)(clip.getMicrosecondLength()/1000); timer=new Timer(100,new ActionListener(){ public void actionPerformed(ActionEvent e){ tick(); } }); } public void play(){ clip.start(); timer.start(); playing=true; } public void stop(){ timer.stop(); clip.stop(); playing=false; } public void reset(){ stop(); clip.setMicrosecondPosition(0); audioPosition=0; } public void skip(int position){ if(position<0||position>audioLength) return; clip.setMicrosecondPosition(position*1000); } public int getLength(){ return audioLength; } public void tick(){ if(clip.isActive()){ audioPosition=(int)(clip.getMicrosecondPosition()/1000); }else{ reset(); } } public int getAudioLength() { return audioLength; } public void setAudioLength(int audioLength) { this.audioLength = audioLength; } public int getAudioPosition() { return audioPosition; } public void setAudioPosition(int audioPosition) { this.audioPosition = audioPosition; } public boolean isPlaying() { return playing; } public void setPlaying(boolean playing) { this.playing = playing; } public Timer getTimer() { return timer; } public void setTimer(Timer timer) { this.timer = timer; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -