Playing .mp3 and .wav in Java?

前端 未结 14 2139
一整个雨季
一整个雨季 2020-11-22 06:52

How can I play an .mp3 and a .wav file in my Java application? I am using Swing. I tried looking on the internet, for something like this example:<

14条回答
  •  -上瘾入骨i
    2020-11-22 07:32

    Java FX has Media and MediaPlayer classes which will play mp3 files.

    Example code:

    String bip = "bip.mp3";
    Media hit = new Media(new File(bip).toURI().toString());
    MediaPlayer mediaPlayer = new MediaPlayer(hit);
    mediaPlayer.play();
    

    You will need the following import statements:

    import java.io.File;
    import javafx.scene.media.Media;
    import javafx.scene.media.MediaPlayer;
    

提交回复
热议问题