| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 689 人关注过本帖
标题:java播放音频文件时出现错误
只看楼主 加入收藏
徐小军你好
Rank: 1
等 级:新手上路
帖 子:6
专家分:2
注 册:2013-4-21
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
java播放音频文件时出现错误
//播放声音
package com.dishizhang;
import *;
import javax.sound.sampled.*;
public class Demo10_8
{
public static void main(String[] args) {
AePlayWave apw=new AePlayWave("d:\\2.wav");  
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 el)
{   el.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,1024);
    if(nBytesRead>=0)   
 auline.write(abData,0,nBytesRead);  
  }  
 }catch(Exception 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(AudioSystem.java:1170)
at com.dishizhang.AePlayWave.run(Demo10_8.java:21)
搜索更多相关主题的帖子: null import public package private 
2013-06-05 18:51
Kingbox_tang
Rank: 7Rank: 7Rank: 7
来 自:天津师范大学
等 级:黑侠
威 望:3
帖 子:146
专家分:677
注 册:2012-11-27
收藏
得分:20 
同学,你代码也太乱了吧,我帮你整理了一下,下次这样就不帮你了。
同学呀,你把那个音乐资源放到程序包中,这样写就可以了,(拟定包名为music ):
public class Enter{
    public static void main(String[] args) {
            AePlayWave apw=new AePlayWave("music\\2.wav");  
            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 el) {   
              el.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,1024);
            if(nBytesRead>=0)   
                auline.write(abData,0,nBytesRead);  
          }  
        }catch(Exception e){
           e.printStackTrace();  
          return;  
        }finally
        {    auline.drain();
            auline.close();   
        }  
        }
}

旨在提高编程水平,学有所用,学有所成,学有所为。
2013-06-06 20:57
徐小军你好
Rank: 1
等 级:新手上路
帖 子:6
专家分:2
注 册:2013-4-21
收藏
得分:0 
回复 2楼 Kingbox_tang
不会,就说不会,完全把上面的代码抄了一下,以为我看不出来呀,鄙视你
2013-06-16 10:21
快速回复:java播放音频文件时出现错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044616 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved