24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 592  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 327考研调剂推荐 +5 呜呜呜呜呢 2026-04-06 5/250 2026-04-06 19:53 by 尚水阁主
[考研] 318求调剂 +12 ykyhsa 2026-04-05 14/700 2026-04-06 17:46 by fuyu_
[考研] 0703化学 +18 妮妮ninicgb 2026-04-04 20/1000 2026-04-06 17:38 by 妮妮ninicgb
[考研] 307求调剂 +3 所念及所望 2026-04-06 3/150 2026-04-06 17:30 by 土木硕士招生
[考研] 一志愿武理车辆专硕总分 281 求调剂 +3 上岸研究生. 2026-04-02 3/150 2026-04-06 16:46 by 小月儿呀呀
[考研] 一志愿国科大信工所,英二数二408总分293分求调剂 +3 ilcyuan 2026-04-02 4/200 2026-04-06 16:35 by likeihood
[考研] 专硕0854初试考材科基,求调剂 +6 3220548044 2026-04-06 9/450 2026-04-06 10:26 by barlinike
[考研] 0855求调剂材料 +11 红桃灼灼 2026-04-04 12/600 2026-04-06 10:26 by 蓝云思雨
[考研] 085410人工智能 初试316分 求调剂 +7 残星拂曙 2026-03-31 7/350 2026-04-06 10:09 by 蓝云思雨
[考研] 材料专硕(0856) 339分求调剂 +10 哈哈哈鹅哈哈哈 2026-04-05 10/500 2026-04-06 09:35 by jp9609
[考研] 求调剂 +5 chenxrlkx 2026-04-05 7/350 2026-04-06 07:54 by houyaoxu
[考研] 327求调剂 +4 拾光任染 2026-04-05 4/200 2026-04-05 20:16 by 南航~万老师
[考研] 322求调剂 +3 嗯哼哼恒 2026-04-05 3/150 2026-04-05 19:52 by nepu_uu
[考研] 272求调剂 +4 电气李 2026-04-05 4/200 2026-04-05 10:41 by lbsjt
[考研] 材料工程085601数二英一335求调剂 +6 双马尾痞老板2 2026-03-31 6/300 2026-04-04 22:29 by hemengdong
[考研] 考研调剂 +5 小sun要好运 2026-04-03 5/250 2026-04-03 21:43 by 啵啵啵0119
[考研] 求调剂 302分初试 0854 +5 伶可乐 2026-04-02 5/250 2026-04-02 17:53 by 笔落锦州
[考研] 农学考研求调剂 +3 dkdkxm 2026-04-01 3/150 2026-04-02 16:04 by wangjagri
[考研] 0710生物学求调剂 +9 manman511 2026-04-01 9/450 2026-04-02 10:00 by zxl830724
[考研] 085410 一志愿211 22408分数359求调剂 +3 123456789qw 2026-03-31 4/200 2026-04-02 00:06 by 义文wang
信息提示
请填处理意见