Mvcplayer
I tried to use the MVC pattern on Android to develop a music player.
GitHub Address: Https://github.com/skyhacker2/MVCPlayer
What is MVC
From Wikipedia
- Controller controllers-responsible for forwarding requests and processing requests.
- Views View-Interface designer for graphical interface design.
- Model-Programmer to write programs should have the function (implementation algorithm, etc.), database experts for data Management and database design (can achieve specific functions).
So on Android, the activity is the controller.
Division of responsibilities
- The activity is responsible for handling view events and getting model data and refreshing the view.
- View is only responsible for displaying
- Model is the data part, cannot update view, data change must update view through activity.
Design our playerModels
- Music-Represents a song, song name, path, singer and other information
- Musicplayer-Player, our logic Processing section.
Why is playing music not doing in the activity?
Playing music is part of the model, not part of the activity.
What do I think the model represents?
So the player is a model, I design a player class to encapsulate the logic of playing music.
Musicplayer.java
public class MusicPlayer { public interface PlayerListener { void onPlay(); void onPause(); void onResume(); void onPlayNext(); void onPlayPrev(); void onProgressUpdate(int progress); } private static MusicPlayer sMusicPlayer; private List<Music> mPlayList; // 播放列表 private boolean mPlaying; // 是否正在播放 private int mCurrentIndex; // 目前播放的位置 private int mCurrentProgress; // 播放进度 private MediaPlayer mMediaPlayer; // 播放器 private PlayerListener mListener; // 监听器 private Timer mTimer; // 计时器 private int mTotalTime; // 播放时间 public static MusicPlayer getInstance(){...} public void play(final int index) {...} public void pause() {...} public void resume() {...} public void playNext() {...} public void playPrev() {...}
To notify the activity when the player's internal state changes, I define a playerlistener
Views
The view is an XML file, nothing to say.
Activity
The activity contains the view object and the Player object.
First get the songs from the phone through Contentresolver,
Then put it in the player and put it in the adapter of the playlist.
mMusicPlayer = MusicPlayer.getInstance();mMusicPlayer.setPlayList(getPlayList());mMusicPlayer.setListener(getPlayerListener());mMusicListAdapter = new MusicListAdapter(this, getPlayList());mMusicListView.setAdapter(mMusicListAdapter);
When you click on the list, tell the player to play the music.
mMusicListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mMusicPlayer.play(position); }});
When the player state changes, the onPlay
method is called to notify the activity to update the view:
public MusicPlayer.PlayerListener getPlayerListener() { if (mPlayerListener == null) { mPlayerListener = new MusicPlayer.PlayerListener() { @Override public void onPlay() { updateUI(); } @Override public void onPause() { updateUI(); } @Override public void onResume() { updateUI(); } @Override public void onPlayNext() { updateUI(); } @Override public void onPlayPrev() { updateUI(); } @Override public void onProgressUpdate(int progress) { updateUI(); } }; } return mPlayerListener;}
The whole process is: View issue Action,controller Change Model,model notify Controller state Change, controller update view.
Summary
The MVC pattern allows us to design the software better, with a clear division of responsibilities across parts. Model and view are relatively independent and easy to change. For example, I do not want to play music in the activity, I can put musicplayer on the service to play, and the playback code does not have to change.
Android MVC implements a music player