有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Java背景中播放音乐

我有个问题。我有一个需要在后台播放音乐的程序。有人能帮我吗?我已经在网上搜索过了,但没有找到适合我的解决方案


共 (1) 个答案

  1. # 1 楼答案

    进口:

    import java.io.File; 
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    

    代码:

    try //playing the music
    {
        String soundName = "yourAudioFile.wav";    
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
        Clip clip = AudioSystem.getClip();
        clip.open(audioInputStream);
        clip.start(); //start to play the clip
    } 
    catch (Exception e) 
    {
        //Do stuff in case of an exception, for example, file not found
    }