-1

I have a big problem over here i tried a million ways of doing this but nothing helps, here are the links i tried to make this...

https://www.youtube.com/watch?v=nUKya2DvYSo
How can I play sound in Java?
http://forum.codecall.net/topic/58228-playing-simple-sampled-audio-in-java/ http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml

there are just some links that are still opened in the browser but never mind.. i'm making this in NetBeans and i really cannot figure out how should i do this please help me. Here is the class i want to make this and also the button.. if you can make it for me i will be very happy

public class Machines extends javax.swing.JFrame {

public Machines() {
    initComponents();
}

private void spinBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        

}

also the music file is spinsound.wav and i have placed in the directory where is my project here is the link C:\Users\MONSTER\Desktop\STEFAN\NetBeansProjects\PasswordSaver\spinsound.wav

Community
  • 1
  • 1
Stefan Rafa
  • 1
  • 1
  • 1
  • 6
  • Can you post your actual code? Or what troubles have you run it to with your tried solutions? That will probably get you further than asking people to make your stuff for you! ;) – Bono Dec 17 '14 at 19:53
  • i have deleted all my code since i posted this question but here is what i have tried the last time `void playSound() throws UnsupportedAudioFileException, IOException, LineUnavailableException { URL url = this.getClass().getClassLoader().getResource("spinsound.wav"); AudioInputStream audioIn; audioIn = AudioSystem.getAudioInputStream(url); Clip clip = AudioSystem.getClip(); clip.open(audioIn); clip.start(); }` – Stefan Rafa Dec 17 '14 at 19:57
  • Please, don't post code in comments, it's unreadable... Instead, use the "edit" link under your question to add it – AJPerez Dec 17 '14 at 20:08

3 Answers3

1

Could you try this:

ClassLoader CLDR = this.getClass().getClassLoader();
InputStream soundName = CLDR.getResourceAsStream("yourDirectory/yourSound.wav");
AudioStream audioStream = new AudioStream(soundName);
AudioPlayer.player.start(audioStream);

Taken from here after checking Google.

Bono
  • 4,757
  • 6
  • 48
  • 77
  • `private void playSound() { try { ClassLoader CLDR = this.getClass().getClassLoader(); InputStream soundName = CLDR.getResourceAsStream("spinsound.wav"); AudioStream audioStream = new AudioStream(soundName); AudioPlayer.player.start(audioStream); } catch (IOException e) { e.printStackTrace(); } }` doesn't work.. btw in the `InputStream soundName = CLDR.getResourceAsStream("spinsound.wav");` i need to put the full path to the sound file ? – Stefan Rafa Dec 17 '14 at 20:08
  • the project is quite big so i founded this error in the console `Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException` – Stefan Rafa Dec 17 '14 at 20:18
  • @StefanRafa Maybe you should check out Fev's answer. Seems to be working for me. – Bono Dec 17 '14 at 20:21
0

Since you just want to play the sound , you can use example I provide below: Look at the file:C:/image/song.wav , there is a file: keyword before your the directory declaration ..

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WavPlayer extends JFrame { 
    JButton btn = new JButton("Play Sound");
    File wavFile;
    URL defaultSound;
    public static Clip clip;
    public static AudioInputStream audioInputStream;

    public WavPlayer(String url) {
        try {
            setSize(300, 100);
            setLocation(400, 300);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            JPanel jp = new JPanel();
            defaultSound = new URL (url);

            jp.add(btn);

            getContentPane().add(jp);
            pack();

            btn.addActionListener(new ActionListener() {             
                @Override
                public void actionPerformed(ActionEvent e) {
                    play();
                }
            });
        } catch (MalformedURLException ex) {
            Logger.getLogger(WavPlayer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void play() {
        try {
            audioInputStream = AudioSystem.getAudioInputStream(defaultSound);

            try {
                clip = AudioSystem.getClip();
                clip.open(audioInputStream);
                clip.loop(20000);
                clip.start();

            } catch (LineUnavailableException e) {
            }

        } catch (UnsupportedAudioFileException | IOException e) {
        }
    }

    public void stop() {
        clip.stop();
    }

    public static void main(String args[]) {
        WavPlayer t = new WavPlayer("file:C:/image/song.wav");
        t.setVisible(true);

    }
}
Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19
  • Tested also on netbeans. – Fevly Pallar Dec 17 '14 at 20:11
  • i just tried this but doesn't work, after button click the project just freezes – Stefan Rafa Dec 17 '14 at 20:22
  • if you want i can copy/paste all class so you all can see whats the problem ? – Stefan Rafa Dec 17 '14 at 20:22
  • nope this code is working, try it carefully..(don't forget to change it according to your directory of the sound file), i try it like 100 times, and just working fine.. – Fevly Pallar Dec 17 '14 at 20:23
  • i just really can't do it do it for me or i will jump of my window PLEASE ! this is order save my life!!!! :D – Stefan Rafa Dec 17 '14 at 20:32
  • XD, now create a folder "image" on disk "C", inside the folder put a *.wav file , and change the name to "song.wav", and run this code under netbeans (as u're using right now). It's kinda funny i explain this but trust me this code is working just fine.. – Fevly Pallar Dec 17 '14 at 20:37
  • I really don't know whats the problem, maybe because of me or because of the PC but i just know one thing that i swear in my mother that i cant even hear any freaking sound after i press that "Play Sound" button xD P.S its not because of my speakers i'm listening frozen one and a half hour !!! :S – Stefan Rafa Dec 17 '14 at 21:02
0

Looking for entirely copied codes will help you less than studying and knowing the coding and it's process.

  • Then, something might be wrong with your system or jdk. Try uninstalling and reinstalling the jdk and run the code again. I assure u it works very well. – GregJava Dec 21 '14 at 07:08