How to Work with AudioClip in Java?

0
19
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

audioclip in java
audioclip in java

How to Work with AudioClip in Java

Remember the issues with the plain audio class? If not then I recommend you to check this link quickly to find more on Audio.

To resolve the issues of audio, java came up with AudioClip class which solves the limitation of play() method.

The methods of AudioClip are defined below:


public AudioClip getAudioClip(URL url);//returns an audio clip object from the url. This method will not return
//until the audio clip has been loaded from the specified URL.So one should place it in a separate thread if
// the file is expected to take a while to download.
public AudioClip getAudioClip(URL url,String name);//Does the same but finds the audio file using base url and
// filename
 

AudioClip object provides the below methods:


public abstract void loop();//plays the clip in a continuous loop.
public abstract void play();//plays the clip from begining.
public abstract void stop();//stops the clip that is currently playing.
 

Here is an example how to work with AudioClips:


AudioClip tabla;
AudioClip flute;
Button startButton=new Button("Start");
Button conButton=new Button("Continue");
Button stopButton=new Button("Stop");
tabla=getAudioClip(getDocumentBase(),"Tabla.au");
flute=getAudioClip(getDocumentBase(),"flute.au");
public void start(){
tabla.loop();
}
public void stop(){
tabla.stop();
}
public boolean action(Event e,Object button)
{
if("Start".equals(button))
{
flute.play();
}
if("Continue".equals(button))
{
flute.loop();
}
if("Stop".equals(button))
{
flute.stop();
}
return true;
}
}
 
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here