mercredi 5 septembre 2018

Singleton for multiple uses?

I am developing an android application in which I can/allowed to use only one instance of MediaPlayer(Singleton instance).

static MediaPlayer mp;
public MediaPlayer getMediaPlayer() {
  if (mp == null) {
     mp = new MediaPlayer();
     return mp;
  }
  return mp;
}

Now This activity can be called for two purposes

  1. from within application (Say ABC)
  2. from any other application (say XYZ) for preview

This instance of mediaplayer is supposed to play either video or audio. So I have created a class which handles request(parameterized constructor to check for audio instance needed or video instance needed) and based on request, it creates required instance.

But the problem arises when Within application user is playing an audio file and user launched other application (XYZ) and requested to play video.

Now I am storing MediaPlayer's previous state (like which file it was playing and current position etc) and releasing MediaPlayer to be used for XYZ application for Video Playing. And once user stops playing video, ABC resumes playing audio file based on the stored instance.

So is this the right approach? Or I need to do modify the architecture of this task?

Please suggest w.r.t. design patterns

Aucun commentaire:

Enregistrer un commentaire