Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 格式不正确,没有协议。如何修复?_Java_Audio_Malformedurlexception - Fatal编程技术网

Java 格式不正确,没有协议。如何修复?

Java 格式不正确,没有协议。如何修复?,java,audio,malformedurlexception,Java,Audio,Malformedurlexception,所以我有这个代码: import java.applet.Applet; import java.applet.AudioClip; import java.net.URL; public class SoundTest { public static void main(String[] args) throws Exception { URL url = new URL("Sprites/Omni's Tones/New tones/bubblespawn_01

所以我有这个代码:

 import java.applet.Applet;
 import java.applet.AudioClip;
 import java.net.URL;

 public class SoundTest {
     public static void main(String[] args) throws Exception {

    URL url = new URL("Sprites/Omni's Tones/New tones/bubblespawn_01.wav");
    AudioClip clip = Applet.newAudioClip(url);
    AudioClip clip2 = Applet.newAudioClip(url);
    clip.play();
    Thread.sleep(1000);
    clip2.loop();
    Thread.sleep(20000);
    clip2.stop();

    System.out.println("end");
     }
 }
出现一个错误:

 Exception in thread "main" java.net.MalformedURLException: no protocol: Sprites/Omni's Tones/New tones/bubblespawn_01.wav
at java.net.URL.<init>(URL.java:586)
at java.net.URL.<init>(URL.java:483)
at java.net.URL.<init>(URL.java:432)
at com.edu4java.minitennis7.SoundTest.main(SoundTest.java:10)
线程“main”java.net.MalformedURLException中的异常:无协议:Sprites/Omni's Tones/New Tones/bubblespawn_01.wav
在java.net.URL.(URL.java:586)
在java.net.URL.(URL.java:483)
位于java.net.URL。(URL.java:432)
位于com.edu4java.minitennis7.SoundTest.main(SoundTest.java:10)
我该如何解决这个问题?非常感谢。
似乎我需要在代码中添加更多行,

如果您在小程序中运行它,请尝试使用和


如果它在独立应用程序中运行,请根据音乐文件位置选择一个:

// Read from same package
InputStream inputStream = getClass().getResourceAsStream("abc.wav");

// Read from music folder parallel to src in your project
File file = new File("music/abc.wav");

// Read from src/music folder
URL url = getClass().getResource("/music/abc.wav");

// Read from src/music folder
InputStream inputStream = getClass().getResourceAsStream("/music/abc.wav");
// Read from same package
InputStream inputStream = getClass().getResourceAsStream("abc.wav");

// Read from music folder parallel to src in your project
File file = new File("music/abc.wav");

// Read from src/music folder
URL url = getClass().getResource("/music/abc.wav");

// Read from src/music folder
InputStream inputStream = getClass().getResourceAsStream("/music/abc.wav");