Question

So I am trying to make a music player that runs on a separate thread. I want to have methods inside of the Player class (that implements Runnable) to pause, play and go to next/prev song. If I was to call inner methods of the Thread, would it still run on a separate thread? Everything works as it should, but I am still new to Threads so I'm not sure if this is good practice or not. Could someone shed some light on this?

class Player implements Runnable, OnCompletionListener {
    public static final String TAG = "Player";
    private MediaPlayer mp;
    private boolean isPlaying = false;
    private SongsManager sm = null;
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    private int currentSongIndex = 0;

    public Player(int currentSongIndex){
        mp = new MediaPlayer();
        mp.setOnCompletionListener(this);
        sm = new SongsManager();
        songsList = sm.getSongList();
        this.currentSongIndex = currentSongIndex;
    }

    @Override
    public void run() {
        try{
            mp.reset();
            mp.setDataSource(songsList.get(currentSongIndex).get(SongsManager.KEY_PATH));
            mp.prepare();
            mp.start();
            isPlaying = true;
        } catch (IllegalArgumentException e){
            e.printStackTrace();
        } catch (IllegalStateException e){
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    public synchronized void pause() {
        if (isPlaying){
            if (mp != null){
                mp.pause();
                isPlaying = false;
            }
        }
    }

    public synchronized void play() {
        if (!isPlaying){
            if (mp != null){
                mp.start();
                isPlaying = true;
            }
        }
    }
    ...
}

So say, if I created a new Thread like so,

Player player = new Player(0);
playerThread = new Thread(player, Player.TAG);
playerThread.start();

and called player.pause(), then player.play() from a different thread, would the player still be running on the separate thread?

Était-ce utile?

La solution

If I was to call inner methods of the Thread, would it still run on a separate thread?

No. Threads aren't magic. When you call a method you are calling that method with the current thread. To fork a thread you actually have to create a thread object (or executor service) and start it. Then it calls run(), etc..

So if you called player.pause() you would be calling pause from the current thread which is possibly the "main" thread. Your playerThread would be doing something else in the background. The whole reason why the pause() method is synchronized is so you can all it from the outside thread and the playerThread without them overlapping. It also means that multiple threads can test and set the isPlaying boolean and other threads will see the updates.

You should most likely start with the Java thread tutorial.

Autres conseils

The content of the run() method is what executes on the separate thread. Anything that calls a method of of the class that contains run() will execute that method on its own thread. You must therefore insure that any data access is properly thread-safe.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top