Android MVC implements a music player

Source: Internet
Author: User

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

    1. Controller controllers-responsible for forwarding requests and processing requests.
    2. Views View-Interface designer for graphical interface design.
    3. 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
    1. The activity is responsible for handling view events and getting model data and refreshing the view.
    2. View is only responsible for displaying
    3. Model is the data part, cannot update view, data change must update view through activity.
Design our playerModels
    1. Music-Represents a song, song name, path, singer and other information
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.