Hi. I got an audiorelated problem in Java:
I know how to play an audiofile with AudioClip,
but there is no option to stop the Audioclip while playing and resume it after a few seconds.
I also need to know how much of it is left to play, has been played etc.
Is there a java-class that offers these methods and not only a play() and stop() ?
There is more advanced sound class to produce sound in java, google for it
about AudioClip:
you can play/stop it that’s all (http://java.sun.com/j2se/1.4.2/docs/api/java/applet/AudioClip.html)
to know how much has been played you can do something like that
start=System.currentTimeMillis();
a.play()
//than later use playingtime=System.ucrrentTimeMillis()-start;
you can mix multiple audio file by doing:
a.play()
a2.play
you can simulate volume using a little trick:
create 5 AudioClip using same source file
than to play the sound with volume=3 do
a1.paly()
a2.play()
a3.play()
and so … for volume =1
a1.play()
finally if you like challenge you can try to produce/get a better control using sinewave as source file for multiple AudioClip and try to produce the sound using FFT but finally you better use something like that http://java.sun.com/j2se/1.4.2/docs/api/javax/sound/sampled/AudioSystem.html
below a sample source code showing how to control volume with AudioClip
it use 5 different sourcefiles
SOUND0.WAV
SOUND1.WAV
SOUND2.WAV
SOUND3.WAV
SOUND4.WAV
to be compatible with JVM 1.1+ but using newAudioClip() you can use only one source file
SOUND.WAV
/**
*
* Adding Volume on Applet AudioClip
*
* @author Bruno Augi
* @version 1.00 08/03/18
*/
import java.awt.*;
import java.applet.*;
import java.net.*;
public class Test extends Applet implements Runnable
{
AudioClip a[]=new AudioClip[5];
public void init()
{
try
{
System.out.println("Loading sound " +getCodeBase()+"/SOUND.WAV");
for(int x=0;x<a.length;x++)
a[x]=this.getAudioClip(new URL(getCodeBase()+"/SOUND"+x+".WAV"));
}
catch(MalformedURLException mue)
{
mue.printStackTrace(System.out);
}
//Prepare sounds to be played (avoid unsynchronisation of AudioClip)
try
{
for(int x=0;x<a.length;x++)
if(this.a[x]!=null)
{
a[x].play();
a[x].stop();
}
Thread.sleep(100);
}
catch(InterruptedException ie)
{
}
}
public void start()
{
Thread t=new Thread(this);
t.start();
}
public void run()
{
Graphics g=this.getGraphics();
for(int x=0;x<this.a.length;x++)
{
g.setColor(Color.white);
g.fillRect(0,0,this.getSize().width,this.getSize().height);
g.setColor(Color.black);
g.drawString("Playing sound volume = " + x,30,30);
this.playVolume(x);
try
{
Thread.sleep(2000);
}
catch(InterruptedException ie)
{
}
}
}
public void playVolume(int volume)
{
for(int x=0;x<volume;x++)
{
if(x<this.a.length && this.a[x]!=null)
a[x].play();
}
}
public void paint(Graphics g)
{
}
}
EDIT:
using 4 differents sources files with differents volume could enable you to change sound volume from 0 to 15
your sound just have to match the below rule:
sound0.wav (volume==1)
sound1.wav (volume==2)
sound2.wav (volume==4)
sound4.wav (volume==8)
then using binary representation of your volume you can play the sound at any volume between 0 and 16 :
sound0 and sound4 for volume=9
sound0 and sound1 and sound2 a for volume=7
etc…