package com.fqq.mp3player;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Queue;
import com.fqq.model.Mp3Info;
import com.fqq.mp3player.service.AppConstant;
import com.fqq.mp3player.service.PlayerService;
import com.fqq.src.LrcProcessor;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
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{
private ImageButton startButton = null;
private ImageButton pauseButton = null;
private 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 long start = 0;
private long nextTimeMill = 0;
private long currentTimeMill = 0;
private String message = null;
private long pauseTimeMills = 0;
private boolean isPlaying = false;
private IntentFilter intentFilter = null;
private BroadcastReceiver receiver = null;
// 当Activity处于不可见的状态时就会执行onPause()方法
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
// 当Avtivity处于可见的状态或恢复到可见的状态就会执行onResume()方法
@Override
protected void onResume() {
super.onResume();
receiver = new LrcMessageBroadcastReceiver();
registerReceiver(receiver, getIntentFileter());
}
@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");
startButton = (ImageButton)findViewById(R.id.start);
pauseButton = (ImageButton)findViewById(R.id.pause);
stopButton = (ImageButton)findViewById(R.id.stop);
startButton.setOnClickListener(new StartButtonListener());
pauseButton.setOnClickListener(new PauseButtonListener());
stopButton.setOnClickListener(new StopButtonListener());
lrcTextView = (TextView)findViewById(R.id.lrcText);
System.out.println("PlayActivity ------>mp3Info ----->" + mp3Info);
}
// 根据歌词文件的名字,来读取歌词文件当中的信息
// private void prepareLrc(String lrcName){
// try {
// InputStream inputStream = new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/");
// LrcProcessor lrcProcessor = new LrcProcessor();
// queues = lrcProcessor.process(inputStream);
// // 创建一个UpdateTimeCallback对象
// updateTimeCallback = new UpdateTimeCallback(queues);
// start = 0;
// currentTimeMill = 0;
// nextTimeMill = 0;
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
class StartButtonListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// 创建一个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);
// 将start的值赋为当前毫秒值
// start = System.currentTimeMillis();
// // 执行updateTimeCallback
// handler.postDelayed(updateTimeCallback, 5);
// isPlaying = true;
}
}
class PauseButtonListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
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);
// start = System.currentTimeMillis() - pauseTimeMills + start;
// }
// isPlaying = isPlaying ? false : true;
}
}
class StopButtonListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// 通知Service停止播放MP3文件
Intent intent = new Intent();
intent.setClass(PlayerActivity.this, PlayerService.class);
intent.putExtra("MSG", AppConstant.PlayerMsg.STOP_MSG);
startService(intent);
// handler.removeCallbacks(updateTimeCallback);
}
}
// class UpdateTimeCallback implements Runnable{
// ArrayList<Queue> queues = null;
// Queue times = null;
// Queue messages = null;
// public UpdateTimeCallback(ArrayList<Queue> queues) {
// this.queues = queues;
// // 从ArrayList当中取出相应的对象
// times = queues.get(0);
// messages = queues.get(1);
// }
// @Override
// public void run() {
// // 计算偏移量,从开始播放mp3到现在为止,共消耗了多长时间,单位毫秒
// long offset = System.currentTimeMillis() - start;
// if(currentTimeMill == 0){
// nextTimeMill = (Long) times.poll();
// message = (String)messages.poll();
// }
// if(offset >= nextTimeMill){
// System.out.println("message is is is is :------->" + message);
// lrcTextView.setText(message);
// message = (String)messages.poll();
// nextTimeMill = (Long)times.poll();
// }
// currentTimeMill = currentTimeMill + 10;
// handler.postDelayed(updateTimeCallback, 10);
// }
// }
private IntentFilter getIntentFileter(){
if(intentFilter == null){
intentFilter = new IntentFilter();
intentFilter.addAction(AppConstant.LRC_MESSAGE_ACTION);
}
return intentFilter;
}
/**
* 广播接收器,主要接收Serivce所返回的广播,也就是更新歌词的TextView
* @author Administrator
*
*/
class LrcMessageBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context content, Intent intent) {
// 从Intent对象当中取出歌词信息
String lrcMessage = intent.getStringExtra("lrcMessage");
lrcTextView.setText(lrcMessage);
}
}
}