0

I cannot play the audio file. I am making a java game and face this problem. I am coding on Eclipse. I have used .wav 16 bits audio file. I have tried to reinstall eclipse program and it still doesn't work.

Error -

javax.sound.sampled.UnsupportedAudioFileException: URL of unsupported format

How I encoded wav files -

enter image description here

enter image description here

import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Sound {
    
    Clip clip;
    URL soundURL[] = new URL[30];
    
    public Sound() {
        
        soundURL[0] = getClass().getResource("/Music.wav");
    }
    
    public void setFile(int i) {
        
        try {
            AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL[i]);
            clip = AudioSystem.getClip();
            clip.open(ais);
            
        }catch(Exception e) {
            e.printStackTrace();
        }
        
    }
    
    public void play() {
        clip.start();
    }
    
    public void loop() {
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }
    
    public void stop() {
        clip.stop();
    }
    

}
Prajwal Kulkarni
  • 1,480
  • 13
  • 22
Fluke
  • 15
  • 4
  • 1
    This is a plain Java problem, not really anything to do with Eclipse which is just your development environment. wav files can contain audio in several different formats, the Java audio system does not support all of the formats. – greg-449 Feb 03 '22 at 10:38
  • @greg-449 My audio file is 51 seconds long, is that part of the problem? – Fluke Feb 03 '22 at 10:42
  • 1
    See [Java's sound API doesn't work with all .WAV files?](https://stackoverflow.com/q/29713371/2670892) – greg-449 Feb 03 '22 at 10:49
  • @greg-449 I have changed the audio file to the one that the link mentioned above. but still can't work. – Fluke Feb 03 '22 at 11:27
  • @greg-449 WAV audio format is 16-bit PCM at 44100Hz. – Fluke Feb 03 '22 at 11:27
  • 1
    *"My audio file is 51 seconds long, is that part of the problem?"* Not the *immediate* problem, but note that (from memory) a `Clip` can only load 1 megabyte of data. 16/2 x 44,100 x 51 = 4,498,200 bytes (almost **4.5** megabytes). What does the file system show as the size of the music clip? – Andrew Thompson Feb 04 '22 at 05:14

0 Answers0