Hi everybody,
I have a terrible problem with playing a sound in wav format, namely, this works, but the sound is repeated many times and make annoying "echo" effect. And that is not only in emulator but in the physical phone too.
I think but that the problem is not in code itself but somewhat general. Can anybody get me some ideas how can I better optimise the sound in the midlet. Maybe the problems is that the graphics is too heavy or I have used not correct packages or so on ... ?
Can anybody help me? Here the code, that I have used:
// Configuration .. The code is writen in Eclipse environment, CLDC 1.1, MIDP 2.0
// Call a function from a GameCanvas class
if (soundOn) sound.play("Click.wav");
/// I have created a special class for playing a sound
import javax.microedition.media.*;
import java.io.*;
public class SoundEffects implements PlayerListener {
private static Player player = null;
PlayerListener pll;
long mt=0;
public SoundEffects()
{
}
public void playerUpdate(Player player, String event, Object data)
{
if (event == PlayerListener.END_OF_MEDIA)
{
try
{
defplayer();
}
catch (MediaException me)
{
player = null;
}
}
}
void defplayer() throws MediaException
{
if (player != null)
{
if (player.getState() == Player.STARTED)
{
player.stop();
}
if (player.getState() == Player.PREFETCHED)
{
player.deallocate();
}
if(player.getState() == Player.REALIZED || player.getState() == Player.UNREALIZED )
{
player.close();
}
}
player = null;
}
final synchronized void play (String filename)
{
try
{
defplayer();
InputStream is = getClass().getResourceAsStream(filename);
player = Manager.createPlayer(is, "audio/x-wav");
player.addPlayerListener(this);
player.realize();
player.prefetch();
player.start();
mt = player.getMediaTime();
player.setMediaTime(mt+1);
}
catch (Throwable t)
{
player = null;
}
}
}

Reply With Quote




