Adam
Adam

Reputation: 1964

which Media Player reached its end, and different set of orders on the onCompletion() method

Lets say I have several players I've created. And Lets say that for each one of them, I want a different set on actions to be activated.

How can I locate which player has been finished (the implementation itself of the case sentence) in order to act differently for each player ?

For example: in order to handle a single player, all we have to do is to implement the onCompletion() method like this:

public void onCompletion(MediaPlayer mp){
    //DO SOMETHING HERE THAT FITS FOR ALL KINDS OF PLAYER'S TYPE OBJECTS
}

How do I add a case sentence to expand it to handle several different player objects ?

Thanks !

Upvotes: 0

Views: 866

Answers (2)

Phil
Phil

Reputation: 36289

The MediaPlayer attribute passed to this method is the same MediaPlayer that has just completed, so as long as you are keeping a pointer to each media player (such as through a global variable), then all you need to do is check which media player you have received:

public class MyClass implements OnClompleteListener 
{
    MediaPlayer player1, player2, player3;
    //initialize them
    player1.setOnCompleteListener(this);
    player2.setOnCompleteListener(this);
    player3.setOnCompleteListener(this);

    @Override
    public void onCompletion(MediaPlayer mp)
    {
        if (mp == player1)
        {
            //TODO handle player 1 completion
        }
        else if (mp == player2)
        {
            //TODO handle player 2 completion
        }
        else if (mp == player3)
        {
            //TODO handle player 3 completion
        }
    }
}

You can also handle this in-line, without implementing the OnCompleteListener:

player1.setOnCompleteListener(new OnCompleteListener() {

    @Override
    public void onCompletion(MediaPlayer mp)
    {
        //mp IS ALWAYS EQUAL TO player1!
    }
});

Upvotes: 4

Joel
Joel

Reputation: 4762

You can either create a class that extends MediaPlayer and give it an attribute like a number to keep track of which player is which. Then you would check the player's number in the onCompletion() method. Otherwise, you could create different onCompletion listeners and set them to the different players.

Upvotes: 0

Related Questions