How to Implement audio lyrics synchronization in Android (1)

Source: Internet
Author: User

Recently, the project needs to use the audio and lyrics synchronization function. There is a single small function on the Internet, for example, the lyrics function for separately reading LRC files, mediaplayer and seekbar. I have not combined the methods, and the positioning and time point positioning are relatively accurate. Let's talk about some ideas about how to associate the lyrics of mediaplayer, seekbar, and LRC files.

First, let's talk about mediaplayer to play audio. If you use a real machine for development, there will be no trouble. If you use a simulator for development, note that if you store the audio file in the SD card, you need to create an SD card image under CMD and load the SD card image under cmd to start the image in your simulator so that the simulator can use the SD card (Google ). After talking about the device, let's talk about the controls we use. First, we can play the audio file on the terminal and control it through the scroll bar. Here we need to start and pause two buttons and the total length of the song. The current song length is two textviews, and seekbar is used to display the playing progress of the song. The most important thing is mediaplayer.

Here I will only introduce the idea. As for the method to be called and how to implement it in the code, I would like to make it myself. Next, you can call and listen to the basic functions of mediaplayer to obtain whether your audio file is ready, the total length of the song, and the current playback duration of the song, set the playing position of a song. Pay attention to the life cycle of the mediaplayer call. Otherwise, there will be many inexplicable bugs. Now the preparation for mediaplayer is complete. Let's talk about how seekbar works with mediaplayer.

First, set the max value of seekbar itself to the total length of the current song during initialization, which saves a lot of trouble in the future algorithm. When the mediaplayer player starts, we can use a thread to call it once every second to obtain the current playback duration of the mediaplayer, assign the progress to the seekbar, and listen to the seekbar, set the mediaplayer position in the onstoptrackingtouch method of seekbar. In this way, the playing of the mediaplayer is bound with the seekbar. The first step is complete. If you want to know how the lyrics are controlled with mediaplayer and seekbar, read the next article, get off work, and write them again tomorrow.

Note:

Mediaplayer lifecycle.

Call the basic function of mediaplayer.

Mediaplayer listener usage.

Seekbar listener.

The basic function usage of seekbar.

If you use a virtual machine, you need to know the android simulator control in cmd.

Reprinted with original Shenyang binzi

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.