android-Music Player Implementation and source download (i)

Source: Internet
Author: User

Tags: music player service Jsoup

Starting from this article, the implementation of a music player is described in detail, as well as the ability to get the latest recommended songs and download songs from the network parsing data.
Features are described below:
1. Get the local song list and play the song function.
2, using hardware acceleration sensor, shake the phone to achieve the function of switching songs
3. Use Jsoup to parse webpage data, get the song list from the network, and realize the function of downloading songs and lyrics to the local phone.
4, notification bar reminder, to realize the QQ music player's notification bar function.
The technologies involved are:
1, Jsoup parsing Web pages, so as to obtain the required data
2, Android access to the network, get files to local network request technology, and download files to local implementation breakpoint download
3. Thread pool
4. Picture Cache
5, service has been running in the background
6, Mobile phone hardware accelerator
7, Notification notice bar design
8. Custom Broadcast
9, Android System file management
The main techniques are these, which, using Jsoup to parse Web pages to get the data you need, please refer to my blog: How to parse Web page data in Android using Jsoup detail

Here is the final result display:

Figure A local song list


Figure Two Network songs list


Figure three play the song interface


Figure Four Playback Interface lyrics Display section


Figure five notification bar shows the display when playing and downloading

In the local song list interface, long press the menu key to display the menu:

In the Network songs list, click the song to display the download and cancel menu items:

The basic interface is shown in the diagram above. If you think you can, just leave a message ~ ~ ^_^ "Shake Hands"

The implementation details are described in detail below. First of all, please refer to my blog: How to parse Web page data in Android using Jsoup in the online song section of the player
The network song list obtains is uses the Jsoup from the Web page to fetch the data to display. This part of the stuff is not explained in detail in this series of posts.

About the music player section, need to use to service services, a total of two, one is music playback service, one is the song download service, about the service has been running in the background problems, Please refer to my blog: implementation of the music player background service has always been the solution of the idea

According to this blog post, it is easy to design the implementation of the application class, the application class implementation code is as follows:

/** * August 15, 2015 16:34:37 * Blog Address: http://blog.csdn.net/u010156024 */ Public  class App extends application {     Public StaticContext Scontext; Public Static intSscreenwidth; Public Static intSscreenheight;@Override     Public void onCreate() {Super. OnCreate ();        Scontext = Getapplicationcontext (); StartService (NewIntent ( This, Playservice.class)); StartService (NewIntent ( This, Downloadservice.class));        WindowManager wm = (WindowManager) getsystemservice (Context.window_service); Displaymetrics DM =NewDisplaymetrics ();        Wm.getdefaultdisplay (). Getmetrics (DM);        Sscreenwidth = Dm.widthpixels;    Sscreenheight = Dm.heightpixels; }}

The above two service services are started in the application class, while the screen width and height of the phone are obtained. Mobile phone width and height is used for the rear I-side design interface.
With application, you can design the activity class, first design the Baseactivity class, put the reused code in the base class, the subclass overrides or overrides the method in Baseactivity, the code is as follows:

/** * August 15, 2015 16:34:37 * Blog Address: http://blog.csdn.net/u010156024 */ Public Abstract  class baseactivity extends fragmentactivity {    protectedPlayservice Mplayservice;protectedDownloadservice Mdownloadservice;Private FinalString TAG = BaseActivity.class.getSimpleName ();PrivateServiceconnection mplayserviceconnection =NewServiceconnection () {@Override         Public void onservicedisconnected(componentname name) {L.L (TAG,"Play--->onservicedisconnected"); Mplayservice =NULL; }@Override         Public void onserviceconnected(componentname name, IBinder service)            {Mplayservice = ((playservice.playbinder) service). GetService ();            Mplayservice.setonmusiceventlistener (Mmusiceventlistener);        OnChange (Mplayservice.getplayingposition ()); }    };PrivateServiceconnection mdownloadserviceconnection =NewServiceconnection () {@Override         Public void onservicedisconnected(componentname name) {L.L (TAG,"Download--->onservicedisconnected"); Mdownloadservice =NULL; }@Override         Public void onserviceconnected(componentname name, IBinder service)        {Mdownloadservice = ((downloadservice.downloadbinder) service). GetService (); }    };/** * Music Playback Service Callback interface implementation Class */    PrivatePlayservice.onmusiceventlistener Mmusiceventlistener =NewPlayservice.onmusiceventlistener () {@Override         Public void Onpublish(intProgress) {baseactivity. This. Onpublish (progress); }@Override         Public void OnChange(intPosition) {baseactivity. This. OnChange (position); }    };/** * Fragment after the view load is complete callback * * NOTE: * Allowbindservice () Start the song using a binding service * Allowunbindservice () method solution     In addition to binding * * Use the StartService () method in Splashactivity.java to start the Music playback service * Then everyone needs to be aware that the service does not unbind by calling the Allowunbindservice () method     * and stop. */     Public void Allowbindservice() {Getapplicationcontext (). Bindservice (NewIntent ( This, Playservice.class), mplayserviceconnection, context.bind_auto_create); }/** * Fragment's view disappears after the callback */     Public void Allowunbindservice() {Getapplicationcontext (). Unbindservice (mplayserviceconnection); }@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);//Binding set -up serviceBindservice (NewIntent ( This, Downloadservice.class), mdownloadserviceconnection, context.bind_auto_create); }@Override    protected void OnDestroy() {unbindservice (mdownloadserviceconnection);Super. OnDestroy (); } PublicDownloadserviceGetdownloadservice() {returnMdownloadservice; }/** * Update Progress * Abstract method implemented by subclasses * Implement service and main interface communication * @param Progress Progress * *     Public Abstract void Onpublish(intProgress);/** * Toggle Song * Abstract method implemented by subclasses * Implement service and main interface communication * @param The location of the position song in the list */     Public Abstract void OnChange(intposition);}

The following gives the start interface, the start interface is very simple, delay 2 seconds into the main interface, the code is as follows:

/** * August 15, 2015 16:34:37 * Blog Address: http://blog.csdn.net/u010156024 */ Public  class splashactivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);//No titleRequestwindowfeature (Window.feature_no_title);//FullscreenGetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN        ); Setcontentview (r.layout.splash_layout);//2s jump to main interface        NewHandler (). postdelayed (NewRunnable () {@Override             Public void Run() {startactivity (NewIntent (splashactivity. This, Mainactivity.class));            Finish (); }        }, -); }}

The above is a preparatory work, with the above preparation, you can design the main interface and the host surface of the two fragment. The following blog post describes the implementation of the main interface.

Music player Source code download

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

android-Music Player Implementation and source download (i)

Related Article

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.

Tags Index: