LyricView Documentation

repository·master·Indexed 20 days ago

https://github.com/asr-pub/lyricviewdemo

An Android custom view for displaying lyrics in music players. LyricView supports automatic charset detection via juniversalchardet-1.0.3, text highlighting, and user interaction callbacks. It provides a Java API for controlling lyric progress and XML attributes for customizing text size, colors, alignment, and fade effects.

Tokens
1.1K
Snippets
2
Records
4
Agent score
22%

What's inside LyricView

  1. Install LyricView via Gradle

    master

    To use LyricView in your Android project, follow these two steps:

    1. Add the JitPack repository to your allprojects block in your build file.
    2. Add the LyricView dependency to your dependencies block.

    Note: The dependency uses the compile configuration.

    // step 1: Add JitPack repository
    allprojects {
        repositories {
            ... 
            maven { url 'https://jitpack.io' }
        }
    }
    
    // step 2: Add the dependency
    dependencies {
        compile 'com.github.zhengken:LyricViewDemo:v1.2'
    }
  2. Implement LyricView in Layout and Java

    master

    To display lyrics, add the me.zhengken.lyricview.LyricView component to your XML layout and control it via Java code.

    1. XML Layout

    Add the view to your layout file:

    <me.zhengken.lyricview.LyricView
        android:id="@+id/custom_lyric_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    2. Java Implementation

    Initialize the view, load a lyric file, and update the progress periodically:

    LyricView mLyricView = (LyricView)findViewById(R.id.custom_lyric_view);
    
    // Set the lyric file
    mLyricView.setLyricFile(lyricFile);
    
    // Update the view every interval (e.g., from your music player progress)
    mLyricView.setCurrentTimeMillis(progress);
    
    // Handle user interaction (dragging lyrics or clicking play icon)
    mLyricView.setOnPlayerClickListener(new LyricView.OnPlayerClickListener() {
        @Override
        public void onPlayerClicked(long progress, String content) {
            // Handle the click event with the new progress and content
        }
    });
    LyricView mLyricView = (LyricView)findViewById(R.id.custom_lyric_view);
    
    // step 3
    mLyricView.setLyricFile(lyricFile);
    
    // step 4, update LyricView every interval
    mLyricView.setCurrentTimeMillis(progress);
    
    // step 5, implement the interface when user drag lyrics and click the play icon
    mLyricView.setOnPlayerClickListener(new LyricView.OnPlayerClickListener() {
        @Override
        public void onPlayerClicked(long progress, String content) {
            
        }
    });
  3. LyricView Java API Reference

    master

    The following methods are available on the LyricView instance to control its state and behavior:

    MethodDescription
    setOnPlayerClickListener(OnPlayerClickListener listener)Callback when click the play icon
    setAlignment(@Alignment int alignment)Set the alignment of the lyrics
    setCurrentTimeMillis(long current)Scroll lyrics to the specify TimeMillis
    setLyricFile(File file)Set the lyric file, and auto set the charset by juniversalchardet-1.0.3
    setLyricFile(File file, String charset)Set the lyric file with the specified charset
    setTypeface(Typeface typeface)Set the typeface of lyrics
    reset()Reset the LyricView
  4. Configure LyricView XML attributes

    master

    You can customize the appearance and behavior of LyricView directly in your XML layout using the following attributes:

    AttributeFormatDefaultDescription
    fadeInFadeOutbooleanfalseEnable lyrics fadeInFadeOut or not
    hintstringNo LyricsDisplay when not exist lyric file
    hintColorcolor#FFFFFFThe color of hint text
    textSizedimension16spThe text size of lyrics
    textColorcolor#8D8D8DThe color of lyrics
    highlightColorcolor#FFFFFFThe color of current lyric that playing
    textAlignenumCENTERThe alignment of lyrics
    maxLengthdimension300dpLine feed when lyric'width beyond maxLength
    lineSpacedimension25dpLine space