码迷,mamicode.com
首页 > 编程语言 > 详细

java音频播放

时间:2014-07-06 14:43:36      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:http   java   数据   os   art   for   

package com.hongyuan.test;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

public class MusicTest {http://www.huiyi8.com/yinxiao/音效下载

public static final String MUSIC_FILE = "相逢一笑.wav";

public static void main(String[] args) throws LineUnavailableException,
UnsupportedAudioFileException, IOException {

// 获取音频输入流
AudioInputStream audioInputStream = AudioSystem
.getAudioInputStream(new File(MUSIC_FILE));
// 获取音频编码对象
AudioFormat audioFormat = audioInputStream.getFormat();

// 设置数据输入
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,
audioFormat, AudioSystem.NOT_SPECIFIED);
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem
.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();

/*
* 从输入流中读取数据发送到混音器
*/
int count;
byte tempBuffer[] = new byte[1024];
while ((count = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) {
if (count > 0) {
sourceDataLine.write(tempBuffer, 0, count);
}
}

// 清空数据缓冲,并关闭输入
sourceDataLine.drain();
sourceDataLine.close();

java音频播放,布布扣,bubuko.com

java音频播放

标签:http   java   数据   os   art   for   

原文地址:http://www.cnblogs.com/cjings/p/3825578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!