Android lyrics show design ideas (7) coming soon

Source: Internet
Author: User

We used six articles to pave the way and finally reached the real application. This part is as simple as ever.
There are two classes for the Application: LiryicMain and SelectFileActivity. It is almost the lowest level of content, and there is no gorgeous content.
Let's take a look at the positions of these two classes in the entire software. It can be seen that LyricMain is the control of the entire software. SelectFileActivity also provides services for LyricMain.

SelectFileActivity is too simple and will not be described in this article. Let's take a look at LyricMain.
First, data members. One is LyricPlayerServiceProxy, the proxy of the lyrics play service, and the other is a List used to save the end position of the lyrics.
Private LyricPlayerServiceProxy mProxy = new LyricPlayerServiceProxy (this );
Private ArrayList <Integer> mLyricEndList = new ArrayList <Integer> ();
LyricPlayerServiceProxy is the content that has been introduced earlier. It will not be repeated here. MLyricEndList needs to be described. In this software, all the lyrics are represented in a TextEditView. To be able to represent the lyrics in the current play, we save the location of each lyrics in mLyricEndList, in this way, when the lyrics in the video play change, you only need to set the lyrics to the selected state.
Next, the simplest implementation of LyricMediaInfoProvider provides fixed song names and location information of song files. If you need to switch the song, you need to be more complicated.
Private class LyricMediaInfoProvider implements MediaPlayerService. MediaInfoProvider {
String mUrl;
String mTitle;

LyricMediaInfoProvider (String url, String title ){
MUrl = url;
MTitle = title;
}

@ Override
Public boolean moveToPrev (){
// TODO Auto-generated method stub
Return false;
}

@ Override
Public boolean moveToNext (){
// TODO Auto-generated method stub
Return false;
}

@ Override
Public String getUrl (){
Return mUrl;
}

@ Override
Public String getTitle (){
// TODO Auto-generated method stub
Return mTitle;
}
}
Next is the onCreate method. I mainly did a few things
1. Establish a connection with LyricPlayerServiceProxy.
2. Provided NotificationProvider implementation (For details, refer to: Android lyrics show design ideas (4) General music play service (II ))
3. Set the size of ImageButton.
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

MLyricEdit = (EditText) this. findViewById (R. id. editLyric );

MProxy. setConnectionListener (this );
MProxy. setLyricPlayerListener (this );
MProxy. setNotificationProvider (new MediaPlayerService. icationicationprovider (){
@ Override
Public Notification createNotification (Context context ){
Notification notification = new Notification (R. drawable. button_blue_play, mProxy. getTitle (), System. currentTimeMillis ());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent. getActivity (context, 0, new Intent (context, LyricMain. class), 0 );
// Set the info for the views that show in the notification panel.
Notification. setLatestEventInfo (context, getText (R. string. media_player_label), mProxy. getTitle (), contentIntent );
Return notification;
}
});
MProxy. startAndBindService ();
MLyricEndList. clear ();

DisplayMetrics metrics = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (metrics );

Int btnId [] = {R. id. buttonPrev, R. id. buttonStop, R. id. buttonPlay, R. id. buttonPause, R. id. buttonNext };
Int btnSize = Math. min (metrics. widthPixels, metrics. heightPixels)/(btnId. length + 1 );

// Adjust the key size.
For (int I = 0; I <btnId. length; ++ I ){
ImageButton ib = (ImageButton) this. findViewById (btnId [I]);
Ib. setAdjustViewBounds (true );
Ib. setMaxHeight (btnSize );
Ib. setMaxWidth (btnSize );
}
ImageButton selectFile = (ImageButton) this. findViewById (R. id. buttonSelectFile );
SelectFile. setAdjustViewBounds (true );
SelectFile. setMaxHeight (btnSize * 2/3 );
SelectFile. setMaxWidth (btnSize * 2/3 );

UpdateButtonState ();
}
Next, the onDestroy method is used. If the music is playing, it is connected to the playing service and exits the program. This means that the playing of the song will continue. If the playback is stopped or paused, it is closed together with the playback service and the program is completely exited.
@ Override
Protected void onDestroy (){
Super. onDestroy ();
MProxy. setConnectionListener (null );
MProxy. setLyricPlayerListener (null );
If (! MProxy. isPlaying ()){
MProxy. stopService ();
}
}
Start SelectFileActivity of the selected file
Public void OnSelectFile (View v ){
Intent I = new Intent (this, SelectFileActivity. class );
StartActivityForResult (I, 0 );
}
SelectFileActivity is disabled. obtain information about the selected media file and notify LyricPlayerServiceProxy.
The next step is button processing.
Public void OnOperationButtonClick (View v ){
Switch (v. getId ()){
Case R. id. buttonPrev:
MProxy. seekToPrevLyric ();
Break;
Case R. id. buttonStop:
If (mProxy. isPlaying () | mProxy. isPausing ()){
MProxy. stop ();
}
Break;
Case R. id. buttonPlay:
If (! MProxy. isPlaying ()){
MProxy. start ();
}
Break;
Case R. id. buttonPause:
If (mProxy. isPlaying ()){
MProxy. pause ();
}
Break;
Case R. id. buttonNext:
MProxy. seekToNextLyric ();
Break;
}
}
Updates the status of each button Based on the playback status.
Protected void updateButtonState (){
(ImageButton) this. findViewById (R. id. buttonPrev). setEnabled (mProxy. isPlaying () | mProxy. isPausing ());
(ImageButton) this. findViewById (R. id. buttonStop). setEnabled (mProxy. isPlaying () | mProxy. isPausing ());
(ImageButton) this. findViewById (R. id. buttonPlay). setEnabled (mProxy. getDataSource ()! = Null &&(! MProxy. isPlaying () | mProxy. isPausing ()));
(ImageButton) this. findViewById (R. id. buttonPause). setEnabled (mProxy. isPlaying ());
(ImageButton) this. findViewById (R. id. buttonNext). setEnabled (mProxy. isPlaying () | mProxy. isPausing ());
}
If a song is already playing when the program starts, update the file title and button status.
// Implement of LyricPlayerServiceProxy. ServiceConnectionListener
Public void onServiceConnected (){
String title = mProxy. getTitle ();
If (title! = Null ){
TextView TV = (TextView) this. findViewById (R. id. fileTitle );
TV. setText (title );
}
UpdateButtonState ();
}
Public void onServiceDisconnected (){

}
Implements the LyricPlayerListener code and processes various notifications of the lyrics play service.
// Implement of LyricPlayerService. LyricPlayerListener
Public void onLyricLoaded (){
MLyricEndList. clear ();
String lyric = new String ();
For (int I = 0; I <mProxy. getLyricCount (); ++ I ){
Lyric + = mProxy. getLyric (I );
Lyric + = "\ r \ n ";
MLyricEndList. add (new Integer (lyric. length ()));
}
MLyricEdit. setText (lyric );
}

Public void onStateChanged (){
UpdateButtonState ();
}

Public void onPositionChanged (long position ){

}

Public void onLyricChanged (int lyric_index ){
Int lyricStart = 0;
If (lyric_index> 0 ){
LyricStart = mLyricEndList. get (lyric_index-1 );
}
Int lyricEnd = mLyricEndList. get (lyric_index );
MLyricEdit. setSelection (lyricStart, lyricEnd );
MLyricEdit. invalidate ();
Log. I (TAG, String. format ("lyric = % d, setSelection (% d, % d)", lyric_index, lyricStart, lyricEnd ));
}
When you read the lyrics, train them into a long string and remember the position of each lyrics in the string.
When the status of the playback service changes, update the status of the button.
When the current lyrics change, the current lyrics are highlighted based on the previously saved location information.
Finally, it is just as simple as jumping to the code of the selected lyrics.
Public void OnLyricClick (View v ){
EditText et = (EditText) v;
Int sel_start = et. getSelectionStart ();
For (int I = 0; I <mLyricEndList. size (); ++ I ){
If (sel_start <mLyricEndList. get (I ))
{
MProxy. seekToLyric (I );
Break;
}
}
}
In combination with the selected location and saved lyrics location information, find the number of the lyrics and let the playing service jump to that sentence.
Author: "From Dalian"

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.