当前位置:操作系统 > 安卓/Android >>

Android 实现歌曲播放时歌词同步显示

我们需要读取以上歌词文件的每一行转换成成一个个歌词实体:


[java]  public class LyricObject {   
    public int begintime; // 开始时间    
    public int endtime; // 结束时间    
    public int timeline; // 单句歌词用时    
    public String lrc; // 单句歌词    
}   

public class LyricObject { 
    public int begintime; // 开始时间 
    public int endtime; // 结束时间 
    public int timeline; // 单句歌词用时 
    public String lrc; // 单句歌词 

可根据当前播放器的播放进度与每句歌词的开始时间,得到当前屏幕中央高亮显示的那句歌词。在UI线程中另起线程,通过回调函数 onDraw() 每隔100ms重新绘制屏幕,实现歌词平滑滚动的动画效果。MainActivity代码如下:

 

[java]   import java.io.IOException;   
import android.app.Activity;   
import android.media.MediaPlayer;   
import android.net.Uri;   
import android.os.Bundle;   
import android.os.Environment;   
import android.os.Handler;   
import android.view.View;   
import android.view.View.OnClickListener;   
import android.widget.Button;   
import android.widget.SeekBar;   
import android.widget.SeekBar.OnSeekBarChangeListener;   
   
public class MainActivity extends Activity {   
    /** Called when the activity is first created. */   
    private LyricView lyricView;   
    private MediaPlayer mediaPlayer;   
    private Button button;   
    private SeekBar seekBar;   
    private String mp3Path;   
    private int INTERVAL=45;//歌词每行的间隔    
   
    @Override   
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        // this.requestWindowFeature(Window.FEATURE_NO_TITLE);    
        // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);    
        setContentView(R.layout.main);   
   
        mp3Path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/LyricSync/1.mp3";   
   
        lyricView = (LyricView) findViewById(R.id.mylrc);   
        mediaPlayer = new MediaPlayer();   
        // this.requestWindowFeature(Window.FEATURE_NO_TITLE);    
   
        ResetMusic(mp3Path);   
        SerchLrc();   
        lyricView.SetTextSize();   
   
        button = (Button) findViewById(R.id.button);   
        button.setText("播放");   
   
        seekBar = (SeekBar) findViewById(R.id.seekbarmusic);   
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {   
   
            @Override   
            public void onStopTrackingTouch(SeekBar seekBar) {   
                // TODO Auto-generated method stub    
   
            }   
   
            @Override   
            public void onStartTrackingTouch(SeekBar seekBar) {   
                // TODO Auto-generated method stub    
   
            }   
   
            @Override   
            public void onProgressChanged(SeekBar seekBar, int progress,   
                    boolean fromUser) {   
                // TODO Auto-generated method stub    
                if (fromUser) {   
                    mediaPlayer.seekTo(progress);   
                    lyricView.setOffsetY(220 - lyricView.SelectIndex(progress)    
                            * (lyricView.getSIZEWORD() + INTERVAL-1));   
   
                }   
            }   
        });   
   
        button.setOnClickListener(new OnClickListener() {   
   
            @Override   
            public void onClick(View v) {   
                // TODO Auto-generated method stub    
                if (mediaPlayer.isPlaying()) {   
                    button.setText("播放");   
           

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,