java 播放midi,wav,mp3
2007-06-02 23:18
176 查看
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import java.io.IOException;
import java.io.File;
public class BasicPlayer {
private AudioInputStream stream = null;
private AudioFormat format = null;
private Clip clip = null;
private SourceDataLine m_line;
public void play(File fileName,int itemStatus)
{
try {
// From file
stream = AudioSystem.getAudioInputStream(fileName);
// At present, ALAW and ULAW encodings must be converted
// to PCM_SIGNED before it can be played
format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
16,
format.getChannels(),
format.getChannels() * 2,
format.getSampleRate(),
false); // big endian
stream = AudioSystem.getAudioInputStream(format, stream);
}
// Create the clip
DataLine.Info info = new DataLine.Info(SourceDataLine.class, stream.getFormat(), AudioSystem.NOT_SPECIFIED);
m_line = (SourceDataLine) AudioSystem.getLine(info);
m_line.open(stream.getFormat(),m_line.getBufferSize());
m_line.start();
int numRead = 0;
byte[] buf = new byte[m_line.getBufferSize()];
while ((numRead = stream.read(buf, 0, buf.length)) >= 0) {
int offset = 0;
while (offset < numRead) {
offset += m_line.write(buf, offset, numRead-offset);
}
}
m_line.drain();
m_line.stop();
m_line.close();
stream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
public double getDuration()
{
return m_line.getBufferSize() /
(m_line.getFormat().getFrameSize() * m_line.getFormat().getFrameRate());
}
public double getDecision()
{
return m_line.getMicrosecondPosition()/1000.0;
}
}
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import java.io.IOException;
import java.io.File;
public class BasicPlayer {
private AudioInputStream stream = null;
private AudioFormat format = null;
private Clip clip = null;
private SourceDataLine m_line;
public void play(File fileName,int itemStatus)
{
try {
// From file
stream = AudioSystem.getAudioInputStream(fileName);
// At present, ALAW and ULAW encodings must be converted
// to PCM_SIGNED before it can be played
format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
16,
format.getChannels(),
format.getChannels() * 2,
format.getSampleRate(),
false); // big endian
stream = AudioSystem.getAudioInputStream(format, stream);
}
// Create the clip
DataLine.Info info = new DataLine.Info(SourceDataLine.class, stream.getFormat(), AudioSystem.NOT_SPECIFIED);
m_line = (SourceDataLine) AudioSystem.getLine(info);
m_line.open(stream.getFormat(),m_line.getBufferSize());
m_line.start();
int numRead = 0;
byte[] buf = new byte[m_line.getBufferSize()];
while ((numRead = stream.read(buf, 0, buf.length)) >= 0) {
int offset = 0;
while (offset < numRead) {
offset += m_line.write(buf, offset, numRead-offset);
}
}
m_line.drain();
m_line.stop();
m_line.close();
stream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
public double getDuration()
{
return m_line.getBufferSize() /
(m_line.getFormat().getFrameSize() * m_line.getFormat().getFrameRate());
}
public double getDecision()
{
return m_line.getMicrosecondPosition()/1000.0;
}
}
相关文章推荐
- java 播放midi,wav,mp3
- iOS利用AudioQueue播放音频文件(mp3,aac,caf,wav等)
- JAVA播放MP3的完整代码---简单写个小例子
- java 获取MP3文件播放时长
- java中的MP3播放核心代码
- java获取Mp3播放时长
- Java工具类之音频播放与mp3转pcm-yellowcong
- 基于JMF下的Java的MP3播放
- Java利用jaudiotagger.jar获取Mp3的播放时长
- 使用windows API播放声音文件(wav、mp3)等
- [java]查找老歌,播放m3u列表中的每个midi的开头部分
- java 播放 wav 声音文件
- 录音播放系统(实现pcm转码mp3,wav)
- 搜集了一段播放用java播放.wav的代码,可以直接使用
- Android 播放mp3 tag乱码分析之java 篇
- JAVA播放MP3的完整代码---简单写个小例子
- Linux Ubuntu 使用play命令播放音乐 mp3 wav 等文件
- 用Windows Media Player控件播放音乐图文教程(MP3,WAV,WMV)
- 用mciSendString制作可播放MPEG,AVI,WAV,MP3等的播放器
- 用mciSendString制作可播放MPEG,AVI,WAV,MP3等的播放器