Giter Site home page Giter Site logo

lyricview's Introduction

LyricView

在音乐播放器项目中使用到酷狗音乐展示歌词的效果,需要支持修改字体、颜色、渐变色等功能

截图如下(截图效果不是很好,请下载Demo查看)

使用方式:

<com.zdc.lyricdemo.view.LyricView
		android:id="@+id/lvView"
		android:layout_width="match_parent"
		android:layout_height="420dp"
		android:background="@drawable/base_bg" />

然后在代码中初始化MediaPlayer、歌词、设置监听:

public class TtActivity extends Activity implements OnClickListener {

	private Button btnPlayPause;
	private LyricView lvView;
	private MediaPlayer mediaPlayer = new MediaPlayer();
	private Handler handler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initMediaPlayer();
	}

	private void initView() {
		btnPlayPause = (Button) findViewById(R.id.btn_play_pause);
		lvView = (LyricView) findViewById(R.id.lvView);
		btnPlayPause.setOnClickListener(this);
	}

	private void initMediaPlayer() {
		AssetManager am = getAssets();
		try {
			mediaPlayer.reset();
			mediaPlayer.setDataSource(am.openFd("cbg.mp3").getFileDescriptor());
			mediaPlayer.prepare();
		} catch (IOException e) {
			e.printStackTrace();
		}
		lvView.initLyricFile(LyricLoader.loadLyricFile("cbg"));
	}

	@Override
	public void onClick(View view) {
		if (!mediaPlayer.isPlaying()) {
			mediaPlayer.start();
			handler.post(runnable);
		} else {
			mediaPlayer.pause();
			handler.removeCallbacks(runnable);
		}
	}

	private Runnable runnable = new Runnable() {
		@Override
		public void run() {
			if (mediaPlayer.isPlaying()) {
				long time = mediaPlayer.getCurrentPosition();
				lvView.updateLyrics((int) time, mediaPlayer.getDuration());
			}
			handler.postDelayed(this, 100);
		}
	};

	@Override
	protected void onDestroy() {
		handler.removeCallbacks(runnable);
		mediaPlayer.reset();
		mediaPlayer.release();
		mediaPlayer = null;
		super.onDestroy();
	}
}	

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.