package mars.mp3player;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Queue;
import mars.lrc.LrcProcessor;
import mars.model.Mp3Info;
import mars.mp3player.service.PlayerService;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
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 {
ImageButton beginButton = null;
ImageButton pauseButton = null;
ImageButton stopButton = null;
MediaPlayer mediaPlayer = null;
private ArrayList<Queue> queues = null;
private TextView lrcTextView = null;
private Mp3Info mp3Info = 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 = null;
private long pauseTimeMills = 0;
private boolean isPlaying = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
Intent intent = getIntent();
mp3Info = (Mp3Info) intent.getSerializableExtra("mp3Info");
beginButton = (ImageButton) findViewById(R.id.begin);
pauseButton = (ImageButton) findViewById(R.id.pause);
stopButton = (ImageButton) findViewById(R.id.stop);
beginButton.setOnClickListener(new BeginButtonListener());
pauseButton.setOnClickListener(new PauseButtonListener());
stopButton.setOnClickListener(new StopButtonListener());
lrcTextView = (TextView)findViewById(R.id.lrcText);
}
/**
* 根据歌词文件的名字,来读取歌词文件当中的信息
* @param lrcName
*/
private void prepareLrc(String lrcName){
try {
InputStream inputStream = new FileInputStream(Environment.getExternalStorageDirectory().getAbsoluteFile() +File.separator + "mp3/" + mp3Info.getLrcName());
LrcProcessor lrcProcessor = new LrcProcessor();
queues = lrcProcessor.process(inputStream);
//创建一个UpdateTimeCallback对象
updateTimeCallback = new UpdateTimeCallback(queues);
begin = 0 ;
currentTimeMill = 0 ;
nextTimeMill = 0 ;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
class BeginButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
//创建一个Intent对象,用于同时Service开始播放MP3
Intent intent = new Intent();
intent.setClass(PlayerActivity.this, PlayerService.class);
intent.putExtra("mp3Info", mp3Info);
intent.putExtra("MSG", AppConstant.PlayerMsg.PLAY_MSG);
//读取LRC文件
prepareLrc(mp3Info.getLrcName());
//启动Service
startService(intent);
//将begin的值置为当前毫秒数
begin = System.currentTimeMillis();
//执行updateTimeCallback
handler.postDelayed(updateTimeCallback, 5);
isPlaying = true;
}
}
class PauseButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
//通知Service暂停播放MP3
Intent intent = new Intent();
intent.setClass(PlayerActivity.this, PlayerService.class);
intent.putExtra("MSG", AppConstant.PlayerMsg.PAUSE_MSG);
startService(intent);
//
if(isPlaying){
handler.removeCallbacks(updateTimeCallback);
pauseTimeMills = System.currentTimeMillis();
}
else{
handler.postDelayed(updateTimeCallback, 5);
begin = System.currentTimeMillis() - pauseTimeMills + begin;
}
isPlaying = isPlaying ? false : true;
}
}
class StopButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
//通知Service停止播放MP3文件
Intent intent = new Intent();
intent.setClass(PlayerActivity.this, PlayerService.class);
intent.putExtra("MSG", AppConstant.PlayerMsg.STOP_MSG);
startService(intent);
//从Handler当中移除updateTimeCallback
handler.removeCallbacks(updateTimeCallback);
}
}
class UpdateTimeCallback implements Runnable{
Queue times = null;
Queue messages = null;
public UpdateTimeCallback(ArrayList<Queue> queues) {
//从ArrayList当中取出相应的对象对象
times = queues.get(0);
messages = queues.get(1);
}
@Override
public void run() {
//计算偏移量,也就是说从开始播放MP3到现在为止,共消耗了多少时间,以毫秒为单位
long offset = System.currentTimeMillis() - begin;
if(currentTimeMill == 0){
nextTimeMill = (Long)times.poll();
message = (String)messages.poll();
}
if(offset >= nextTimeMill){
lrcTextView.setText(message);
message = (String)messages.poll();
nextTimeMill = (Long)times.poll();
}
currentTimeMill = currentTimeMill + 10;
handler.postDelayed(updateTimeCallback, 10);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
《Android开发视频教学·源码》第一季
共738个文件
class:279个
xml:99个
java:99个
5星 · 超过95%的资源 需积分: 10 80 下载量 178 浏览量
2011-09-07
22:53:43
上传
评论 2
收藏 6.44MB RAR 举报
温馨提示
《Android开发视频教学》 www.marsdroid.org Android开发视频教程 第一季视频的全部源代码
资源推荐
资源详情
资源评论
收起资源包目录
《Android开发视频教学·源码》第一季 (738个子文件)
resources.ap_ 2.94MB
resources.ap_ 369KB
resources.ap_ 12KB
resources.ap_ 12KB
resources.ap_ 12KB
resources.ap_ 12KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 10KB
Mp3Player04.apk 2.95MB
Mp3Player03.apk 384KB
Mp3Player01.apk 22KB
TestCP.apk 17KB
xml.apk 17KB
Activity_06.apk 16KB
download.apk 16KB
sqlite.apk 16KB
activity_03.apk 16KB
Activity_05.apk 15KB
Activity_04.apk 15KB
Activity_07.apk 15KB
listview.apk 15KB
wifi.apk 15KB
Layout_04.apk 15KB
TestBC2.apk 15KB
Activity_02.apk 15KB
TestBC.apk 15KB
ProgressBarHandler.apk 14KB
HandlerTest2.apk 14KB
Layout_03.apk 14KB
Socket.apk 14KB
progressbar.apk 14KB
HandlerTest.apk 14KB
Layout_02.apk 14KB
Layout_01.apk 14KB
.checkstyle 287B
.checkstyle 287B
Mp3ListActivity.class 5KB
Mp3ListActivity.class 5KB
Mp3ListActivity.class 5KB
FirstContentProvider.class 5KB
PlayerActivity.class 5KB
FileUtils.class 4KB
FileUtils.class 4KB
FileUtils.class 4KB
HttpDownloader.class 3KB
HttpDownloader.class 3KB
PlayerActivity.class 3KB
LrcProcessor.class 3KB
HttpDownloader.class 3KB
HttpDownloader.class 3KB
HttpDownloader.class 3KB
LocalMp3ListActivity.class 3KB
LocalMp3ListActivity.class 3KB
PlayerService.class 3KB
MyContentHandler.class 3KB
FileUtils.class 3KB
FileUtils.class 3KB
Mp3ListContentHandler.class 2KB
Mp3ListContentHandler.class 2KB
Mp3ListContentHandler.class 2KB
Activity01.class 2KB
PlayerActivity$UpdateTimeCallback.class 2KB
SQLiteActivity$QueryListener.class 2KB
LocalMp3ListActivity.class 2KB
PlayerActivity$BeginButtonListener.class 2KB
PlayerActivity$PauseButtonListener.class 2KB
Activity03.class 2KB
FirstActivity.class 2KB
Mp3Info.class 2KB
Mp3Info.class 2KB
Mp3Info.class 2KB
XMLActitity$ParseButtonListener.class 2KB
RadioTest.class 2KB
SecondActivity.class 2KB
FirstActivity.class 2KB
MainActivity.class 2KB
MainActivity.class 2KB
MainActivity.class 2KB
DatabaseHelper.class 2KB
HandlerTest2$MyHandler.class 2KB
HandlerTest2.class 2KB
DatabaseHelper.class 2KB
共 738 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
Zeng
- 粉丝: 4
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页