如何在Java背景中播放音乐

如何在Java背景中播放音乐,java,Java,我有个问题。我有一个需要在后台播放音乐的程序。有人能帮我吗?我已经在internet上搜索过,但没有解决方案适合我。导入: import java.io.File; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; 代码: 到目前为止您尝试了什么?欢迎使用堆栈溢出。请学习如何使用堆栈溢出,并阅读如何提高问题的

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

导入:

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


到目前为止您尝试了什么?欢迎使用堆栈溢出。请学习如何使用堆栈溢出,并阅读如何提高问题的质量。然后检查,看看你可以问什么问题。请参阅:。请显示您尝试过的尝试以及从尝试中获得的问题/错误消息。我尝试了此方法,然后发现与此相同的方法,但没有任何效果,然后我尝试了很多youtube教程,但没有任何效果添加您自己的非工作代码请我在某个时候用Java制作了一个游戏,其中有一段背景音乐,您可以在我的github repos中找到该项目:
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
}