How to write a serious Android music player three

Source: Internet
Author: User

To achieve music playback.

In order to quickly achieve music playback, we use MediaPlayer instead of Soundpool, which is said to soundpool more freedom than MediaPlayer. However, according to my understanding, Soundpool does not support audio decoding, have to decode themselves, and Meidaplayer has built up some decoding scheme, I see, audio in addition to the Ape lossless format, can play.

You need to read:

MediaPlayer API:

Http://developer.android.com/reference/android/media/MediaPlayer.html

MediaPlayer API Guide:

Http://developer.android.com/guide/topics/media/mediaplayer.html

In this case, the use of MediaPlayer is no longer introduced.


On the way, we can see the life cycle of the MediaPlayer, in which the blue ellipse is a different state of MediaPlayer.

An exception occurs if you perform some actions that you do not want in the disallowed state. For different operations, the legal preconditions and the illegal status, refer to this link:

Http://developer.android.com/reference/android/media/MediaPlayer.html#Valid_and_Invalid_States

We declare a class to record a state.

public static class state {public        static final int                IDLE = 0,                INITIALIZED = 1,                PREPARED = 2,                PREPARING = 3,                STARTED = 4,                PAUSED = 5,                STOPPED = 6,                completed = 7,                END =-1,                ERROR =-2;    }

in our mainservice, you can write the code that starts playing:

This method is used to initialize our mediaplayerprivate void init () {        if (mPlayer = = = null) {            MPlayer = new MediaPlayer ();            Changestate (State.idle);        } else {            if (mcurrentstate = = State.idle | | mcurrentstate = state.initialized | | mcurrentstate = state.prepared | |                    mcurrentstate = = State.started | | mcurrentstate = = State.paused | | mcurrentstate = state.stopped | |                    Mcurrentstate = = State.completed | | Mcurrentstate = = state.error) {                mplayer.reset ();                Changestate (State.idle);//Note Status change code            }        }        Mplayer.setonerrorlistener (this);//mainservice to implement these three interfaces        Mplayer.setonpreparedlistener (this);        Mplayer.setoncompletionlistener (this);    }

A way to start broadcasting openly:

public void        Start (audio audio) {init ();    try {if (mcurrentstate = = State.idle) {Mplayer.setdatasource (this, Audio.geturi ());            Valid Sates IDLE} changestate (state.initialized);      if (mcurrentstate! = state.error) {mplayer.setaudiostreamtype (audiomanager.stream_music);                 Invalid states ERROR} if (mcurrentstate = = State.initialized | | mcurrentstate = state.stopped) {            Mplayer.prepareasync ();//valid sates{initialized, Stopped} changestate (state.preparing);        }} catch (IOException e) {e.printstacktrace (); }    }

Note that in this method, we do not go directly to play, but instead use Mplayer.prepareasync (). Let MediaPlayer to do the application play resources, and so on, this process may have dozens of milliseconds of time, wait until the callback Onprepared callback succeeds, in onprepared to do a real start, we call the Dostart method in onprepared:

private void Dostart () {        mplayer.start ();        Changestate (state.started);    }

So you can start playing the job. In the above code, there is a changestate method. As follows:

private void Changestate (int state) {        mcurrentstate = state;        if (Mplaybacklistener! = null) {            mplaybacklistener.onstatechanged (Mcurrentaudio, mcurrentstate);}    } /* Here is a Setonplaybacklistener method, if necessary, you can also use a list to save a listener collection, as long as the appropriate time to release, such as in the service OnDestroy method, Go and empty the list */public void Setonplaybacklistener (Onplaybacklistener listener) {        Mplaybacklistener = listener;    } Public    Static interface Onplaybacklistener {public        void onstatechanged (Audio source, int.);    }



How to write a serious Android music player three

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.