今天,基本上实现了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歌词的时间和内容。
实现效果: