怎么玩.mp3而不是.wav?

时间:2013-08-30 21:24:54

标签: java audio

如何获取和使用mp3而不是.wav?这适用于wav文件,但在mp3中出错。

  private String audioFile = "/audio/menu_screen.wav";
 public void getSound() {
    BufferedInputStream inaudio = new BufferedInputStream(getClass().getResourceAsStream(audioFile)); 


    clip = null;
    try {

        clip = AudioSystem.getClip();
        clip.open(AudioSystem.getAudioInputStream(inaudio));

    } catch (LineUnavailableException | UnsupportedAudioFileException | IOException exc) {
        exc.printStackTrace(System.out);
    }

}

public void playSound(int x) {
  if(clip != null){
  clip.stop();
  }

错误是..

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1119)
at frogger.Board.getSound(Board.java:181)
at frogger.Board.muted(Board.java:235)

2 个答案:

答案 0 :(得分:0)

如何尝试使用MediaMedia Player类:

String s= "abc.mp3";
Media m= new Media(s);
MediaPlayer mediaPlayer = new MediaPlayer(m);
mediaPlayer.play();

答案 1 :(得分:0)

尝试使用.ogg而不是wav,就像Android文档推荐的那样。

此外,SoundPool可能是在您的应用中播放剪辑的好方法 - http://developer.android.com/reference/android/media/SoundPool.html