public class AudioPlayer implements javax.microedition.media.PlayerListener{
private Player player;
public playMedia(String[] audiofile){
int adflen = audiofile.length;
InputStream is;
try{
byte[][] bfiles = new byte[adflen][];
byte[] temp = new byte[800];// all files in audiofile are approx of 800bytes
int totalbytes = 0; int numread = 0; int strmlen = 0;
for(int k=0; k<adflen; k++)
{
numread =0; totalbytes = 0;
is = this.getClass().getResourceAsStream(audiofile[k]);
while(numread >= 0)
{
totalbytes+=numread;
numread = is.read(temp);
}
bfiles[k] = new byte[totalbytes];
strmlen+=totalbytes;
is.close();
for(int cnt=0; cnt<totalbytes; cnt++)
bfiles[k][cnt] = temp[cnt];
}
byte[] stream = new byte[strmlen];
int pos =0;
for(int out=0; out<adflen; out++)
for(int in=0; in<bfiles[out].length; in++)
stream[pos++] = bfiles[out][in];
ByteArrayInputStream bais = new ByteArrayInputStream( stream );
player = Manager.createPlayer(bais,"audio/x-amr");
player.addPlayerListener(this);
player.realize();
player.prefetch();
player.start();
}
catch (Exception e) {
System.out.println(e.toString());
}
}
public void playerUpdate(Player p,String event,Object eventData) {
if (event == PlayerListener.END_OF_MEDIA)
{
try {
p.deallocate();
p.close();
}
catch(Exception e){
p.deallocate();
p.close();
return;
}
}
}