Playing .mp3 and .wav in Java?

前端 未结 14 2398
一整个雨季
一整个雨季 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条回答
  •  醉话见心
    2020-11-22 07:40

    Using MP3 Decoder/player/converter Maven Dependency.

    import javazoom.jl.decoder.JavaLayerException;
    import javazoom.jl.player.Player;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    
    public class PlayAudio{
    
    public static void main(String[] args) throws FileNotFoundException {
    
        try {
            FileInputStream fileInputStream = new FileInputStream("mp.mp3");
            Player player = new Player((fileInputStream));
            player.play();
            System.out.println("Song is playing");
            while(true){
                System.out.println(player.getPosition());
            }
        }catch (Exception e){
            System.out.println(e);
        }
    
      }
    
    }
    

提交回复
热议问题