Android lyrics show design ideas (5) lyrics play service

Source: Internet
Author: User

The following describes the LyricPlayerService that provides the lyrics player service. The role of this class in the whole playing process is
1. manages the lifecycle of the LyricAdapter.
2. Control the playing of music, parse the lyrics, and coordinate the synchronization of music and lyrics.
3. Collect superclasses and send notifications from LyricAdapter to the logged-on LyricAdapterListener.
First, let's take a look at the position of this class in the whole graph.

 
We can clearly see that this class is in the node position of the entire graph. From this point, we can understand the importance of this class.
There are not many codes. Here is a brief description.
First, it defines the interface used to receive status change notifications from LyricPlayerService and provides a method to specify a concrete interface.
During the playing of a song, the application continues to exit LyricPlayerService. At this time, if you restart the player application, you will have a re-connection, that is, the scenario where LyricPlayerService specified the interface in the playback. In the design of LyricPlayerService, The onLyricLoad and onLyricChanged methods of LyricPlayerListener are re-called. In this way, the implementation of the application can be simplified.
// Define an interface for changing the player status
Public interface LyricPlayerListener {
// The playing position changes.
Public void onPositionChanged (long position );
// The player status changes.
Public void onStateChanged ();
// Current lyrics change
Public void onLyricChanged (int lyric_index );
// The lyrics file is parsed.
Public void onLyricLoaded ();
}

Private LyricPlayerListener mLyricPlayerListener = null;

Void setLyricPlayerListener (LyricPlayerListener listener ){
MLyricPlayerListener = listener;
If (mLyricPlayerListener! = Null)
{
// The newly set Listener directly calls the onLyricLoad method, giving a chance to prepare the table content.
MLyricPlayerListener. onLyricLoaded ();
Int curLyric = mLyricAdapter. getCurrentLyric ();
If (curLyric> = 0 & curLyric <mLyricAdapter. getLyricCount ()){
// If it is already playing, notify the Listener of the current lyrics
MLyricPlayerListener. onLyricChanged (curLyric );
}
}
}
Create a LyricAdapter instance
Private LyricAdapter mLyricAdapter = new LyricAdapter ();
Create an instance of SafetyTimer and set the action. In the OnTimer event, first obtain the current playing position of the MediaPlayer and notify LyricAdapter and the logged-on mLyricPlayerListener of this information. The content is very simple, but here is the most important part of the normal operation of LyricPlayerService. In addition to the play part of the song we cannot see, this is almost half of the main program.
Private SafetyTimer mLyricTimer = new SafetyTimer (500, new SafetyTimer. OnTimeListener (){
Public void OnTimer (){
If (mMediaPlayer! = Null ){
Int position = mMediaPlayer. getCurrentPosition ();
If (mLyricPlayerListener! = Null ){
MLyricPlayerListener. onPositionChanged (position );
}
MLyricAdapter. policytime (position );
}
}
});
OnCreate and onDestroy are nothing special. Of course, in onDestroy, you can also call mLyricAdapter. setListener (null); there is no difference.
@ Override
Public void onCreate (){
Super. onCreate ();
MLyricAdapter. setListener (this );
}
 
@ Override
Public void onDestroy (){
Super. onDestroy ();
MLyricAdapter = null;
}
The following describes how to associate playback control. Mainly do the following:
1. Call the superclass function.
2. Control the action of SafetyTimer.
3. corresponding functions of Listener are used.
 
@ Override
Public void start (){
If (isStop ()){
// The stop status starts and Timer needs to be restarted.
Super. start ();
MLyricTimer. startTimer ();
} Else {
// The pause status starts and Timer does not need to be restarted.
Super. start ();
}
If (mLyricPlayerListener! = Null ){
MLyricPlayerListener. onStateChanged ();
}
}

@ Override
Public void stop (){
MLyricTimer. stopTimer ();
Super. stop ();
If (mLyricPlayerListener! = Null ){
MLyricPlayerListener. onStateChanged ();
}
}

@ Override
Public void pause (){
Super. pause ();
If (mLyricPlayerListener! = Null ){
MLyricPlayerListener. onStateChanged ();
}
}
 
The following two methods cover the corresponding super-class method, and change the function originally used to play the songs before and after into the lyrics in the same song.
@ Override
Public void playNext (){
SeekToNextLyric ();
}

@ Override
Public void playPrev (){
SeekToPrevLyric ();
}
The following describes how to use the lyrics. The LyricAdapter and the super class location control functions are basically used.
Public void loadLyric (){
String url = getDataSource ();
// Files with the same file name and lrc extension in the same directory as the song
String strLyricFileUrl = url. substring (0, url. lastIndexOf (".") + 1) + "lrc ";
MLyricAdapter. LoadLyric (strLyricFileUrl );
}

Public int getLyricCount (){
Return mLyricAdapter. getLyricCount ();
}

Public String getLyric (int index ){
Return mLyricAdapter. getLyric (index );
}

Public long seekToLyric (int index ){
Long position = mLyricAdapter. getLyricTime (index );
If (position! =-1 ){
Return seek (position );
} Else {
Return 0;
}
}

Public void seekToPrevLyric (){
Int curLyric = mLyricAdapter. getCurrentLyric ();
If (curLyric> 0 ){
SeekToLyric (curLyric-1 );
}
}

Public void seekToNextLyric (){
Int curLyric = mLyricAdapter. getCurrentLyric ();
If (curLyric <mLyricAdapter. getLyricCount ()-1 ){
SeekToLyric (curLyric + 1 );
}
}
There is not much left. The onCompletion overload is to disable Timer at this time and notify mLyricPlayerListener of this change.
@ Override
Public void onCompletion (MediaPlayer mp ){
If (mLyricTimer. isRunging ()){
MLyricTimer. stopTimer ();
}
If (mLyricPlayerListener! = Null ){
MLyricPlayerListener. onStateChanged ();
}
Super. onCompletion (mp );
}
The last step is to inherit the two methods required by LyricAdapter. LyricListener. Send a notification from LyricAdapter to the logged-on mLyricPlayerLister. The reason for doing so is to make it easier for the caller to use it.
@ Override
Public void onLyricChanged (int lyric_index ){
If (mLyricPlayerListener! = Null ){
MLyricPlayerListener. onLyricChanged (lyric_index );
}
}

@ Override
Public void onLyricLoaded (){
If (mLyricPlayerListener! = Null ){
MLyricPlayerListener. onLyricLoaded ();
MLyricPlayerListener. onStateChanged ();
}
}
Software Function Description: Original: Android Application Development-Andorid lyrics show, with source code
Project, source code download: Android lyrics show source code, project file 2011/9/11
Author: "From Dalian"

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.

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.