Jump to content

Audio files in Java help?

Arcapse

So I'm trying to play a .wav file in Eclipse, Java, I've followed a guide and afaik, I've done everything the same way. However, it does not work and I receive the following error:

byCC2Nk.jpg

 

I'm thinking it's because Eclipse is registering the audio files as text files as both the .wav and .mp3 files have the same icons as the .txt files as shown below:

 

NS8FfDK.jpg

 

Am I correct? How would I go about fixing this if so?

Cheers & Best Regards,

Arcapse

Link to comment
Share on other sites

Link to post
Share on other sites

Images are not working on my end, could you post the code please?

 

Current Build:

CPU: AMD Ryzen 5 2600 // Mobo: Ryzen AM4 B350 GAMING PLUS ATX // RAM: 16GB Corsair Vengeance LPX DDR4 3000MHz // GPU: Gigabyte AMD Radeon RX 580 Gaming 8GB // SSD: Kingston A400 120GB // HDD: 3 x WD Blue 1TB // PSUCorsair 650M // Case: Corsair 450D // Monitor: LG Ultrawide 29" IPS

 

Plex Server:

CPU: AMD FX 8350 Black Edition // Mobo: Gigabyte - GA-78LMT-USB3 R2 Micro ATX // RAM: 16GB Corsair Vengeance DDR3 1600 MHz // GPU: GeForce GTX 670 // HDD: Seagate BarraCuda 2TB // PSU: Kolink Core Series 500W 80 Plus Certified // Case: AVP Viper Mini Tower

 

Other:

PS4 Pro // PS3 // Nintendo Switch (Pokemon edition) // Nintendo 3DS // Xbox 360 // iPhone 8 Plus // Macbook Retina 2013

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Rhyss said:

Images are not working on my end, could you post the code please?

 

import java.applet.Applet;
import java.applet.AudioClip;

public class Sound {

    public static final Sound sound1 = new Sound("/Short Beep Sound Effect.wav");

    private AudioClip clip;
    
    public Sound(String filename){
    try{
        clip = Applet.newAudioClip(Sound.class.getResource(filename));
    }catch(Exception e){
        e.printStackTrace();
    }
    }
    
    public void play(){
        try{
            new Thread(){
                public void run(){
                    clip.play();
                }
            }.start();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
}
 

Cheers & Best Regards,

Arcapse

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Arcapse said:

// CODE

Try

import java.io.*;
import sun.audio.*;

public class Sound
{
  public static void main(String[] args) 
  throws Exception
  {
    String beep = "LINK TO FILE";
    InputStream in = new FileInputStream(beep);

    AudioStream audioStream = new AudioStream(in);

    AudioPlayer.player.start(audioStream);
  }
}

Not sure on this one though. Did some research myself and this should work.

 

Although looking at your old code:

public static final Sound sound1 = new Sound("/Short Beep Sound Effect.wav");

That isn't a file link Java would understand

Current Build:

CPU: AMD Ryzen 5 2600 // Mobo: Ryzen AM4 B350 GAMING PLUS ATX // RAM: 16GB Corsair Vengeance LPX DDR4 3000MHz // GPU: Gigabyte AMD Radeon RX 580 Gaming 8GB // SSD: Kingston A400 120GB // HDD: 3 x WD Blue 1TB // PSUCorsair 650M // Case: Corsair 450D // Monitor: LG Ultrawide 29" IPS

 

Plex Server:

CPU: AMD FX 8350 Black Edition // Mobo: Gigabyte - GA-78LMT-USB3 R2 Micro ATX // RAM: 16GB Corsair Vengeance DDR3 1600 MHz // GPU: GeForce GTX 670 // HDD: Seagate BarraCuda 2TB // PSU: Kolink Core Series 500W 80 Plus Certified // Case: AVP Viper Mini Tower

 

Other:

PS4 Pro // PS3 // Nintendo Switch (Pokemon edition) // Nintendo 3DS // Xbox 360 // iPhone 8 Plus // Macbook Retina 2013

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Rhyss said:

public static final Sound sound1 = new Sound("/Short Beep Sound Effect.wav");

That isn't a file link Java would understand

How so?

Cheers & Best Regards,

Arcapse

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Rhyss said:

Try


import java.io.*;
import sun.audio.*;

public class Sound
{
  public static void main(String[] args) 
  throws Exception
  {
    String beep = "LINK TO FILE";
    InputStream in = new FileInputStream(beep);

    AudioStream audioStream = new AudioStream(in);

    AudioPlayer.player.start(audioStream);
  }
}

 

Okay so this works..

Now how would I get this to be called correctly?

 

Originally, it got called like this:

 

if(TempWord.getY()>=400){
                w.remove(TempWord);
                Sound.sound1.play();
                Game.HEALTH -= 50;
                addWord(new Word(250, 50, game));
            }
 

Cheers & Best Regards,

Arcapse

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×