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

    Why the applet can not play sound?

    //Here is the code,Could anybody help me find out why it does not work? Thank you!

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Example10_3 extends Applet implements ActionListener
    { AudioClip clip;
    Button buttonPlay,
    buttonLoop,
    buttonStop;
    public void init()
    { clip=getAudioClip(getCodeBase(),"ding.Wav");
    //have ding.wav file in the same directory with the applet
    buttonPlay=new Button("Play");
    buttonLoop=new Button("Replay");
    buttonStop=new Button("Stop");
    buttonPlay.addActionListener(this);
    buttonStop.addActionListener(this);
    buttonLoop.addActionListener(this);
    add(buttonPlay);
    add(buttonLoop);
    add(buttonStop);
    }
    public void stop()
    { clip.stop();
    }
    public void actionPerformed(ActionEvent e)
    { if(e.getSource()==buttonPlay)
    { clip.play();
    }
    else if(e.getSource()==buttonLoop)
    { clip.loop();
    }
    if(e.getSource()==buttonStop)
    { clip.stop();
    }
    }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Why the applet can not play sound?

    The only reason I can see for it not working is it can't find ding.Wav. I have run it on my system and it works fine.

    Make sure the file ding.Wav is in the same directory as the class file and if you are running on a case sensitive OS that the case is correct. You should also try running the wav file directly from the OS to make sure it is outputting an audible sound.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Sep 2011
    Posts
    8

    Re: Why the applet can not play sound?

    Thank you,but this still can not run in my computer, do you think it is OS dependent?

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Why the applet can not play sound?

    What OS are you running on?

    Try adding the following lines to your code just before you create the audio clip
    and then run your applet. You should see the print out in the applet console and it will tell you if the sound file is visible.
    Code:
     File f = new File(getCodeBase().getFile(), "ding.Wav");
     System.out.println(f+", Exists = "+f.exists());
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

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