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 Contributor
Cluber22
Posts: 4
Registered: ‎07-11-2011
My Device: Curve 8520
My Carrier: Virgin Mobile
Accepted Solution

playerUpdate() method not being called in PlayerListener

[ Edited ]

I am testing on my blackberry curve and compiling using BB JRE 6.0

 

There is no actual error but when I am trying to recieve events on my listener, it just isn't been executed.

 

    private void loadSound() {
       
        try {
            InputStream stream = (InputStream)this.getClass().getResourceAsStream("/HelloWorld.mp3");
            player = javax.microedition.media.Manager.createPlayer(stream, "audio/mpeg");
            player.addPlayerListener(new PlayerListener(){
                public void playerUpdate(Player player, String event, Object eventData) {
                    /*if (event == PlayerListener.STARTED) {
                        Dialog.alert("Started");
                        btnPlayHelloWorld.setLabel("1");
                    }
                    if (event == PlayerListener.END_OF_MEDIA) {
                        Dialog.alert("End");
                        btnPlayHelloWorld.setLabel("2");
                    }
                    if (event == PlayerListener.CLOSED) {
                        Dialog.alert("Closed");
                        btnPlayHelloWorld.setLabel("3");
                    }*/
                    Dialog.alert("Hello");
                    btnPlayHelloWorld.setLabel("1");
                }
            });
            player.realize();
            player.prefetch();
        } catch (IOException e) {
            System.out.println("Error creating player");
        } catch (MediaException e) {
            System.out.println("Error media type");
        }
       
    }
   
    private void playSound(){
        try {
            player.start();
            btnPlayHelloWorld.setLabel("Stop");
        } catch (MediaException e) {
            System.out.println("Error media type");
        }
    }

 

I can't work out why it's not entering it at all. Full code is here http://pastie.org/private/ti6i6qmxvd8cib07ufrrvq



EDIT: The sound does play it's just the listener that is the problem.

Please use plain text.
Administrator
MSohm
Posts: 14,241
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: playerUpdate() method not being called in PlayerListener

You should be passing in an instance of your own class that overrides PlayerListener in the addPlayerListener method.  Otherwise, the system has no way to notify you.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
BlackBerry Development Advisor (Retired)
BlackBerry Development Advisor (Retired)
BVP
Posts: 150
Registered: ‎10-19-2010
My Device: Not Specified
My Carrier: Rogers

Re: playerUpdate() method not being called in PlayerListener

Also, 'event' is a String, so you should use String.equals() instead of '=='.

Please use plain text.
Developer
Aviator168
Posts: 709
Registered: ‎09-10-2009
My Device: 8520, 8900, 9000, 9300, 9650, 9700, 9780, 9800, 9810, 9900, 9930
My Carrier: Verizon

Re: playerUpdate() method not being called in PlayerListener


BVP wrote:

Also, 'event' is a String, so you should use String.equals() instead of '=='.



What the original poster did is also correct since he is comparing if event is the same object as one of the string objects. What he didn't do was to create his own listener classs.

Please use plain text.
BlackBerry Development Advisor (Retired)
BlackBerry Development Advisor (Retired)
BVP
Posts: 150
Registered: ‎10-19-2010
My Device: Not Specified
My Carrier: Rogers

Re: playerUpdate() method not being called in PlayerListener

Actually, now that I think about it, using an anonymous class for the PlayerListener implementation (as Cluber22 is doing above) should be fine, and in fact works great when I try it.  The problem is actually the Dialog.alert call is throwing an Exception that is being swallowed by the playerUpdate method - because it is run asynchronously  in a new thread the Exception has no one to intercept it when it gets thrown up, so it disappears!  If you add a try/catch around it and log it to System.out,  you will see it (or remove it altogether - FYI the setLabels() will also throw an Exception for the same reason).

 

The Dialog (and setLabel) methods must be run on the app event thread, so the solution is to eiether make the calls in an invokeLater() to push it onto the event thread, or log it in some other way that does not use UI calls.

 

What the original poster did is also correct since he is comparing if event is the same object as one of the string objects.

 

Sorry, my point here was that this only works because the implementation is calling the playerUpdate method and passing in a PlayerListener constant as the event String.  If the underlying implementation were passing "started" instead of PlayerListener.STARTED explicitly when invoking the listener (for example), this code would break (since both Strings have the same value but they are differernt objects so the == woud return false in this case). 

 

Using the equals() method would guarantee that the code works the same on all JSR-135 MMAPI-compliant platforms.  Just a java best-practice thing - it is typically very rare that you need to know if 2 Strings are the same reference but very common to want to know if they are the same value.  Maybe all of this would have been good to add to my original post :smileyvery-happy:

 

 

Please use plain text.