package com.tarena.fashionmusic.play;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.AsyncQueryHandler;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.tarena.fashionmusic.MyApplication;
import com.tarena.fashionmusic.R;
import com.tarena.fashionmusic.entry.BaiduMusic;
import com.tarena.fashionmusic.entry.Music;
import com.tarena.fashionmusic.lrc.BaiduLrc;
import com.tarena.fashionmusic.lrc.Lyric;
import com.tarena.fashionmusic.lrc.LyricView;
import com.tarena.fashionmusic.lrc.PlayListItems;
import com.tarena.fashionmusic.lrc.xml.LRCXmlParser;
import com.tarena.fashionmusic.main.MainActivity;
import com.tarena.fashionmusic.main.adapter.NowPlayListAdapter;
import com.tarena.fashionmusic.play.popmeu.lrcsetting;
import com.tarena.fashionmusic.service.MusicPlayerService;
import com.tarena.fashionmusic.ui.KeywordsFlow;
import com.tarena.fashionmusic.util.BitmapTool;
import com.tarena.fashionmusic.util.Constant;
import com.tarena.fashionmusic.util.HttpTool;
import com.tarena.fashionmusic.util.MusicPreference;
import com.tarena.fashionmusic.util.Rotate3dAnimation;
import com.tarena.fashionmusic.util.SavelrcTool;
import com.tarena.fashionmusic.util.StrTime;
public class MusicPlayActivity extends Activity implements OnClickListener {
ImageView chose_albumlist;
private ViewGroup mContainer;
ViewPager viewPager;
RelativeLayout contain_viewpage;
ListView album_list;
private SeekBar seekBar;
private List<View> mListViews;
private ImageView ivnowpage, ivshow_album;
private ImageButton btprevious, btplay, btnext, bt_playmode, bt_down;
private TextView tvsongname, tvsinger, tvcurrent, tvdurction, tvcurrlrc,
tv_nolrc;
private AwesomePagerAdapter pagerAdapter;
private MusicinfoRec MusicinfoRec;
Bitmap nowbitmap;
private KeywordsFlow keywordsFlow;
private LyricView lyricView;
public static Lyric mLyric;
public static final String TTpath = "mnt/sdcard/TMusic/";
int progress = 0;
public static Intent intent;
public Context context;
int position, nowplaymode;// 当前播放歌曲下标 播放模式
boolean ishavelrc = false;
int totalms = 1;
Music music;
LayoutInflater inflater;
MusicPreference musicPreference;
AudioManager mAudioManager;
int MAX_WORLDS = 10;
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
sendBroadcast(new Intent(Constant.ACTION_STOP));
MyApplication.getInstance().exit();
}
};
private class MusicinfoRec extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 更新时间进度
// if (intent.getAction().equals(Constant.ACTION_UPDATE_TIME)) {
// int curms = intent.getIntExtra("musiccurrent", 0);
// int totalms = intent.getIntExtra("totalms", 1);// 总时长
// tvcurrent.setText(StrTime.gettim(curms));
// try {
// // 将当前的时间 转换成ProgressBar进度值
// int progress = curms * 100 / totalms;
// // 设置当前进度
// seekBar.setProgress(progress);
// } catch (ArithmeticException e) {
// e.printStackTrace();
// }
// // 更新 歌曲信息
// } else
if (intent.getAction().equals(Constant.ACTION_UPDATE)) {
position = intent.getIntExtra("position", 0);
music = (Music) intent.getSerializableExtra("music");
totalms = intent.getIntExtra("totalms", 288888);// 总时长
Log.i("SMZ", totalms + "");
try {
tvsongname.setText(music.getMusicName());
tvsinger.setText(music.getSinger());
tvdurction.setText(StrTime.getTime(music.getTime()));
ShowSongalbum(context);
if (MyApplication.mediaPlayer.isPlaying()) {
btplay.setImageResource(R.drawable.desktop_pausebt_b);
isplaying = true;
} else {
isplaying = false;
btplay.setImageResource(R.drawable.desktop_playbt_b);
}
((NowPlayListAdapter) album_list.getAdapter())
.showNowPlayPos(position);
ishavelrc = false;
ShowLyric(TTpath + music.getMusicName() + "-"
+ music.getSinger() + ".lrc");// 显示歌词
Log.i("music", " 开始更新 下载歌词");
tvcurrlrc.setText("");
if (album_list.isFocused()) {
album_list.setSelection(position);
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (Constant.ACTION_UPDATE_LRC.equals(intent.getAction())) {
String currLrc = intent.getStringExtra("lrccurr");
if (currLrc != null && tvcurrlrc != null && !currLrc.equals(""))
if (ishavelrc) {
tvcurrlrc.setText(currLrc);
} else
tvcurrlrc.setText("");
}
}
}
/**
* 显示 专辑图片!
*
* @param context
* @param intent
*/
Animation animation;
private void ShowSongalbum(Context context) {
String albumkey = music.getAlbumkey();
if (albumkey != null && !"".equals(albumkey)) {
nowbitmap = BitmapTool.getbitBmBykey(context, music.getAlbumkey());
} else {
nowbitmap = MyApplication.bitmap_l;
}
if (nowbitmap != null && nowbitmap.isRecycled() == false) {
ivshow_album.setImageBitmap(nowbitmap);
} else {
ivshow_album.setImageBitmap(MyApplication.bitmap_l);
}
ivshow_album.startAnimation(animation);
}
private void feedKeywordsFlow(KeywordsFlow keywordsFlow, String[] arr) {
Random random = new Random();
for (int i = 0; i < MAX_WORLDS; i++) {
int ran = random.nextInt(arr.length);
String tmp = arr[ran];
keywordsFlow.feedKeyword(tmp);
}
}
Handler nameshandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
final BaiduMusic b = (BaiduMusic) msg.obj;
Log.i("music", "解析完毕---------" + b.getLrcid());
if (b.getLrcid() != null) {
new Thread(new Runnable() {
@Override
public void run() {
try {
SavelrcTool.save(HttpT