Laurent
Laurent

Reputation: 1739

Inheritance and MediaPlayer

I have a class called CoordMediaPlayer that inherits from MediaPlayer. My CoordMediaPlayer must be able to use the MediaPlayer methods so It's why I use inheritance...

The problem is that to instanciate a MediaPlayer it must use a MediaPlayer.create() static method. It's not like if it just calls a constructor and then I can use method of the parent class normally. If I inherit from MediaPlayer I can't access to the create() method from a class that inherit MediaPlayer I guess because it's static method, I can't Override it.

In ideal I would want to have my CoordMediaPlayer, this CoordMediaPlayer would not contains a MediaPlayer object but I would be able to call all MediaPlayer methods directly from a CoordMediaPlayer instance. It would be still better if I don't have the same kind of static create() method in my CoordMediaPlayer, this creation would be done when I instanciate my CoordMediaPlayer.

The only one way I found to deal with this, is to have a MediaPlayer object in my CoordMediaPlayer, then my CoordMediaPlayer inherit from MediaPlayer and override all the methods I need just by calling the method of my MediaPlayer object... but that looks kind of weird to me... is there any other way to deal with this kind of situation, without having a MediaPlayer in my CoordMediaPlayer but call directly my inherited MediaPlayer methods ?

This is how I deal with this, my CoordMediaPlayer class inherit from MediaPlayer and at the same time contains an instance of a MediaPlayer and have to override all methods of MediaPlayer I want to use...

Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    context = getApplicationContext();

    int fileResId = context.getResources().getIdentifier("s2", "raw", context.getPackageName());
    CoordMediaPlayer cmp = new CoordMediaPlayer(context,fileResId);
    cmp.start();
}

CoordMediaPlayer

public class CoordMediaPlayer extends MediaPlayer  {
    private MediaPlayer mediaPlayer;
    private Point coordinates;

    @Override
    public void start() throws IllegalStateException {
        mediaPlayer.start();
    }

    public CoordMediaPlayer(Context context, int resId) {

        mediaPlayer = MediaPlayer.create(context,resId);
    }

    public void setCoordinates(Point coordinates) {
        this.coordinates = coordinates;
    }

    public Point getCoordinates() {
        return coordinates;
    }
}

Upvotes: 0

Views: 195

Answers (1)

Josef Adamcik
Josef Adamcik

Reputation: 5780

MediaPlayer class actually has public costructor so you don't have to use the create(..) method for contruction. Documentation only suggest to use static methods in some cases.

By the way, the approach you illustrated in your question is called "composition" and is legit and usually thought as better approach than iheritance. Check this stackoverflow response: Prefer composition over inheritance?.

However I'd recomend to think more about reasons, why do you think you need to inherit from the MediaPlayer class, in your case. I honestly think you don't need to and you can just use own class encapsulating MediaPlayer instance and providing it's own api to the rest of your application.

Upvotes: 1

Related Questions