CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2008
    Posts
    49

    Small problem trying to play a wav file?

    This code seems to compile properly without any errors, and when i run it, it displays a frame, without a problem, as you can see i may be over complicating this by using threads, but thats because this project will eventually turn into a multi-threaded pacman game (for university assignment)

    anyway, the code compiles, and a frame appears, there is not a null pointer exception, so im guessing my application can "see" the wav file in its directory, but theres no sound... any ideas?

    oh and by the way, the random generator doesn't do anything, its just left in there from an experiment...

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.applet.AudioClip;
    import java.net.URL;
    
    import java.util.Random;
    
    public class PacMan implements Runnable
    {
    
    
    	private final static Random generator = new Random();
    	private JFrame frame;
    
    	public static void main(String[]args)
    	{
    		Thread CreateGui = new Thread(new PacMan());
    		
    		CreateGui.start();	
    	}
    	
    	public PacMan()
    	{
    		frame = new JFrame("Test PacMan");
    		frame.setSize(500,500);
    		frame.setVisible(true);
    
    		playAudioResource("pacman_intro.wav");
    					
    	}
    	
    	public void playAudioResource(String AudioResourceName)
    	{
    		ClassLoader cl = PacMan.class.getClassLoader();
    		
    		URL resourceURL = cl.getResource(AudioResourceName);
    		
    		AudioClip sound = JApplet.newAudioClip(resourceURL);
    		
    		sound.play();
    		
    		
    	}
    	
    	public void run()
    	{
    
    	}
    }

  2. #2
    Join Date
    Apr 2009
    Posts
    3

    Re: Small problem trying to play a wav file?

    It depends on the type of your operating system.
    Try to use classes in package javax.sound.sampled, for example, AudioSystem, instead of java.applet.*.

  3. #3
    Join Date
    Sep 2008
    Posts
    49

    Re: Small problem trying to play a wav file?

    i am using windows vista, 64 bit... I have been told that i have to do it the way i am currently approaching the problem, as part of the assignment. thanks for your help though.

  4. #4
    Join Date
    Sep 2008
    Posts
    49

    Re: Small problem trying to play a wav file?

    can anyone help me? thanks in advance...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured