ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词

时间:2021-05-20 20:10:22

一、流程分析

1.点击播放按钮,会根据lrc名调用LrcProcessor的process()分析歌词文件,得到时间队列和歌词队列

2.new一个hander,把时间队列和歌词队列传给自定义的线程类UpdateTimeCallback,调用handler.postDelayed(updateTimeCallback, 5);启动线程

3.UpdateTimeCallback会在线程执行时用当前时间减去成员变量begin,则可知歌曲播放了多久,再根据此时间与时间队列的时间比较,就是知道此时要显示什么歌词,从而把歌词队列的一个message设置给lrcTextView以显示

4.UpdateTimeCallback最后会自己调用handler.postDelayed(updateTimeCallback, 100);,所以线程会每0.1秒判断一次歌词的显示

PS:此代码有一个不足之处,即使app后台播放,更新歌词的线程仍会执行,浪费资源,一个版本会通过broastreciever来解决此问题

二、简介

ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词

ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词

在linux用apk处理歌词

ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词

三、代码
1.xml

2.java
(1)PlayerActivity.java

 package tony.mp3player;

 import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Queue; import tony.model.Mp3Info;
import tony.mp3player.service.PlayerService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView; public class PlayerActivity extends Activity { private ImageButton beginBtn = null;
private ImageButton pauseBtn = null;
private ImageButton stopBtn = null; private List<Queue> queues = null;
private TextView lrcTextView = null;
private Mp3Info info = null;
private Handler handler = new Handler();
private UpdateTimeCallback updateTimeCallback = null;
private long begin = 0;
private long nextTimeMill = 0;
private long currentTimeMill = 0;
private String msg = null;
private long pauseTimeMills = 0;
private boolean isPlaying = false; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
Intent intent = getIntent();
info = (Mp3Info) intent.getSerializableExtra("mp3Info");
beginBtn = (ImageButton) findViewById(R.id.begin);
pauseBtn = (ImageButton) findViewById(R.id.pause);
stopBtn = (ImageButton) findViewById(R.id.stop);
lrcTextView = (TextView) findViewById(R.id.lrcText); beginBtn.setOnClickListener(new BeginListener());
pauseBtn.setOnClickListener(new PauseListener());
stopBtn.setOnClickListener(new StopListener());
} /**
* 根据歌词文件的名字,来读取歌词文件当中的信息
* @param lrcName
*/
private void prepareLrc(String lrcName) {
try {
InputStream inputStream;
inputStream = new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + "mp3" + File.separator + info.getLrcName());
LrcProcessor lrcProcessor = new LrcProcessor();
queues = lrcProcessor.process(inputStream);
updateTimeCallback = new UpdateTimeCallback(queues);
begin = 0;
currentTimeMill = 0;
nextTimeMill = 0;
} catch (Exception e) {
e.printStackTrace();
}
} class BeginListener implements OnClickListener {
@Override
public void onClick(View v) {
if(!isPlaying) {
//创建一个Intent对象,用于通知Service开始播放MP3
Intent intent = new Intent();
intent.putExtra("mp3Info", info);
intent.putExtra("MSG", AppConstant.PlayerMsg.PLAY_MSG);
intent.setClass(PlayerActivity.this, PlayerService.class);
//读取LRC文件,放于startservice前,是为了防止歌曲已播放,但歌词没读完,造成不同步
prepareLrc(info.getLrcName());
startService(intent);
begin = System.currentTimeMillis();
handler = new Handler();
handler.postDelayed(updateTimeCallback, 5);//5毫秒是试验得出的
isPlaying = true;
}
}
} class PauseListener implements OnClickListener {
@Override
public void onClick(View v) {
//通知Service暂停播放MP3
Intent intent = new Intent();
intent.putExtra("MSG", AppConstant.PlayerMsg.PAUSE_MSG);
intent.setClass(PlayerActivity.this, PlayerService.class);
startService(intent);
if(isPlaying) {
//不再更新歌词
handler.removeCallbacks(updateTimeCallback);
//用来下面代码计算暂停了多久
pauseTimeMills = System.currentTimeMillis();
} else {
handler.postDelayed(updateTimeCallback, 5);
//因为下面的时间偏移是这样计算的offset = System.currentTimeMillis() - begin;
//所以要把暂停的时间加到begin里去,
begin = System.currentTimeMillis() - pauseTimeMills + begin;
}
isPlaying = !isPlaying;
}
} class StopListener implements OnClickListener {
@Override
public void onClick(View v) {
//通知Service停止播放MP3文件
Intent intent = new Intent();
intent.putExtra("MSG", AppConstant.PlayerMsg.STOP_MSG);
intent.setClass(PlayerActivity.this, PlayerService.class);
startService(intent);
//从Handler当中移除updateTimeCallback
handler.removeCallbacks(updateTimeCallback);
isPlaying = false;
}
} class UpdateTimeCallback implements Runnable{
Queue<Long> times = null;
Queue<String> msgs = null; public UpdateTimeCallback(List<Queue> queues) {
this.times = queues.get(0);
this.msgs = queues.get(1);
} @Override
public void run() {
//计算偏移量,也就是说从开始播放MP3到现在为止,共消耗了多少时间,以毫秒为单位
long offset = System.currentTimeMillis() - begin;
if(currentTimeMill == 0) {//刚开始播放时,调用prepareLrc(),在其中设置currentTimeMill=0
nextTimeMill = times.poll();
msg = msgs.poll();
}
//歌词的显示是如下:例如
//[00:01.00]Look
//[00:03.00]Up
//[00:06.00]Down
//则在第1~3秒间是显示“look”,在第3~6秒间是显示"Up",在第6秒到下一个时间点显示"Down"
if(offset >= nextTimeMill) {
lrcTextView.setText(msg);
msg = msgs.poll();
nextTimeMill = times.poll();
}
currentTimeMill = currentTimeMill + 100;
//在run方法里调用postDelayed,则会形成循环,每0.01秒执行一次线程
handler.postDelayed(updateTimeCallback, 100);
}
}
}

(2)PlayService.java

 package tony.mp3player.service;

 import java.io.File;

 import tony.model.Mp3Info;
import tony.mp3player.AppConstant;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder; public class PlayerService extends Service { private boolean isPlaying = false;
private boolean isPause = false;
private boolean isReleased = false;
private MediaPlayer mediaPlayer = null; @Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Mp3Info info = (Mp3Info) intent.getSerializableExtra("mp3Info");
int MSG = intent.getIntExtra("MSG", 0);
if(info != null) {
if(MSG == AppConstant.PlayerMsg.PLAY_MSG) {
play(info);
}
} else {
if(MSG == AppConstant.PlayerMsg.PAUSE_MSG) {
pause();
}
else if(MSG == AppConstant.PlayerMsg.STOP_MSG) {
stop();
}
}
return super.onStartCommand(intent, flags, startId);
} private void stop() {
if(mediaPlayer != null) {
if(isPlaying) {
if(!isReleased) {
mediaPlayer.stop();
mediaPlayer.release();
isReleased = true;
isPlaying = false;
}
}
}
} private void pause() {
if(mediaPlayer != null) {
if(!isReleased){
if(!isPause) {
mediaPlayer.pause();
isPause = true;
} else {
mediaPlayer.start();
isPause = false;
}
}
}
} private void play(Mp3Info info) {
if(!isPlaying) {
String path = getMp3Path(info);
mediaPlayer = MediaPlayer.create(this, Uri.parse("file://" + path));
mediaPlayer.setLooping(false);
mediaPlayer.start();
isPlaying = true;
isReleased = false;
}
} private String getMp3Path(Mp3Info mp3Info) {
String SDCardRoot = Environment.getExternalStorageDirectory()
.getAbsolutePath();
String path = SDCardRoot + File.separator + "mp3" + File.separator
+ mp3Info.getMp3Name();
return path;
}
}

3.LrcProcessor.java

 package tony.mp3player;

 import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class LrcProcessor { public ArrayList<Queue> process(InputStream inputStream) {
Queue<Long> timeMills = new LinkedList<Long>();
Queue<String> messages = new LinkedList<String>();
ArrayList<Queue> queues = new ArrayList<Queue>();
try {
InputStreamReader inputReader = new InputStreamReader(inputStream);
BufferedReader bufferReader = new BufferedReader(inputReader);
//创建一个正则表达式对象,寻找两边都带中括号的文本
Pattern p = Pattern.compile("\\[([^\\]]+)\\]");
String temp = null;
String result = null;
while((temp = bufferReader.readLine()) != null) {
Matcher m = p.matcher(temp);
if(m.find()) {
if(result != null) {//正则第一次到时是,此时result还没值,到下一次循环时,就会把第一次计算出的result加到队列里
messages.add(result);
}
String timeStr = m.group();
Long timeMill = time2Long(timeStr.substring(1, timeStr.length() - 1));
timeMills.offer(timeMill);//和add相比,offer不会抛异常
//取出时间串后面的歌词,如[00:02.31]Lose Yourself,得到“Lose Yourself”
String msg = temp.substring(timeStr.length());
result = "" + msg + "\n";//防止msg为null时抛nullpoint
} else {
result = result + temp + "\n";
//比如歌词如下:则上面的if会得到result = a + "\n",
//而else里会使result = a + "\n" + b + "\n" + c + "\n"
//[00:32.42]a
//b
//c
}
}
messages.add(result);//把最后一次循环的result加到quenue里
queues.add(timeMills);//把时间队列和歌词队列都加到list里
queues.add(messages);
} catch (Exception e) {
e.printStackTrace();
}
return queues;
} private Long time2Long(String timeStr) {
//eg : 00:02.31
String [] s = timeStr.split(":");
int min = Integer.parseInt(s[0]);
String ss[] = s[1].split("\\.");
int sec = Integer.parseInt(ss[0]);
int mill = Integer.parseInt(ss[1]);
return min * 60 * 1000 + sec * 1000 + mill * 10L;
} }

4.AppConstant.java

 package tony.mp3player;

 public interface AppConstant {

     public class PlayerMsg {
public static final int PLAY_MSG = 1;
public static final int PAUSE_MSG = 2;
public static final int STOP_MSG =3;
}
public class URL {
public static final String BASE_URL = "http://192.168.1.104:8080/mp3/";
}
}