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);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
mars android 视频代码(全集)和安卓PPT讲义,附件大小为17.7M,是花费很长时间搜索整理,包括mars老师Android视频的全部代码和PPT讲义。。。其中包括了SQLite、Handler、滚动条、Mp3等等等等等等等等等等等等等等等等等等等等等等等等.........自己看,懒得写
资源推荐
资源详情
资源评论
收起资源包目录
mars android 视频代码(全集)和安卓PPT讲义(18集) (1366个子文件)
resources.ap_ 2.94MB
resources.ap_ 369KB
resources.ap_ 253KB
resources.ap_ 21KB
resources.ap_ 20KB
resources.ap_ 13KB
resources.ap_ 12KB
resources.ap_ 12KB
resources.ap_ 12KB
resources.ap_ 12KB
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_ 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
resources.ap_ 10KB
resources.ap_ 10KB
Mp3Player04.apk 2.95MB
Mp3Player03.apk 384KB
10_appwidget04.apk 258KB
15_animations05.apk 24KB
helloworld.apk 24KB
Mp3Player01.apk 20KB
Mp3Player01.apk 18KB
03_autocomplete_textview.apk 18KB
12_animations02.apk 17KB
TestCP.apk 17KB
xml.apk 17KB
16_animations06.apk 17KB
14_animations04.apk 16KB
Mp3Player01.apk 16KB
download.apk 16KB
09_appwidget03.apk 16KB
08_appwidget02.apk 16KB
sqlite.apk 16KB
activity_03.apk 16KB
Activity_04.apk 16KB
04_expandablelistview.apk 15KB
07_appwidget01.apk 15KB
11_animations01.apk 15KB
Activity_05.apk 15KB
01_spinner.apk 15KB
Activity_07.apk 15KB
17_animations07.apk 15KB
listview.apk 15KB
02_bluetooth02.apk 15KB
wifi.apk 15KB
Layout_04.apk 15KB
TestBC2.apk 15KB
Activity_02.apk 15KB
TestBC.apk 15KB
ProgressBarHandler.apk 14KB
05_location02.apk 14KB
HandlerTest2.apk 14KB
Layout_03.apk 14KB
Socket.apk 14KB
02_date_and_time.apk 14KB
04_location01.apk 14KB
01_bluetooth01.apk 14KB
progressbar.apk 14KB
06_location03.apk 14KB
HandlerTest.apk 14KB
05_seekbar.apk 14KB
06_ratingbar.apk 14KB
Layout_02.apk 14KB
Activity_01.apk 13KB
Layout_01.apk 13KB
共 1366 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
jllon03
- 粉丝: 0
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页