24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 594  |  回复: 0

wangjing1377

金虫 (小有名气)

[求助] Java中添加音频文件 总是出错

package com.test2;
import java.io.*;
//import javax.sound.sampled.*;

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;
public class TestAudio {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                //1.先创建一个AePlayWave对象实例
                AePlayWave apw = new AePlayWave("d:\\111.wav\\";

                //2.启动该线程并播放
                apw.start();
        }
}
//播放声音的类
class AePlayWave extends Thread {
        private String filename;
        public AePlayWave(String wavfile) {
                filename = wavfile;
        }
        public void run(){
                File soundFile=new File(filename);
                AudioInputStream audioInputStream = null;
                try{
               
                                audioInputStream =AudioSystem.getAudioInputStream(soundFile);
               
                }catch(Exception e1){
                        e1.printStackTrace();
                        return;
                }
                AudioFormat format =  audioInputStream.getFormat();
                SourceDataLine auline=null;
                DataLine.Info info=new DataLine.Info(SourceDataLine.class, format);
                try{
                        auline = (SourceDataLine) AudioSystem.getLine(info);
                        auline.open(format);
                }catch(Exception e){
                        e.printStackTrace();
                        return;
                }
                auline.start();
                int nBytesRead = 0;
                //这是缓冲
                byte [] abData=new byte[1024];
                try{
                        while(nBytesRead != -1){
                                nBytesRead=audioInputStream.read(abData, 0,abData.length);
                                if(nBytesRead>=0)
                                        auline.write(abData, 0, nBytesRead);
                        }
                }catch(IOException e){
                        e.printStackTrace();
                        return;
                }finally{
                        auline.drain();
                        auline.close();
                }
        }
}



javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
        at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
        at com.test2.AePlayWave.run(TestAudio.java:32)
回复此楼
加油
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 wangjing1377 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 336求调剂,一志愿中科大 +5 墨彧 yuyu 2026-04-06 5/250 2026-04-06 21:24 by 52305043001
[考研] 319求调剂 +3 handrui 2026-04-05 3/150 2026-04-06 09:33 by jp9609
[考研] 一志愿南昌大学,085600,344分求调剂 +9 调剂上岸玘 2026-04-05 10/500 2026-04-06 09:30 by dongzh2009
[考研] 085600,320分求调剂 +7 大馋小子 2026-04-01 8/400 2026-04-05 21:19 by 学员8dgXkO
[考研] 工科08-机械专硕-求调剂 +3 雷欧飞踢 2026-04-02 3/150 2026-04-05 18:49 by 蓝云思雨
[考研] 一志愿同济大学323分(080500)求调剂 +8 yikeniu 2026-04-01 8/400 2026-04-05 18:15 by cql1109
[考研] 083200 333求调剂 +3 十二!! 2026-04-04 3/150 2026-04-05 08:28 by barlinike
[考研] 考研调剂 +11 小sun要好运 2026-04-04 11/550 2026-04-05 08:02 by qlm5820
[考研] 26考研调剂0710 0860 +9 补补不补 2026-04-03 14/700 2026-04-04 23:32 by 果冻大王
[考研] 材料求调剂 +10 呢呢妮妮 2026-04-01 10/500 2026-04-04 23:12 by 无际的草原
[考研] 一志愿沪9,求生物学调剂,326分 +6 刘墨墨 2026-04-04 6/300 2026-04-04 19:44 by 唐沐儿
[考研] 085602 找调剂 +4 逆时针快乐 2026-04-02 4/200 2026-04-04 19:32 by 蓝云思雨
[考研] 348分环境工程·调剂 +10 吴彦祖24k 2026-04-03 11/550 2026-04-04 14:19 by 无际的草原
[考研] 一志愿085404,总分291,四级已过,求调剂 +5 阿俊阿俊阿俊 2026-04-04 7/350 2026-04-04 13:23 by 莲菜就是藕吧
[考研] 387求调剂 +4 爱吃片豆土 2026-04-03 5/250 2026-04-04 08:10 by 岸上的一条鱼
[考研] 085601一志愿北理325分求调剂 +6 找调剂,, 2026-04-02 6/300 2026-04-03 22:20 by 柟?
[考研] 五邑大学土木工程招调剂生2026 +3 wyutj 2026-03-31 4/200 2026-04-03 18:21 by zengxj_7201
[考研] 295求调剂 +7 愿旅途永远坦然 2026-04-02 7/350 2026-04-03 08:22 by fangshan711
[考研] 一志愿陕西师范大学生物学317分 +5 1563日。 2026-04-02 5/250 2026-04-03 06:58 by ilovexiaobin
[考研] 安全工程 285 求调剂 +3 Xinyu56 2026-04-01 4/200 2026-04-01 21:50 by 静静静静静静静
信息提示
请填处理意见