BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/)
-   Developer Forum (http://www.blackberryforums.com/developer-forum/)
-   -   Telling my thread what to do (http://www.blackberryforums.com/developer-forum/219435-telling-my-thread-what-do.html)

brentriss 02-11-2010 03:49 PM

Telling my thread what to do
 
Hi,

I created an application that creates an object that extends mainscreen.

that object creates an object that extends Thread.

THAT object gets intantiated and i do a .start() on it.

Naturally, that runs the run() function of that class that extends Thread, right?

I have that run() function do all the steps of creating a media player, prefetching and playing.

Now, how do I control that player with the menu buttons or UI buttons?

the object is called playerThread. Why can't I go playerThread.stopPlaying(); inside the run() of my menuItem???

Code:

class myApp extends UiApplication {
    public static void main(String[] args)
    {
        // Create a new instance of the application.
        myApp theApp = new myApp();

        // To make the application enter the event thread and start processing messages,
        // we invoke the enterEventDispatcher() method.
        theApp.enterEventDispatcher();
    }

    private myApp()
    {
        // Push the main screen instance onto the UI stack for rendering.
        pushScreen(new myAppScreen());
    }   
}

class myAppScreen extends MainScreen
{
    private String murl;
    private String currentUrlToPlay;
    private HttpConnection conn;
    private InputStream is;
    private SharedInputStream readAhead;
    private Player _player;

    myAppScreen() {
        boolean whereAmI = UiApplication.getUiApplication().isEventThread();
        System.out.println("Am I in the event thread?" + whereAmI);

        PlayerThread playerThread = new PlayerThread();
        playerThread.start();
    }
   
    class PlayerThread extends Thread {

        void startPlayer() {
            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    Status.show("invoked message.");
                }
            });

            murl = "*mp3streamURL*;deviceside=true;interface=wifi";
                try
                {
                    conn = (HttpConnection) Connector.open(murl, Connector.READ_WRITE);
                }
                catch (Exception e)
                {
                    murl = "*mp3streamURL*;deviceside=true";
                    try {
                        conn = (HttpConnection) Connector.open(murl, Connector.READ_WRITE);
                    }
                    catch (Exception f) {
                    }
                }
                try
                {
                    is = conn.openInputStream();
                    readAhead = SharedInputStream.getSharedInputStream(is);
                    _player = Manager.createPlayer(readAhead, "audio/mpeg");
                    _player.realize();
                    _player.prefetch();
                    _player.start();
                }
                catch (Exception e) {
            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    Status.show("failure.");
                }
            });
                }
        }

        void stopPlaying() {
            _player.stop();
        }

        public void run() {
            startPlayer();
        }
    }

    private MenuItem playItem = new MenuItem("Play", 200000, 10) {
    public void run() {
        Status.show("Connecting...");
    }
    };

    private MenuItem pauseItem = new MenuItem("Pause", 200000, 10) {
    public void run() {
        Status.show("Pausing...");
        playerThread.stopPlaying();
    }
    };


kellyjdrummer 02-12-2010 08:15 AM

"Intantiated?"

Dougsg38p 02-12-2010 03:09 PM

Maybe because you haven't specified the scope of the stopPlaying() method?

Try

public void stopPlaying()


All times are GMT -5. The time now is 05:05 AM.

Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.