Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 109
Registered: ‎02-09-2009
My Device: Not Specified

Need help with efficiently playing the same sounds

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" );

    }

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Need help with efficiently playing the same sounds

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){ }