MP3播放器的实现
今天,基本上实现了MP3播放器的基本功能,现在总结一下。
首先,下载服务器端的MP3列表,这里用到了下载技术和解析XML文件技术。
下载参考(http://blog.csdn.net/huim_lin/article/details/9857317),解析XML文件参考(http://blog.csdn.net/huim_lin/article/details/9923595)
然后点击列表中某一个文件,下载MP3文件,这里用到了下载文件技术。
public int downFile( String url , String path , String fileName ) {
InputStream input = null;
try {
FileUtils fileUtils = new FileUtils();
if ( fileUtils.isFileExist(fileName , path) ) {
return 1;
} else {
input = getInputStreamformUrl(url);
File resultFile = fileUtils.write2SDformInput(path , fileName , input);
if ( resultFile == null ) {
return -1;
}
}
} catch ( Exception e ) {
e.printStackTrace();
return -1;
} finally {
try {
input.close();
} catch ( Exception e ) {
e.printStackTrace();
}
}
return 0;
}
private InputStream getInputStreamformUrl( String strUrl ) throws Exception {
URL url = new URL(strUrl);
HttpURLConnection conn = ( HttpURLConnection ) url.openConnection();
InputStream input = conn.getInputStream();
return input;
}
接着设置播放按钮,用到了service
public class PlayService extends Service {
private boolean isPlaying = false;
private boolean isPause = false;
private boolean isReleased = false;
private boolean isSeekChanged = false;
private MediaPlayer mediaPlayer = null;
private ArrayList< Queue > queues = null;
private Handler handler = new Handler();
private UpdateTimeCallback updateTimeCallback = null;
private long begin = 0;
private long nextTimeMill = 0;
private long currentTimeMill = 0;
private String message = "";
private long pauseTimeMills = 0;
Queue times = null;
Queue messages = null;
Intent intent = new Intent();
ArrayList< String > messageList = new ArrayList< String >();
ArrayList< Long > timeList = new ArrayList< Long >();
@ Override
public int onStartCommand( Intent intent , int flags , int startId ) {
Mp3Info mp3Info = ( Mp3Info ) intent.getSerializableExtra("mp3Info");
this.intent.setAction(AppConstant.LRC_MESSAGE_ACTION);
int MSG = intent.getIntExtra("MSG" , 0);
if ( mp3Info != null ) {
if ( MSG == AppConstant.PlayerMsg.PLAY_MSG ) {
play(mp3Info);
}
} else {
if ( MSG == AppConstant.PlayerMsg.PAUSE_MSG ) {
pause();
} else if ( MSG == AppConstant.PlayerMsg.STOP_MSG ) {
stop();
}
}
String strSeekStop = intent.getStringExtra("SeekStop");
onSeekStop(strSeekStop);
String strSeekStart = intent.getStringExtra("SeekStart");
if ( strSeekStart != null ) {
onSeekStart();
}
return super.onStartCommand(intent , flags , startId);
}
@ Override
public IBinder onBind( Intent arg0 ) {
return null;
}
private void onSeekStart() {
isSeekChanged = true;
}
private void onSeekStop( String progress ) {
if ( progress != null ) {
currentTimeMill = Long.parseLong(progress);
mediaPlayer.seekTo(( int ) currentTimeMill);
isSeekChanged = false;
}
}
private void play( Mp3Info mp3 ) {
if ( isPause ) {
mediaPlayer.start();
handler.postDelayed(updateTimeCallback , 5);
begin = System.currentTimeMillis() - pauseTimeMills + begin;
isPlaying = true;
isPause = false;
} else if ( !isPlaying ) {
String path = getMp3Path(mp3);
// System.out.println(mp3.getMp3Name()+"\t"+path);
mediaPlayer = MediaPlayer.create(this , Uri.parse("file://" + path));
mediaPlayer.setLooping(false);
mediaPlayer.start();
isPlaying = true;
isReleased = false;
// System.out.println(mp3.getMp3Name() + " is playing " +
// isPlaying);
prepareLrc(mp3.getLrcName());
System.out.println("lrc->" + mp3.getLrcName());
handler.postDelayed(updateTimeCallback , 5);
begin = System.currentTimeMillis();
long total = mediaPlayer.getDuration();
// intent.putExtra("total" , total);
// sendBroadcast(intent);
}
}
private void pause() {
if ( !isReleased ) {
if ( isPlaying ) {
mediaPlayer.pause();
handler.removeCallbacks(updateTimeCallback);
pauseTimeMills = System.currentTimeMillis();
} else {
mediaPlayer.start();
handler.postDelayed(updateTimeCallback , 5);
begin = System.currentTimeMillis() - pauseTimeMills + begin;
currentTimeMill = begin;
}
isPlaying = isPlaying ? false : true;
isPause = true;
}
}
private void stop() {
if ( mediaPlayer != null ) {
if ( isPlaying || isPause ) {
if ( !isReleased ) {
handler.removeCallbacks(updateTimeCallback);
mediaPlayer.stop();
mediaPlayer.release();
pauseTimeMills = System.currentTimeMillis();
isReleased = true;
}
isPlaying = false;
isPause = false;
}
}
}
private String getMp3Path( Mp3Info mp3Info ) {
// TODO Auto-generated method stub
String SDCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
String path = SDCardRoot + File.separator + "mp3" + File.separator + mp3Info.getMp3Name();
return path;
}
private void prepareLrc( String lrcName ) {
String SDCRoot = Environment.getExternalStorageDirectory().getAbsoluteFile() + File.separator;
try {
InputStream inputStream = new FileInputStream(SDCRoot + "mp3/" + lrcName);
LrcProcessor lrcProcessor = new LrcProcessor();
timeList = new ArrayList< Long >();
messageList = new ArrayList< String >();
lrcProcessor.process2(inputStream , timeList , messageList);
updateTimeCallback = new UpdateTimeCallback(timeList , messageList);
// queues = lrcProcessor.process(inputStream);
// updateTimeCallback = new UpdateTimeCallback(queues);
begin = 0;
currentTimeMill = 0;
nextTimeMill = 0;
} catch ( FileNotFoundException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class UpdateTimeCallback implements Runnable {
ArrayList< Queue > queues = null;
ArrayList< String > messageList = null;
ArrayList< Long > timeList = null;
public UpdateTimeCallback ( ArrayList< Queue > queues ) {
this.queues = queues;
}
public UpdateTimeCallback ( ArrayList< Long > timeList ,
ArrayList< String > messageList ) {
this.timeList = timeList;
this.messageList = messageList;
}
@ Override
public void run() {
long offset = mediaPlayer.getCurrentPosition();
int i = 0;
intent.putExtra("current" , mediaPlayer.getCurrentPosition() + "");
intent.putExtra("total" , mediaPlayer.getDuration() + "");
sendBroadcast(intent);
while ( (i + 1) < timeList.size() && offset > timeList.get(i + 1) ) {
i++;
}
if ( i < timeList.size() ) {
message = messageList.get(i);
intent.putExtra("lrcMessage" , message);
sendBroadcast(intent);
}
currentTimeMill = currentTimeMill + 10;
handler.postDelayed(updateTimeCallback , 10);
}
}
}
其中实现了lrc歌词的同步
本程序还实现了拖动seekBar实现实时同步。用两个List保存lrc歌词的时间和内容。
实现效果:
MP3播放器的实现的更多相关文章
- MP3播放器团队项目
一.设计思路 程序要求能播放MP3文件,因此需调用库中的播放方法:右键工具箱选择项,添加com组件,选择window media player后工具箱就会多一个控件,然后拖到窗体中就OK了.另在窗体中 ...
- 你也可以用java的swing可以做出这么炫的mp3播放器_源码下载
I had published the blog : 你用java的swing可以做出这么炫的mp3播放器吗? and to display some screenshots about this M ...
- 你用java的swing可以做出这么炫的mp3播放器吗?
这个mp3播放器是基于java的swing编写的,我认为界面还是可以拿出来和大家看一看评一评. 先说说创作的初衷,由于前段时间工作不是很忙,与其闲着,还不如找一些东西来给自己捣腾捣腾,在 之前写的 j ...
- 安卓MP3播放器开发实例(1)之音乐列表界面
学习安卓开发有一年了,想想这一年的努力,确实也收获了不少.也找到了比較如意的工作. 今天准备分享一个以前在初学阶段练习的一个项目.通过这个项目我真正的找到了开发安卓软件的感觉,从此逐渐步入安卓开发的正 ...
- 开源mp3播放器--madplay 编译和移植 简记
madplay是一款开源的mp3播放器. http://madplay.sourcearchive.com/ 下面简单记录一下madplay的编译与移植到ARM开发板上的过程 一.编译x86版本的ma ...
- 基于Stm32的MP3播放器设计与实现
原创博文,转载请注明出处 这是我高级电子技术试验课做的作业,拿来共享一下.项目在安福莱例程基础之上进行的功能完善,里面的部分内容可参考安福莱mp3例程.当然用的板子也是安福莱的板子,因为算起来总共做了 ...
- x宝23大洋包邮的老式大朝华MP3播放器简单评测
(纯兴趣测评,非广告) 最近逛X宝,看到了这个古董级MP3播放器居然还在售,于是脑抽+情怀泛滥买了一个. 然后呢,从遥远的深圳跨越好几千公里邮过来了这个玩意: 那节南孚5号电池是我自己的,是为了对比一 ...
- 从零开始学习PYTHON3讲义(十四)写一个mp3播放器
<从零开始PYTHON3>第十四讲 通常来说,Python解释执行,运行速度慢,并不适合完整的开发游戏.随着电脑速度的快速提高,这种情况有所好转,但开发游戏仍然不是Python的重点工作. ...
- C# wave mp3 播放器探寻
C# wave mp3 播放器探寻 最近无聊,想听听歌曲.可怜新电脑上歌曲就两三首,要听其它的就得在旧电脑上播放.可是,那台古董但不失健壮的本本被老婆无情的霸占了.无奈. 思来想去,得,写个程序播 ...
随机推荐
- >/dev/null 2>&1 这句话的含义
1表示标准输出,2表示标准错误输出 2>&1表示将标准错误输出重定向到标准输出,这样,程序或者命令的正常输出和错误输出就可以在标准输出输出(也就是一起输出). 一般来讲标准输出和标准错误 ...
- SQL中使用的一些函数问题
abs()取绝对值ceil()上取整floor()下取整initcap()使串中的所有单词的首字母变为大写substr()取子串 这些函数都是oracle的sql内置函数.
- solve_lock-1024-大功告成
create or replace procedure solve_lock_061203(v_msg out varchar2) as v_sql varchar2(3000); --定义 v_s ...
- IOS学习--UILable使用手册(20150120)
第一步:创建一个UILable对象 UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; 第二步:设置对象的各种属性 ...
- 【转】iOS-Core-Animation-Advanced-Techniques(一)
图层树.寄宿图以及图层几何学(一)图层的树状结构 巨妖有图层,洋葱也有图层,你有吗?我们都有图层 -- 史莱克 原文:http://www.cocoachina.com/ios/20150104/1 ...
- SGU 132.Another Chocolate Maniac
时间限制:0.25s 空间限制:4M 题目: Bob非常喜欢巧克力,吃再多也觉得不够.当他的父母告诉他将要买很多矩形巧克力片为他庆祝生日时,他的喜悦是能被理解的.巧克力都是 2x1 或 1x2 的矩形 ...
- window下配置SSH连接GitHub、GitHub配置ssh key(转)
转自:http://jingyan.baidu.com/article/a65957f4e91ccf24e77f9b11.html 此经验分两部分: 第一部分介绍:在windows下通过msysGit ...
- ICE学习第三步-----Slice语言
ICE:Slice语言(一)-编译 Introduce简介 Slice(Specification language for ice)是分离对象和对象的实现的基础的抽象机制.Slice在客户端和服务器 ...
- 学习笔记--【转】Parameter与Attribute的区别&servletContext与ServletConfig区别
原文链接http://blog.csdn.net/saygoodbyetoyou/article/details/9006001 Parameter与Attribute的区别 request. ...
- 用urlencode(String str)对URL传递参数进行编码,提高安全
在PHP 提交地址后面带有参数的时候,参数会在浏览器的地址栏暴露无疑,这样是不安全的,这个时候就必须用些方法对这些参数进行安全处理 这里可以用 urlencode(String URL);//对URL ...