Advertisement



< Prev
Next >



Playing an audio in Applet





In this article, we will show you how to make an applet play an audio file of .wav format, using -
  • The play() method of Applet class.
  • The getAudioClip() method of Applet class in combination with AudioClip interface.

Note : These methods don't play an audio file of .mp3 format.




Playing an audio file in our applet using play() method of Applet class


Playing a music file in .wav format using play() method.
//Evening handling in an applet 

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

/*
<applet  code="Applet12" width=400 height=350>
</applet>
*/

public class Applet12 extends Applet implements ActionListener
{
Button b1;

public void init()
{
System.out.println("initializing an applet");
System.out.println("The full path to our applet file : " + getDocumentBase());	//getDocumentBase() gives URL object
System.out.println("Directory in which our applet file is located : " + getCodeBase());	//getCodeBase() gives URL object


b1 = new Button("Play");
add(b1);

b1.addActionListener(this);


}

public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Play"))
play(getDocumentBase(),"Acoustic Guitar.wav"); //playing a song(.wav format) from the same directory in which applet code file is located
}


public void paint(Graphics g)
{
g.drawString("Click on the play button to play the music", 80,80);
showStatus("Applet playing audio(.wav file)");
}
}


Output-


In order to run our applet using appletviewer, type the following command at command prompt-

appletviewer Applet12.java

Where Applet12.java is the name of java file that contains the code of an applet.




Advertisement




  • Playing an audio file in our applet using getAudioClip() method of Applet class


  • In the upcoming code, we are calling a method getAudioClip() of Applet class, which gives us an object of type AudioClip. Through this object, we could use the three method of AudioClip interface -
  • play(), to play the .wav audio file.
  • loop(), to play the .wav audio file in a loop.
  • stop(), to stop the .wav audio file being played.

  • Note : play() method of AudioClip class doesn't play audio file in .mp3 format.
    //Evening handling in an applet 
    
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    /*
    <applet  code="Applet13" width=400 height=350>
    </applet>
    */
    
    public class Applet13 extends Applet implements ActionListener
    {
    Button b1, b2, b3;
    AudioClip ac;
    
    public void init()
    {
    System.out.println("initializing an applet");
    System.out.println("The full path to our applet file : " + getDocumentBase());	//getDocumentBase() gives URL object
    System.out.println("Directory in which our applet file is located : " + getCodeBase());	//getCodeBase() gives URL object
    
    
    b1 = new Button("Play");
    add(b1);
    b2 = new Button("Loop");
    add(b2);
    b3 = new Button("Stop");
    add(b3);
    
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    
    
    }
    
    public void actionPerformed(ActionEvent ae)
    {
    if(ae.getActionCommand().equals("Play"))
    {
    ac = getAudioClip(getDocumentBase(),"Acoustic Guitar.wav"); //playing a song(.wav format) from the same directory in which applet code file is located
    ac.play();
    }
    
    
    if(ae.getActionCommand().equals("Loop"))
    {
    ac = getAudioClip(getDocumentBase(),"Acoustic Guitar.wav"); //playing a song(.wav format) from the same directory in which applet code file is located
    ac.loop();
    }
    
    if(ae.getActionCommand().equals("Stop"))
    {
    ac = getAudioClip(getDocumentBase(),"Acoustic Guitar.wav"); //playing a song(.wav format) from the same directory in which applet code file is located
    ac.stop();
    }
    }
    
    
    public void paint(Graphics g)
    {
    g.drawString("Click on the button to play,loop or stop the music", 80,80);
    showStatus("Applet playing audio(.wav file)");
    }
    }


    Output-


    In order to run our applet using appletviewer, type the following command at command prompt-

    appletviewer Applet13.java

    Where Applet13.java is the name of java file that contains the code of an applet.



    • After we click on the play button, the music file is played.
    • After we click on the loop button, the music file is played in the loop.
    • After we click on the stop button, the music file being played is stopped.




    Please share this article -



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement

    p