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
moinuddin
Posts: 1
Registered: ‎11-16-2009
My Device: Not Specified

Re: 2 Questions about playing short mp3

Hi,

I m running an application of playing multiple mp3 files simultaneusly. Here is my player load and play logic look like

 public class PSound
{
        private javax.microedition.media.Player player=null;   
        private InputStream is = null;     

 

       boolean isPlaying()
        {
                return false;
        }

        public PSound()
        {

        }

 

        public int Load(String path)
        {
                try
                {
                        is = this.getClass().getResourceAsStream(path);
                        player = javax.microedition.media.Manager.createPlayer(is, "audio/mpeg");
                        player.realize();
                        player.prefetch();
                        VolumeControl vc = (VolumeControl) player.getControl("VolumeControl");
                        vc.setLevel(70);
                        return 0;
                }
                catch (Exception e)
                {
                        System.out.println("Unable to load sound. => " + e);
                        return -1;
                }
        }

 

       public void Play()
        {
                try
                {
                    if(player == null)
                    {
                          return;
                    }
                   if(0 < player.getMediaTime())
                   {
                    
                     player.setMediaTime(-1);
                   }
                   if(player.getState() == javax.microedition.media.Player.STARTED)
                   {
                          player.stop();
                          player.deallocate();
                          player.prefetch();
                   }     
                   player.start();
                }
                catch (Exception e)
                {
                        System.out.println("!!----!!!!!!!Unable to play sound. => " + e);
                }
      }

}

I m creating separate instances of player running in separate threads, still the audio files are not playing simultaneously.

Is there anything I m missing??

Or blackberry can only run one  audio file at a time.

 

Thanks....

 

 

 

Please use plain text.
Contributor
sampablokuper
Posts: 10
Registered: ‎12-17-2010
My Device: Not Specified

Re: 2 Questions about playing short mp3

I'm also having trouble getting a short MP3 file to play. Because I'm a newbie at BlackBerry development, I've uploaded my project to http://stroke.sampablokuper.com/stroke.zip because I don't know if the problem's with my java programming, or the way I've laid out the resources in my project, or something else.

 

It's a very small project, so please help me by seeing if you can run it without errors in the Curve 8520 simulator on your computer. (It's designed for the 8520, because that's the phone a friend of mine has; I don't have a BB myself - yet!)

 

The idea is that when the user presses/scrolls "down" on the trackball/pad, a sound will be played, but currently instead of the sound, I just get an error message: javax.microedition.media.MediaException .

 

I've tried to debug this, but as I say, I'm a total newbie to BB development, so I don't really know how to make sense of the information I get from the breakpoints I've set.

 

I really want to finish this before Christmas; please help!

 

Thanks in advance :smileyhappy:

Please use plain text.
Developer
Marc_Paradise
Posts: 155
Registered: ‎01-22-2010
My Device: PlayBook -> 9900, 9810, 9800, 9700,9000, 8800, 8700...

Re: 2 Questions about playing short mp3

@sampablokuper 

 

I'll need more info to help you.  Instead of using toString on the exception, use getMessage.

 

Too - have you stepped through with a debugger to find out where the failure is occurring?

 

The first thing that comes to mind, based on what's in your code, is that the format of the string in createPlayer is wrong:

 

.createPlayer("103574__ryansnook__growl2.mp3");

 

But according to the javadoc (http://www.blackberry.com/developers/docs/4.5.0api/javax/microedition/media/Manager.html) the String argument is media locator - probably not what you want in this case.

 

Instead look at creating a stream for the MP3 file:

createPlayer(InputStream stream, String type)
          Create a Player to play back media from an InputStream.

 

Another option is to use the MediaPlayer/MediaManager classes from the BlackBerry platform.

**

 

Side note - I saw you stopped by ##blackberry on freenode IRC but only waited 20 min or so before leaving  -- it's alway best to wait for a while when you post a question; people are in different time zones and while you'll almost never get an immediate reply... however if you stick around, a reply usually does come along, sooner or later. 





Try out BBSSH, a free Blackberry SSH client.
  • If you like my post, please let me know by Liking it!
  • If my post solved your problem please click on the Accept as Solution button.

Please use plain text.
Contributor
sampablokuper
Posts: 10
Registered: ‎12-17-2010
My Device: Not Specified

Re: 2 Questions about playing short mp3

@Marc_Paradise, thanks for all the advice. I disconnected from IRC as my plans for the evening changed at short notice & I had to run to catch a train! Instead of programming this evening, I ended up having other things to do, but I'll go through all your suggestions asap; tomorrow morning if possible. Looking forward to it. Thanks again!

Please use plain text.
Contributor
sampablokuper
Posts: 10
Registered: ‎12-17-2010
My Device: Not Specified

Re: 2 Questions about playing short mp3


Marc_Paradise wrote:

@sampablokuper 

I'll need more info to help you.  Instead of using toString on the exception, use getMessage.


I used toString based on the BlackBerry docs here. Changing this to getMessage as per your suggestion has the following effect: in the simulator, the alert box with the error message is empty. Instead of having an exclamation mark, an "OK" button and the text, "javax.microedition.media.MediaException", it now only has an exclamation mark and an "OK" button.


Marc_Paradise wrote:

Too - have you stepped through with a debugger to find out where the failure is occurring?


Yes. I did this before posting my initial help request. I put a breakpoint on the line:

} else if (action == "growl") {

 After that breakpoint triggers, I then step through with F5, and when the line

Player p = javax.microedition.media.Manager.createPlayer("103574__ryansnook__growl2.mp3");

is executed, the following item appears in the Debug pane in Eclipse: "Manager.<clinit>() line: 386". Double-clicking that item brings up a pane with "Manager.<clinit>() line: 386" as its title, the following text in red: "Source not found.", and a button marked "Edit Source Lookup Path".

 

If I subsequently press F8 (resume), then the next time I press the Down key in the simulator to attempt to play the sound, a different item appears in the Debug pane in Eclipse instead: "Manager.createPlayer(String) line: 737". Similarly, double-clicking that item brings up a pane with "Manager.createPlayer(String) line: 737" as its title, the following text in red: "Source not found.", and a button marked "Edit Source Lookup Path".


Marc_Paradise wrote:

The first thing that comes to mind, based on what's in your code, is that the format of the string in createPlayer is wrong:

 

.createPlayer("103574__ryansnook__growl2.mp3");


Again, that code is based on the BlackBerry docs here. If my code is wrong in that respect, then so are the docs, unless I'm much mistaken. I'd be very grateful for clarity on this point!


Marc_Paradise wrote:

But according to the javadoc (http://www.blackberry.com/developers/docs/4.5.0api/javax/microedition/media/Manager.html) the String argument is media locator - probably not what you want in this case.


Aha! Those javadocs may be the clarity I was looking for! The fact that the String argument is a "media locator", which the javadocs say is a URI, would explain why in the docs I linked above, a URL worked, but simply putting the name of a file doesn't. I need to somehow turn the file name of my MP3 into a URI.

 

So instead of just having .createPlayer("103574__ryansnook__growl2.mp3") , I should probably have something more like .createPlayer("file://103574__ryansnook__growl2.mp3") or .createPlayer("file://res/sounds/103574__ryansnook__growl2.mp3")

 

The first of these two attempts produces an alert box with the error message, "java.io.IOException" (I've switched back to toString instead of getMessage), and the second produces an alert box with the error message, "java.io.InterruptedIOException: Local connection timed out after ~ 120000".

 

.createPlayer("file:///103574__ryansnook__growl2.mp3") produces, "net.rim.device.api.io.file.FileIOException: File system error (1003)", as does .createPlayer("file:///res/sounds/103574__ryansnook__growl2.mp3")

 

I've tried some other variations on URI syntax, none successfully, but I've a hunch that it's just a matter of finding the right syntax. Help with this would be much appreciated!


Marc_Paradise wrote:

Instead look at creating a stream for the MP3 file:

createPlayer(InputStream stream, String type) 
          Create a Player to play back media from an InputStream.

 

Another option is to use the MediaPlayer/MediaManager classes from the BlackBerry platform.


If we can find the right way to express the file name as a URI, I think I'll be home free. If not, then I'll try those approaches.

 

Thanks again!

Please use plain text.
Contributor
sampablokuper
Posts: 10
Registered: ‎12-17-2010
My Device: Not Specified

Re: 2 Questions about playing short mp3

Gave up trying to formulate a URI that would work. (Seriously, BlackBerry, improve your docs!)

 

Ended up using: 

 

InputStream is = getClass().getResourceAsStream("/103574__ryansnook__growl2.mp3");
Player p = javax.microedition.media.Manager.createPlayer(is,"audio/mp3");

 It's a bit crufty by comparison, but at least it works.

 

Please use plain text.
Developer
Marc_Paradise
Posts: 155
Registered: ‎01-22-2010
My Device: PlayBook -> 9900, 9810, 9800, 9700,9000, 8800, 8700...

Re: 2 Questions about playing short mp3

Assuming you're bundling the file in your project COD, try:

 

cod://filename.mp3

 

Other than that, I think your current solution of using a stream is best (especially since it works :smileywink: I don't tthink that the media player will close the stream upon completion, so you might need to do that yourself.



Try out BBSSH, a free Blackberry SSH client.
  • If you like my post, please let me know by Liking it!
  • If my post solved your problem please click on the Accept as Solution button.

Please use plain text.
Contributor
sampablokuper
Posts: 10
Registered: ‎12-17-2010
My Device: Not Specified

Re: 2 Questions about playing short mp3

Thanks, Marc. I tried cod://filename.mp3 without success - please let me know if that's anomalous.

 

Thanks also for the heads-up about closing the stream.

Please use plain text.
Developer
precisionpete
Posts: 264
Registered: ‎10-05-2010
My Device: Not Specified

Re: 2 Questions about playing short mp3

[ Edited ]

I'm having trouble playing very short "beep" clips on OS 4.5.0 devices (e.g. 8100). The mp3 clips are only about 40ms long. If I play a longer one, it seems like the first 150ms is being ignored and it just catches the end of the clip.

 

These work just fine on devices runnign OS 5 or 6. Note: The app is compiled for 4.5.0.

 

I know I could make the clips longer. But that will sound different or at least introduce a delay on devices that do it right.

 

The code is based on the example above. I've tried a bunch of things all with the same result...

 

Even stranger is that this same code works fine with another app.

 

Has anyone got any workarounds? 

 

Thanks

 

 

public class Beeper {

	static Player _beep1, _beep2;
	static Options options = Options.getInstance();
	private int _currentVolume;
	UiApplication theApp = UiApplication.getUiApplication();
	
	private Beeper(){
		try {
			Class cl = getClass();
			_beep1 = Manager.createPlayer(cl.getResourceAsStream("/beep.mp3"),"audio/mpeg");
			_beep2 = Manager.createPlayer(cl.getResourceAsStream("/beep-beep.mp3"),"audio/mpeg");
			_beep1.prefetch();
			_beep2.prefetch();		
		} catch (IOException ioe) {
			System.out.println("IOE: "+ioe.toString());
		} catch (Exception e){
			System.out.println("E: "+e.toString());
		}
		_currentVolume = options.getVolume();
		setVolume(_currentVolume);
	}
	static private Beeper _instance = new Beeper(); 
	static public Beeper getInstance() {
		return _instance;
	}
    public void setVolume(int level) {
        VolumeControl vol1 = (VolumeControl) _beep1.getControl("VolumeControl");
        VolumeControl vol2 = (VolumeControl) _beep2.getControl("VolumeControl");
        vol1.setLevel(level);
        vol2.setLevel(level);
    }    
    public void volumeUp() {
        _currentVolume += 10;
        if (_currentVolume > 100) {
            _currentVolume = 100;
        }
        setVolume(_currentVolume);
        options.setVolume(_currentVolume);
    }
    public void volumeDown() {
        _currentVolume -= 10;
        if (_currentVolume < 0) {
            _currentVolume = 0;
        }
       setVolume(_currentVolume);
       options.setVolume(_currentVolume);
    }    
	void beep(int beeps){
		if (options.getBeepMode()==0){
			try {
				switch(beeps){
				case 1:
					_beep1.start();
					break;
				case 2:
					_beep2.start();
					break;
				}
			} catch (MediaException me) {
				System.out.println("ME: "+me.toString());
			}
		}
	}
}

 

 

Please use plain text.