07-03-2009 02:05 AM
I have a game and need to play the same 3 sounds over and over again when users perform actions. I have code that works great, but after they play a certain amount of times (~100), my users phones start freezing up and generally have to have a battery pull done. My guess is that this is an out of memory situation and that I am not efficiently using the audio functions and instead loading the file into memory each time.
Can you guys help me out by checking out this code and seeing if I am doing it wrong? Thanks!
public void play(String noise)
{
if (this.m_player != null){
try {
this.m_player.stop();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
Class cl = Class.forName("com.mynamespace.blahblahblah");
InputStream input = cl.getResourceAsStream("/" + noise + ".mp3");
this.m_player = Manager.createPlayer(input,"audio/mpeg");
this.m_player.realize();
m_volumeControl = getVolumeControl(m_player);
if (m_volumeControl != null)
m_volumeControl.setLevel(60);
this.m_player.prefetch();
this.m_player.start();
this.m_player.deallocate();
}
catch (Exception e){
System.out.println("ERR: " + e.toString());
}
}
private VolumeControl getVolumeControl( Player p ) {
return (VolumeControl)p.getControl( "VolumeControl" );
}
07-03-2009 08:35 AM
I had a similar issue and found that the player does not release resources unless it is stopped.
try { m_player.stop(); m_player.close(); } catch (Exception e){ }