Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: playing sound using java

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation playing sound using java

    hi friends,

    i am a beginner in java.i am working on a program to play a sound file.
    i used the following code snippet.
    import java.applet.*;
    import java.awt.event.*;
    import java.awt.*;
    class SoundExample extends Applet 
    {
    AudioClip soundFile1;
    SoundExample()
    {
    soundFile1=getAudioClip(getDocumentBase(),"1.au");
    soundFile1.play();
    }
    }
    public  class sound_eg
    {
    public static void main(String args[])
    {
    SoundExample s=new SoundExample();
    System.out.println("Completed");
    }
    }
    the program is compiling successfully.i am getting the following run time errors.
    "Exception in thread "main" java.lang.NullPointerException
    at java.applet.Applet.getDocumentBase(Applet.java:141 )
    at SoundExample.(init)(sound_eg.java:10)
    at sound_eg.main(sound_eg.java:18)

    i even tried by using a function "init" instead of a constructor.

    kindly help me!!![/COLOR]
    Last edited by helloworld922; March 22nd, 2010 at 07:10 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: playing sound using java

    Applets come pre-supplied with a main method. You shouldn't be trying to create an applet separately. Applets also should be initialized in the init() method, not the applet constructor. Try running just your SoundExample class.

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    491
    Thanks
    8
    Thanked 47 Times in 45 Posts

    Default Re: playing sound using java

    Or, if all you want is to play a sound in a non-Applet application, use the static method Applet#newAudioClip(URL)

    db

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Re: playing sound using java

    Quote Originally Posted by helloworld922 View Post
    Applets come pre-supplied with a main method. You shouldn't be trying to create an applet separately. Applets also should be initialized in the init() method, not the applet constructor. Try running just your SoundExample class.
    Sir,
    i tried as u said,with the following snippet.

    import java.applet.*;
    import java.awt.event.*;
    import java.awt.*;
    class SoundExample extends Applet 
    {
    AudioClip soundFile1;
    public void init()
    {
    soundFile1=getAudioClip(getDocumentBase(),"1.au");
    soundFile1.play();
    }
    }

    but i get a runtime error "Exception in thread "main" java.lang.NoSuchMethodError:main
    Last edited by JavaPF; March 26th, 2010 at 04:21 AM. Reason: Please use code tags

  5. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    491
    Thanks
    8
    Thanked 47 Times in 45 Posts

    Default Re: playing sound using java

    First of all, make up your mind whether you want an applet or an application.

    Note that Applets/JApplets are NOT executed by invoking java classname.
    Lesson: Applets (The Java™ Tutorials > Deployment)

    db

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: playing sound using java

    You must run applets differently than Java applications. If you're using an IDE (such as Eclipse), you have the choice to run a program as an applet or as a Java application.

    Otherwise, create a simple HTML that will launch your applet (replace "A" with the name of your applet):

     <html>
     <p> This file launches the 'A' applet: A.class! </p>  
     <applet code="A.class" height=200 width=320>
     No Java?!
     </applet>
     </html>

  7. #7
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: playing sound using java

    Quote Originally Posted by Darryl.Burke View Post
    First of all, make up your mind whether you want an applet or an application.

    Note that Applets/JApplets are NOT executed by invoking java classname.
    Lesson: Applets (The Java™ Tutorials > Deployment)

    db
    sorry to trouble u further brother. Its an application. actually i'm doing my project in java .i want a sound to be played when my output is being displayed. i want a piece of code that could be added in my project to play a sound.its a normal java program using jdk. kindly help me or if u could, pls send me a sample code.

Similar Threads

  1. Playing .wav file
    By harsha in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 11:33 PM
  2. need help with Timer and sound
    By amahara in forum AWT / Java Swing
    Replies: 4
    Last Post: February 18th, 2010, 11:22 AM
  3. Java refuses to play sound on my machine
    By DDgeva in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 31st, 2010, 07:58 AM
  4. Replies: 2
    Last Post: January 8th, 2010, 07:22 AM
  5. Difference between Speech API and Sound API
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: October 22nd, 2009, 12:22 AM