MyException - 我的异常网
当前位置:我的异常网» J2SE » javax.sound.sampled.UnsupportedAudioFileExceptio

javax.sound.sampled.UnsupportedAudioFileException: could not get audio (2)

www.MyException.Cn  网友分享于:2013-12-12  浏览:1943次

  list.add(filename);
  }
  }
  }
}

// 播放
private void play() {
  try {
  isStop = true;// 停止播放线程
  // 等待播放线程停止
  System.out.print("开始播放:" + filename);
  while (!hasStop) {
  System.out.print(".");
  try {
  Thread.sleep(10);
  } catch (Exception e) {
  }
  }
  File file = new File("E:\\game.mp3");
  labelfilename.setText("播放文件:" + filename);

  // 取得文件输入流
  audioInputStream = AudioSystem.getAudioInputStream(file);
  audioFormat = audioInputStream.getFormat();
  // 转换MP3文件编码
  if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
  audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
  audioFormat.getSampleRate(), 16, audioFormat
  .getChannels(), audioFormat.getChannels() * 2,
  audioFormat.getSampleRate(), false);
  audioInputStream = AudioSystem.getAudioInputStream(audioFormat,
  audioInputStream);
  }

  // 打开输出设备
  DataLine.Info dataLineInfo = new DataLine.Info(
  SourceDataLine.class, audioFormat,
  AudioSystem.NOT_SPECIFIED);
  sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
  sourceDataLine.open(audioFormat);
  sourceDataLine.start();

  // 创建独立线程进行播放
  isStop = false;
  Thread playThread = new Thread(new PlayThread());
  playThread.start();
  } catch (Exception e) {
  e.printStackTrace();
  }
}

class PlayThread extends Thread {
  byte tempBuffer[] = new byte[320];

  public void run() {
  try {
  int cnt;
  hasStop = false; // 读取数据到缓存数据
  while ((cnt = audioInputStream.read(tempBuffer, 0,
  tempBuffer.length)) != -1) {
  if (isStop)
  break;
  if (cnt > 0) {// 写入缓存数据
  sourceDataLine.write(tempBuffer, 0, cnt);
  }
  } // Block等待临时数据被输出为空
  sourceDataLine.drain();
  sourceDataLine.close();
  hasStop = true;
  } catch (Exception e) {
  e.printStackTrace();
  System.exit(0);
  }
  }

}

public static void main(String args[]) {
  new MusicPlayer();
}
}
------------------------------------

可是我运行却出错
------------------------------------
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at MusicPlayer.play(MusicPlayer.java:129)
at MusicPlayer.access$1(MusicPlayer.java:113)
at MusicPlayer$2.mouseClicked(MusicPlayer.java:68)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.List.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
----------------------------------------

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有