• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

Still can't get a sound to play in an applet

This is my code, the html file only shows a grey box

package jas;

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


public class PlaySound extends Applet {
    boolean isStandalone = false;
    BorderLayout borderLayout1 = new BorderLayout();

    //Get a parameter value
    public String getParameter(String key, String def) {
        return isStandalone ? System.getProperty(key, def) :
                (getParameter(key) != null ? getParameter(key) : def);
    }
   
    public static void main(String[  ] args)
       throws java.net.MalformedURLException
   {
       java.applet.AudioClip clip =
        java.applet.Applet.newAudioClip(new java.net.URL("1000Hz.au"));
       clip.play( );
    }

    //Construct the applet
    public PlaySound() {
    }

    //Initialize the applet
    public void init() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //Component initialization
    private void jbInit() throws Exception {
    }

    //Start the applet
    public void start() {
    }

    //Stop the applet
    public void stop() {
    }

    //Destroy the applet
    public void destroy() {
    }

    //Get Applet information
    public String getAppletInfo() {
        return "Applet Information";
    }

    //Get parameter info
    public String[][] getParameterInfo() {
        return null;
    }
}
0
pigmentarts
Asked:
pigmentarts
  • 6
  • 6
1 Solution
 
objectsCommented:
You load your sound in main() which does not get run when you run it as an applet, that only gets run if you run it as an application

java jas.PlaySound


Where is the .au file stored?
Put it is=n the same directory as the html file and use:

   java.applet.AudioClip clip =
        java.applet.Applet.newAudioClip(new java.net.URL(getDocumentBase(), "1000Hz.au"));
0
 
pigmentartsAuthor Commented:
I think i understand what you mean by 'java jas.PlaySound'  but i don't know how to do it. The only method of running an application i know is pressing the green play button :)
0
 
objectsCommented:
What ide are you using?

To run it as application run the command I posted from the command line
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
objectsCommented:
Or if you want to run it as an applet, move/copy the code you have in main into your start() method.
0
 
pigmentartsAuthor Commented:
I get an error saying that the  getDocumentBase() cannot be referenced from a static line

0
 
pigmentartsAuthor Commented:
I'm using j-builder 2005 foundation the (80meg edition) I can get access to a full version of J-builder X at times tho
0
 
objectsCommented:
> I get an error saying that the  getDocumentBase() cannot be referenced from a static line

getDocumentBase() is an applet method, if you are running it as an applet then you need to move that code into start().

Did you want an applet or an application?
0
 
objectsCommented:
0
 
objectsCommented:
If the au file is in the same directory as the class file then your start() method could loo like:

public void start()
{
       java.applet.AudioClip clip =
          java.applet.Applet.newAudioClip(getClass().getResource("1000Hz.au"));
       clip.play( );
}
0
 
pigmentartsAuthor Commented:
ah right..yes an applet,
Unless i've got it in the wrong place entirely i get an error message saying the  'java.net.MalformedURLException must be caught or declared'

 public static void main(String[  ] args)
       throws java.net.MalformedURLException
   {
       
    }

    //Construct the applet
    public PlaySound() {
    }

    //Initialize the applet
    public void init() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //Component initialization
    private void jbInit() throws Exception {
    }

    //Start the applet
    public void start() {
       
        java.applet.AudioClip clip =
          java.applet.Applet.newAudioClip(new java.net.URL(getDocumentBase(), "1000Hz.au"));

      clip.play( );

    }

    //Stop the applet
0
 
pigmentartsAuthor Commented:
I surrounded it with a try catch clause and it works now thats great!
!
0
 
pigmentartsAuthor Commented:
it works in the application and the browser, i'm going to try and loop it now, then i'll come pester you guru's some more
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now