简体   繁体   English

如何播放.mp3而不是.wav?

[英]How to play .mp3 instead of .wav?

How do I get and use mp3s instead of .wav? 如何获取和使用mp3而不是.wav? This works fine with wav files but gets an error with mp3. 这对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();
  }

The error is.. 错误是..

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)

How about trying using the Media and Media Player Classes this:- 如何尝试使用MediaMedia Player类:

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

Try using .ogg instead of wav as that's what the Android docs recommend. 尝试使用.ogg而不是wav,因为这是Android文档建议的方法。

Also, SoundPool might be a good way to play clips in your apps - http://developer.android.com/reference/android/media/SoundPool.html 另外,SoundPool可能是在您的应用中播放剪辑的好方法-http: //developer.android.com/reference/android/media/SoundPool.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM