Hello! I am writing a player for Android and I now have this picture:

  1. Activity class It contains a list of music tracks, as well as the bottom panel. On the panel there are buttons "Back", "Play / Pause" and "Next", as well as the name of the track and its author.

  2. List adapter class In the list, each element has a name and author of the track, as well as either the first letter of the track name in the circle (if this track is not playing), or ProgressBar (if preparing to play), or MiniEqualizer (if music is playing), or the Play icon (if music is paused). Clicking on the item (list item) starts playing the track, pressing it again pauses.

  3. Class player. It has MediaPlayer , which is responsible for playing music, this class has methods for controlling playback, information about the track being played is also stored there, and there are methods getters returning this information.

  4. PlayBack interface. This is actually the question. I created an interface (we will not complicate it, let it be only play and pause methods). By clicking on a pause from the bottom panel in the list item, the equalizer should change to the Play icon. It is the same with stopping music from the list and changing the picture in the bottom panel. Ie, in simple, language, the bottom panel (activity) and the list (adapter) should be mutually connected, so I decided to resort to the interface. Initially, an instance of the Player class is created in activity, then passed to the adapter. I call its methods, there they pull the methods of the interface, and thus realize the connection between the two classes.

What is the problem?

I pass 2 Playback to the class of the player: one for activity, the second for the adapter, and call their methods in two places. Is it possible to somehow call in one?

  • Pattern "observer" to help. Sign up for changes in MediaPlayer . And he, in turn, when someone changes the state (no matter who), will notify all subscribers about this change. As soon as the activation receives this notification, it will update the list and update the panel. - Vladimir Parfenov
  • @Vladimir Parfenov, then all the same there is something! Observer means. Where to read about him? :) - Flippy
  • 2
    Yes, even in Google. But I liked the explanation in the Head First Design Patterns book. - Vladimir Parfenov
  • @Vladimir Parfenov, why didn't you say right away - an observer? :) - Flippy
  • Well, you are able to translate) - Vladimir Parfenov

1 answer 1

the media player has a set of lisener interfaces for all events. For example, myMediaPlayer.setOnCompletionListener(OnCompletionListener (activity or adapter)) Read more in the documentation , let your activations and the adapter implement these interfaces. But I think this is not the best option. If you are writing a player, then in the future you will need to play music on the background, hence Service . and it is the service that will interact with the media player. And the activation and the adapter is already with the service. There is a good guide on the topic of players.

  • Come on, the player’s class can be remade into service without any problems at all, the essence is different - Flippy
  • I know all about these lists. - Flippy