java 播放 wav格式的声音

import javax.sound.sampled.AudioFormat
import javax.sound.sampled.AudioInputStream
import javax.sound.sampled.AudioSystem
import javax.sound.sampled.DataLine
import javax.sound.sampled.SourceDataLine

// <copyright file="Test.groovy" company="SuperSight">
//     ©2013 SuperSight Corporation. All rights reserved.	
// </copyright> 

/**
 * Created with IntelliJ IDEA.
 * User: yuekun
 * Date: 13-7-23
 * Time: 下午2:34
 * To change this template use File | Settings | File Templates.
 */
class Test {
    public static void main(String[] args) {
        try {
            AudioInputStream ais = AudioSystem.getAudioInputStream(new File("D:\\notify.wav"));// 获得音频输入流
            AudioFormat baseFormat = ais.getFormat();// 指定声音流中特定数据安排
            System.out.println("baseFormat="+baseFormat);
            DataLine.Info info = new DataLine.Info(SourceDataLine.class,
                    baseFormat);
            System.out.println("info="+info);
            SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
            // 从混频器获得源数据行
            System.out.println("line="+line);
            line.open(baseFormat);// 打开具有指定格式的行,这样可使行获得所有所需的系统资源并变得可操作。
            line.start();// 允许数据行执行数据 I/O
            int BUFFER_SIZE = 4000 * 4;
            int intBytes = 0;
            byte[] audioData = new byte[BUFFER_SIZE];
            while (intBytes != -1) {
                intBytes = ais.read(audioData, 0, BUFFER_SIZE);// 从音频流读取指定的最大数量的数据字节,并将其放入给定的字节数组中。
                if (intBytes >= 0) {
                    int outBytes = line.write(audioData, 0, intBytes);// 通过此源数据行将音频数据写入混频器。
                }
            }

        } catch (Exception e) {

        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值