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
New Developer
Posts: 47
Registered: ‎10-19-2009
My Device: Not Specified

Play a background music

Hi all

   I want to play a small background music continously when a button is pressed. How can i do that?.

 

Thanks in advance.

Developer
Posts: 133
Registered: ‎07-20-2009
My Device: Not Specified

Re: Play a background music

Try to use player to play music and then request the app to background by using

 

UiApplication.getUiApplication.requestbackground

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Play a background music

Just to clarify when you say

"I want to play a small background music continuously when a button is pressed"

do you mean that it only plays while the button is pressed or that the button press starts it playing?

New Developer
Posts: 47
Registered: ‎10-19-2009
My Device: Not Specified

Re: Play a background music

Thanks for the reply

 

      I wanted a music file which is of 1 second starts playing in a loop after a button is pressed. this should go on till i press another button.

 

 Another doubt, when a this background music has started can i play another music simultenously which would be start some specified timer.

Developer
Posts: 110
Registered: ‎10-22-2009
My Device: 9700
My Carrier: ChinaMobile

Re: Play a background music

use Button.setChangeListener to detect the pressing action.

 

and use Player to control a music file.

Best Regards,
Tiandi
www.tiandiyoyo.com
New Developer
Posts: 47
Registered: ‎10-19-2009
My Device: Not Specified

Re: Play a background music

Thanks tiandi79

    Can I play another music file after some time. I.e. the background music is on and I would start a timer when that timer finishes I want another music simultaneously.

Developer
Posts: 110
Registered: ‎10-22-2009
My Device: 9700
My Carrier: ChinaMobile

Re: Play a background music

so u mean play 2 music files at the same time on device?

 

yes, u can set up 2 Player to run.

Best Regards,
Tiandi
www.tiandiyoyo.com
New Developer
Posts: 47
Registered: ‎10-19-2009
My Device: Not Specified

Re: Play a background music

[ Edited ]

Thanks tiandi79

    How can we do it.

 

     Another thing the I created a player instance for a single music file. whats happening is on bold this works fine but on storm the file is not played and when i debug the program its saying file not found. The code which i have written is below and the Startup.wav is in source package.

 

 

 

import java.io.InputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;

public class BBMusic extends UiApplication
{

public BBMusic()
{
pushScreen(new PlayScreen());
}

public static void main(String args[])
{
BBMusic play = new BBMusic();
play.enterEventDispatcher();
}
}

class PlayScreen extends MainScreen
{
public PlayScreen()
{
super();
setTitle("Play Music");

ButtonField btn = new ButtonField("Play",ButtonField.CONSUME_CLICK)
{
protected void fieldChangeNotify(int context)
{
// TODO Auto-generated method stub
Thread t = new Sound();
t.start();
super.fieldChangeNotify(context);
}
};
add(btn);
}

}


class Sound extends Thread
{
public void run()
{
try{
InputStream is =getClass().getResourceAsStream("Startup.wav");
Player player = Manager.createPlayer(is,"audio/x-wav");
player.prefetch();
player.start();
}
catch (Exception e) {
// TODO: handle exception
}

}

 

 

 

Developer
Posts: 110
Registered: ‎10-22-2009
My Device: 9700
My Carrier: ChinaMobile

Re: Play a background music

the simple code as below

class mainscreen extends MainScreen{
    public mainscreen() {
        super();
        ButtonField b1 = new ButtonField("play",ButtonField.CONSUME_CLICK);
        b1.setChangeListener(new FieldChangeListener() {

            public void fieldChanged(Field field, int context) {
                try {
                    Player p1 = Manager.createPlayer("file:///SDCard/1.mp3");
                    p1.start();
                } catch (IOException ex) {
                    Dialog.alert("can not found file");
                } catch (MediaException ex) {
                    Dialog.alert("format error");
                }
            }
        });
        add(b1);
    }
}

 sorry I dont test it on storm.

Best Regards,
Tiandi
www.tiandiyoyo.com
New Developer
Posts: 47
Registered: ‎10-19-2009
My Device: Not Specified

Re: Play a background music

Hi Tiandi

  when i use this code i should put the code in SDCard or some location in phone memory. Is it not possible to put it in your project folder and play it.