ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词
一、流程分析
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来解决此问题
二、简介
在linux用apk处理歌词
三、代码
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/";
}
}
ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词的更多相关文章
- ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast
一.代码 1.xml(1)radio.xml <?xml version="1.0" encoding="utf-8"?> <LinearLa ...
- ANDROID_MARS学习笔记_S01原始版_004_TableLayout
1.xml <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android ...
- ANDROID_MARS学习笔记_S01原始版_003_对话框
1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest ...
- ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用
一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...
- ANDROID_MARS学习笔记_S01原始版_001_Intent
一.Intent简介 二.代码 1.activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.co ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER005_用广播BroacastReciever实现后台播放不更新歌词
一.代码流程1.自定义一个AppConstant.LRC_MESSAGE_ACTION字符串表示广播"更新歌词" 2.在PlayerActivity的onResume()注册Bro ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3
一.简介 1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity 2.PlayerActivity通过 ...
- ANDROID_MARS学习笔记_S01原始版_022_MP3PLAYER002_本地及remote标签
一.简介 1.在main.xml中用TabHost.TabWidget.FrameLayout标签作布局 2.在MainActivity中生成TabHost.TabSpec,调用setIndicato ...
- ANDROID_MARS学习笔记_S01原始版_021_MP3PLAYER001_下载mp3文件
一.简介 1.在onListItemClick()中new Intent,Intent以存储序列化后的mp2Info对象作为参数,启动serivce 2.DownloadService在onStart ...
随机推荐
- php的标记形式
共三种: 推荐第一种,第三种需要在php.ini中配置 效果: 第三种配置 将short_open_tag=Off改为On重启Apache就可以了
- phpize php扩展模块安装
安装(fastcgi模式)的时候,常常有这样一句命令:/usr/local/webserver/php/bin/phpize一.phpize是干嘛的?phpize是什么东西呢?php官方的说明:htt ...
- Apache Rewrite url重定向功能的简单配置
http://www.jb51.net/article/24435.htm 1.Apache Rewrite的主要功能 就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则表达式规范.平时帮助我 ...
- String类中toCharArray()方法的用法
该方法的作用是返回一个字符数组,该字符数组中存放了当前字符串中的所有字符 eg: public class class6_3 { public static void main(String arg ...
- ASP.NET Web Service如何工作(2)
ASP.NET Web Service如何工作(2) [日期:2003-06-26] 来源:CSDN 作者:sunnyzhao(翻译) [字体:大 中 小] HTTP管道一旦调用了.asmx句柄,便 ...
- OC1_银行账户类
// // BankAccount.h // OC1_银行账户类 // // Created by zhangxueming on 15/6/10. // Copyright (c) 2015年 zh ...
- eNSP
L2交换机 sysvlan 200q interface Vlanif 200ip address 192.168.0.1 24dis thisq interface Ethernet 0/0/1po ...
- Convert CString to TCHAR
Quote from: http://vctipsplusplus.wordpress.com/2008/05/21/cstring-to-tchar/ CString is a very usefu ...
- ViewState压缩
/// <summary> ///CompressViewState 的摘要说明 /// </summary> public class CompressViewState:S ...
- 关于H5 storage 的一些注意事项以及用法
在我们使用H5 storage之前,先了解一下storage的介绍吧: Storage模块管理应用本地数据存储,用于应用数据的保存和读取,应用本地数据localStorage,sessionStora ...