简体   繁体   中英

How to play .mp3 instead of .wav?

How do I get and use mp3s instead of .wav? This works fine with wav files but gets an error with 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:-

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.

Also, SoundPool might be a good way to play clips in your apps - http://developer.android.com/reference/android/media/SoundPool.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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