android-Music Player Implementation and source download (i)

Source: Internet
Author: User



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 post address: http://blog.csdn.net/u010156024
  * /
public class App extends Application {
     public static Context sContext;
     public static int sScreenWidth;
     public static int sScreenHeight;

     @Override
     public void onCreate () {
         super.onCreate ();
         sContext = getApplicationContext ();

         startService (new Intent (this, PlayService.class));
         startService (new Intent (this, DownloadService.class));

         WindowManager wm = (WindowManager) getSystemService (Context.WINDOW_SERVICE);
         DisplayMetrics dm = new DisplayMetrics ();
         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 post address: http://blog.csdn.net/u010156024
 * /
public abstract class BaseActivity extends FragmentActivity {
    protected PlayService mPlayService;
    protected DownloadService mDownloadService;
    private final String TAG = BaseActivity.class.getSimpleName ();

    private ServiceConnection mPlayServiceConnection = new ServiceConnection () {
        @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 ());
        }
    };

    private ServiceConnection mDownloadServiceConnection = new ServiceConnection () {
        @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 ();
        }
    };

    / **
     * Implementation class of music playback service callback interface
     * /
    private PlayService.OnMusicEventListener mMusicEventListener =
            new PlayService.OnMusicEventListener () {
        @Override
        public void onPublish (int progress) {
            BaseActivity.this.onPublish (progress);
        }

        @Override
        public void onChange (int position) {
            BaseActivity.this.onChange (position);
        }
    };

    / **
     * Callback after Fragment view is loaded
     *
     * Note:
     * allowBindService () uses the bound method to start the song playback service
     * allowUnbindService () method to unbind
     *
     * Started the music player service using startService () method in SplashActivity.java
     * Then you need to pay attention, the service will not be unbound by calling allowUnbindService () method
     * And stop.
     * /
    public void allowBindService () {
        getApplicationContext (). bindService (new Intent (this, PlayService.class),
                mPlayServiceConnection,
                Context.BIND_AUTO_CREATE);
    }

    / **
     * Callback after fragment view disappears
     * /
    public void allowUnbindService () {
        getApplicationContext (). unbindService (mPlayServiceConnection);
    }

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        // Binding download service
        bindService (new Intent (this, DownloadService.class),
                mDownloadServiceConnection,
                Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy () {
        unbindService (mDownloadServiceConnection);
        super.onDestroy ();
    }

    public DownloadService getDownloadService () {
        return mDownloadService;
    }

    / **
     * Update progress
     * Abstract methods are implemented by subclasses
     * Realize communication between service and main interface
     * @param progress
     * /
    public abstract void onPublish (int progress);
    / **
     * Switch songs
     * Abstract methods are implemented by subclasses
     * Realize communication between service and main interface
     * @param position The position of the song in the list
     * /
    public abstract void onChange (int position);
} 


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 post address: http://blog.csdn.net/u010156024
  * /
public class SplashActivity extends Activity {
     @Override
     protected void onCreate (Bundle savedInstanceState) {
         super.onCreate (savedInstanceState);
         // no title
         requestWindowFeature (Window.FEATURE_NO_TITLE);
         // full screen
         getWindow (). setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
         setContentView (R.layout.splash_layout);

         // 2s jump to the main interface
         new Handler (). postDelayed (new Runnable () {
             @Override
             public void run () {
                 startActivity (new Intent (SplashActivity.this, MainActivity.class));
                 finish ();
             }
         }, 2000);
     }
} 


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)


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.