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: 143
Registered: ‎07-14-2008
My Device: Not Specified
Accepted Solution

2 Questions about playing short mp3

I'm trying to setup a simple Class Snd to encapsulate sound effects, so I can do this:

 

Snd sound = new Snd( );

sound.throwSwitch( );   // Plays a short mp3 switch sound

 

So, after reviewing RIM and other working examples Iam trying this:

 

import java.io.InputStream;
import javax.microedition.media.*;

public class Snd {
   
    private Player player;
   
    // Find the supported content types
    static final String types[] = Manager.getSupportedContentTypes(null);

    public void throwSwitch() {
        // Makes a click
        for (int cnt = types.length - 1; cnt >= 0; --cnt) {
            if (types[cnt].equals("audio/mpeg")) {
                try {
                    // Retrieve the MP3 file
                    //Class clazz = Class.forName("net.berrysoft.dib.sound");
                    //InputStream is = clazz.getResourceAsStream("switch.mp3");
                    InputStream is = getClass().getResourceAsStream("switch.mp3");
                   
                    // Create an instance of the player from the InputStream
                    player = javax.microedition.media.Manager.createPlayer(is, "audio/mpeg");
                    player.realize();
                    player.prefetch();
                    player.start();            // Start the player
                    Manager.playTone(0x45, 2000, 100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } // end: for
    }
   
}

 

but I don't get any sound.  I have verified that the mp3 format is supported, and loaded the sound to my media card and played it as a ring tone no problem.

 

What might I be doing wrong?

 

Second question is that in the past (other languages) I've had two ways to play a sound... one as a "one-shot" meaning not streamed, and as a stream (used for sounds to large to contain entirely in memory).  In Java does this distinction exist?  It seems the above code streams my sound, but it is so short, it could be held entirely in memory.  It is only played once (at startup), but I'm wondering given it is such a short effect if there is a better way.

 

Patrick

 

BlackBerry Development Advisor
Posts: 15,813
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: 2 Questions about playing short mp3

What BlackBerry model and handheld software version are you testing on?  You can find this under Options, About on the BlackBerry handheld.  Is any exception thrown when the file fails to play?

 

 

The Player class itself handles the buffering/streaming/etc, so your application doesn't have to worry about this.  It'll adjust itself based on the type of file and its location.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: 2 Questions about playing short mp3

I'm coding to 4.2.1 and testing it on an 8820.  No exception is thrown.

 

I'm compiling on Eclipse, and wonder if maybe the mp3 resource is just not being found.

 

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

Re: 2 Questions about playing short mp3

Here is an example that works. In the example, the COD file generated is com_foo_Foobar, Foobar is the root directory for the COD file, and Foobar.java is the main applicaiton class. The MP3 is in the root '/' directory of the COD file. Note the "Class.forName" call and the non-intuitive way that the root is specified within this string parameter. This took me quite a few tries before I figured it out.

 

public void play() { if (m_player != null){ stop(); } try { Class cl = Class.forName("com.foo.Foobar.Foobar"); InputStream input = cl.getResourceAsStream("/foonoise.mp3"); m_player = Manager.createPlayer(input,"audio/mpeg"); m_player.realize(); m_volumeControl = getVolumeControl(m_player); if (m_volumeControl != null) m_volumeControl.setLevel(m_volume); m_player.prefetch(); m_player.start(); } catch (Exception e){ System.out.println("ERR: " + e.toString()); } }

 

 

 

Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: 2 Questions about playing short mp3

[ Edited ]

Well, given your explaination, this is what I tried:

 

package:  net.berrysoft.dib

Main class: Dib.java

 

switch.mp3:  located in subdirectory {base}/sounds

 

 

public void playClick( ) { try { Class cl = Class.forName("net.berrysoft.dib.Dib"); InputStream is = cl.getResourceAsStream("/sounds/switch.mp3"); // Create an instance of the player from the InputStream player = Manager.createPlayer( is, "audio/mpeg" ); player.realize(); volumeControl = getVolumeControl(player); if (volumeControl != null) volumeControl.setLevel(volume); player.prefetch(); player.start(); } catch (Exception e) { System.out.println("ERR: " + e.toString()); } } private VolumeControl getVolumeControl( Player p ) { return (VolumeControl)p.getControl( "VolumeControl" ); }

 

 

but still no joy.

 

So, it should be finding the mp3 in the sounds sub-directory from the Dib class in the base directory.

Message Edited by Berrysoft on 12-09-2008 10:06 PM
Message Edited by Berrysoft on 12-09-2008 10:07 PM
Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: 2 Questions about playing short mp3

Got it to work by moving the mp3 file to the base directory and changing the string to "/switch.mp3".

 

Developer
Posts: 1,636
Registered: ‎07-14-2008
My Device: Z10
My Carrier: Fido

Re: 2 Questions about playing short mp3

i'm having a strange problem on the Storm using the above code.

 

It appears the sound only comes out of the Headset.  If I unplug

the headset it doesnt play in the speaker.  I even reset the device and it is the same.

 

anyone have this problem?

 

BlackBerry Development Advisor
Posts: 15,813
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: 2 Questions about playing short mp3

This is an issue with BlackBerry handheld software version 4.7.0.75 for the BlackBerry Storm.  You can read a lengthy thread about this here:  http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=11963&query.id=29...
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Developer
Posts: 1,636
Registered: ‎07-14-2008
My Device: Z10
My Carrier: Fido

Re: 2 Questions about playing short mp3

Mark, i have a Telus Storm v 0.76 and this is also the problem.

 

Developer
Posts: 1,636
Registered: ‎07-14-2008
My Device: Z10
My Carrier: Fido

Re: 2 Questions about playing short mp3

one final question:

 

what sound API did the games WordMole and BrickBreaker use?

i can hear the sounds fine.