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 播放器探寻 最近无聊,想听听歌曲.可怜新电脑上歌曲就两三首,要听其它的就得在旧电脑上播放.可是,那台古董但不失健壮的本本被老婆无情的霸占了.无奈. 思来想去,得,写个程序播 ...
随机推荐
- iOS、mac开源项目及库汇总
原文地址:http://blog.csdn.net/qq_26359763/article/details/51076499 iOS每日一记------------之 中级完美大整理 iOS.m ...
- 知识库系统confluence5.8.10 安装与破解
一直对知识库体系很在意,设想这样的场景,公司历年的研发资料只要一个搜索,相关的知识点就全部摆在面前,任君取用,想一想就无限迷人,只是从10年开始,由于种种原因,终究没能好好研究一下.最近机缘巧合,可以 ...
- TestNG Listener
常用接口 IExecutionListener 监听TestNG运行的启动和停止. IAnnotationTransformer 注解转换器,用于TestNG测试类中的注解. ISuiteList ...
- VB.NET中LINQ TO List泛型查询语句(分组,聚合函数)
Public Class LinqToList 'LINQ在C#中使用比较方便,但是在VB中使用比较麻烦,复杂,和C#用法并不太一样 Dim listNew As List(Of Product) = ...
- php之框架增加日志记录功能类
<?php /* 思路:给定文件,写入读取(fopen ,fwrite……) 如果大于1M 则重写备份 传给一个内容, 判断大小,如果大于1M,备份 小于则写入 */ class Log{ // ...
- [css][移动设备]禁止横竖屏时内容自动调整
参考:http://www.kankanews.com/ICkengine/archives/106643.shtml iOS下当竖屏转向横屏的时候,发现内容字体会自动变大,通过各种方法设置字体大小都 ...
- [Android1.5]TextView跑马灯效果
from: http://www.cnblogs.com/over140/archive/2010/08/20/1804770.html 前言 这个效果在两周前搜索过,网上倒是有转载,可恨的是转载之后 ...
- DEDECMS 关键字不能小于2个字节!
今天在做DEDECMS模板时,突然遇到了“关键字不能小于2个字节!”晕,是怎么回事呢?百度了一下,找到了答案,把他记录下来,方便自己日后再遇到这种问题时,可以查询: <form name=&qu ...
- python学习笔记(一)元组,序列,字典
python学习笔记(一)元组,序列,字典
- HashMap在Android和Java中的不同实现
起因 今天在项目中遇到一个很"奇葩"的问题.情况大致是这样的:Android终端和服务器(Spring),完全相同的字符串键值对放入HashMap中竟然顺序不一样,这直接导致了服务 ...