CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2001
    Posts
    27

    Playing Sound from Applet

    I have one Button on my Applet and I want that when I click on Button audio file MySound.wav will play. I am using AWT for creating applet.



  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Playing Sound from Applet

    I'm not sure what your question is. Do you need to know the classes and methods for playing a sound? You would use Applet.getAudioClip() to load the sound and AudioClip.play() to play it.

    Norm
    Norm

  3. #3
    Join Date
    Jul 2001
    Posts
    27

    Re: Playing Sound from Applet

    Thank you for your anwser i know that for playing sound i can use AudioClip class but i don't know how. i write this prog. but it didn't work.

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;


    /*
    <applet code="MySound" width=200 height=200>
    </applet>
    */

    public class MySound extends Applet {


    AudioClip audioClip;

    public void init()

    audioClip = Applet.getAudioClip(getCodeBase(),"gradually.wav");

    audioClip.play();
    }
    }





  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Playing Sound from Applet

    You didn't say what version of the JVM you were using. I think that .wav files work on Java v1.2. If you're running it in a browser without the plugin, it won't work. Most browsers are at v1.1. Try it in the AppletViewer for a JVM >= 1.2 and it should work.

    Norm
    Norm

  5. #5
    Join Date
    Jul 2001
    Posts
    27

    Re: Playing Sound from Applet

    Dear Norm

    Thank you for reply. As you ask I use this things.

    JDK 1.2.2
    Internet Explorer Version: 6.0.2462.0000
    OS Name: Microsoft Windows Whistler Beta Professional

    When I try to Compile my Code it give me this error:

    MySound.java:20: Can’t make static reference to method java.applet.AudioClip getAudioClip(java.net.URL, java.lang.String) in class java.applet.Applet.


    My Code is:


    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;


    /*
    <applet code="MySound" width=200 height=200>
    </applet>
    */

    public class MySound extends Applet implements ActionListener {


    Button bPlay;
    AudioClip audioClip;

    public void init() {

    bPlay = new Button("Play Sound");
    audioClip = Applet.getAudioClip(getCodeBase(),"gradually.wav");

    add(bPlay);
    bPlay.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
    if(ae.getSource() == bPlay) {
    audioClip.play();
    }
    }
    }






  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    static vs non-static method calls

    To correct the compiler error you get, you need to read the documentation for the getAudioClip() method. Is it a static method of the Applet class? Or is it non-static? To call a class's static method you code: classname.methodname();
    For example: int x = Math.min(1, 2); calls the Math class's min() method which is static.
    To call a class's non-static method you code: methodname();
    For example in an Applet you code: Image img = getImage(url); to get an image object.

    Norm
    Norm

  7. #7
    Join Date
    Jul 2001
    Posts
    27

    Re: static vs non-static method calls

    At last I done it. Hear how I done it


    /**
    * Java code for playing sound on an Applet.
    * @author Ajay Parmar
    * e-mail: pndatasystem@yahoo.com
    */

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.URL;


    /*
    <applet code="MySound" width=200 height=200>
    </applet>
    */


    public class MySound extends Applet implements ActionListener {


    Button bPlay;
    AudioClip audioClip;
    URL clipPath;

    public void init() {

    bPlay = new Button("Play Sound");

    try {
    clipPath = new URL(this.getCodeBase()+"MySound.wav");
    }
    catch(Exception e) { }

    audioClip = this.newAudioClip(clipPath);

    add(bPlay);
    bPlay.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
    if(ae.getSource() == bPlay) {
    audioClip.play();
    }
    }
    }







  8. #8
    Join Date
    Nov 2000
    Posts
    8

    Re: Playing Sound from Applet

    to solve the problem that you mentioned
    just replace the following line in your code:
    <font color=red>audioClip = Applet.getAudioClip(getCodeBase(),"gradually.wav");</font>
    with this 1
    <font color=red>audioClip = getAudioClip(getCodeBase(),"gradually.wav");</font>



  9. #9
    Join Date
    Jul 2001
    Posts
    27

    Re: Playing Sound from Applet

    Dear

    Thank you very much for your suggestion. I had done that before. Actually I replace Applet word with this so it work well. Any way thank you very much for your replay. My code is as under:


    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.URL;


    /*
    <applet code="MySound" width=200 height=200>
    </applet>
    */


    public class MySound extends Applet implements ActionListener {


    Button bPlay;
    AudioClip audioClip;
    URL clipPath;

    public void init() {

    bPlay = new Button("Play Sound");

    try {
    clipPath = new URL(this.getCodeBase()+"gradually.wav");
    }
    catch(Exception e) { }

    audioClip = this.newAudioClip(clipPath);

    add(bPlay);
    bPlay.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
    if(ae.getSource() == bPlay) {
    audioClip.play();
    }
    }
    }





    Ajay Parmar




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