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
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Playing music fromBackground Application

There are other improvements I would make to your code, but in reality I think it will work fine.  However I would encourage you to close the connection in a finally clause like the following:

 

    public void playMusic (String musicToPlay) {
        FileConnection fileConnection = null;
        InputStream inputStream = null;
        try {
            fileConnection = (FileConnection) Connector.open(musicToPlay);
            inputStream = fileConnection.openInputStream();
            byte [] audioData = IOUtilities.streamToBytes(inputStream);
            ByteArrayInputStream bais = new ByteArrayInputStream(audioData);
            player = Manager.createPlayer(bais, "audio/m4a");
            player.realize();
            player.prefetch();
            player.setLoopCount(-1);
            player.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Getting exception from "+e.getMessage());
            e.printStackTrace();
        } catch (MediaException e) {
            // TODO Auto-generated catch block
            System.out.println("Getting exception from Media exception "+e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if(inputStream != null) {
                    inputStream.close();              
                }
                if(fileConnection != null) {
                    fileConnection.close();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                System.out.println("Getting exception from Media close "+e.getMessage());
                e.printStackTrace();
            }
        }
    }

Highlighted
Developer
Posts: 144
Registered: ‎03-15-2011
My Device: blackberry smart phone
My Carrier: Airtel

Re: Playing music fromBackground Application

yeah done that before i was not closing input stream but it was working though Smiley Wink

Hey can you see this question please...http://supportforums.blackberry.com/t5/Java-Development/getting-control-over-pushing-screen-globally...