12-07-2008 08:41 PM
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
Solved! Go to Solution.
12-09-2008 02:47 PM
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.
12-09-2008 03:34 PM
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.
12-09-2008 09:06 PM
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()); } }
12-09-2008 09:57 PM - last edited on 12-09-2008 10:07 PM
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.
12-09-2008 10:12 PM
Got it to work by moving the mp3 file to the base directory and changing the string to "/switch.mp3".
01-04-2009 06:53 PM
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?
01-07-2009 09:10 AM
01-07-2009 02:30 PM
Mark, i have a Telus Storm v 0.76 and this is also the problem.
01-09-2009 01:43 PM
one final question:
what sound API did the games WordMole and BrickBreaker use?
i can hear the sounds fine.