package com.gem.music;
import java.util.ArrayList;
import java.util.List;
import com.gem.Utils.Music;
import com.gem.music.R;
import com.gem.tools.SetListDataForOur;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint({ "HandlerLeak", "ShowToast", "DefaultLocale" })
public class MusicActivity extends Activity implements OnSeekBarChangeListener,
MediaPlayer.OnErrorListener, Runnable {
public static MediaPlayer player = null;
private TextView textName;
private TextView textSinger;
private TextView textStartTime;
private TextView textEndTime;
private SeekBar seekBar1;
@SuppressWarnings("unused")
private ImageView icon;
private ImageButton imageBtnLast;
private ImageButton imageBtnRewind;
private ImageButton imageBtnPlay;
private ImageButton imageBtnForward;
private ImageButton imageBtnNext;
private ImageButton imageBtnLoop;
private ImageButton imageBtnRandom;
private boolean isLoop = false;
public static int _id = -1;
@SuppressWarnings("unused")
private int cruID = -1;
private List<Music> lists;
private AudioManager audioManager;// 音量管理者
private int maxVolume;// 最大音量
private int currentVolume;// 当前音量
private SeekBar seekBarVolume;// 进度条
private Cursor cursor;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int position = msg.what;// 获取handler 跳转位置
int total = player.getDuration();
int progress = position * 100 / total;
textStartTime.setText(toTime(position));
seekBar1.setProgress(progress);
super.handleMessage(msg);
}
};
/*****
* 该方法,调用的是,显示的时间的功能
*
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.music);
textName = (TextView) this.findViewById(R.id.music_name);
textSinger = (TextView) this.findViewById(R.id.music_singer);
textStartTime = (TextView) this.findViewById(R.id.music_start_time);
textEndTime = (TextView) this.findViewById(R.id.music_end_time);
seekBar1 = (SeekBar) this.findViewById(R.id.music_seekBar);
icon = (ImageView) this.findViewById(R.id.image_icon);
imageBtnLast = (ImageButton) this.findViewById(R.id.music_lasted);
imageBtnRewind = (ImageButton) this.findViewById(R.id.music_rewind);
imageBtnPlay = (ImageButton) this.findViewById(R.id.music_play);
imageBtnForward = (ImageButton) this.findViewById(R.id.music_foward);
imageBtnNext = (ImageButton) this.findViewById(R.id.music_next);
imageBtnLoop = (ImageButton) this.findViewById(R.id.music_loop);
seekBarVolume = (SeekBar) this.findViewById(R.id.music_volume);
imageBtnRandom = (ImageButton) this.findViewById(R.id.music_random);
setListData();
imageBtnLast.setOnClickListener(new MyListener());
imageBtnRewind.setOnClickListener(new MyListener());
imageBtnPlay.setOnClickListener(new MyListener());
imageBtnForward.setOnClickListener(new MyListener());
imageBtnNext.setOnClickListener(new MyListener());
imageBtnLoop.setOnClickListener(new MyListener());
imageBtnRandom.setOnClickListener(new MyListener());
seekBar1.setOnSeekBarChangeListener(this);// 监听进度条时所用
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);// 获得最大音量
currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);// 获得当前音量
seekBarVolume.setMax(maxVolume);
seekBarVolume.setProgress(currentVolume);
seekBarVolume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, AudioManager.FLAG_ALLOW_RINGER_MODES);
}
});
}
@SuppressWarnings("deprecation")
@Override
protected void onStart() {
// TODO Auto-generated method stub
int id = getIntent().getIntExtra("id", 1);
Music m = lists.get(id);
SongsActivity.listMusic.add(m);
textName.setText(m.getTitle());
textSinger.setText(m.getSinger());
textEndTime.setText(toTime((int) m.getTime()));
imageBtnPlay.setImageResource(R.drawable.pause1);
if (id != _id) {
_id = id;
if (player != null) {
if (player.isPlaying()) {
player.release();
player = null;
}
}
String url = m.getUrl();
Uri myUri = Uri.parse(url);
player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setWakeMode(this, PowerManager.PARTIAL_WAKE_LOCK);
try {
player.setDataSource(getApplicationContext(), myUri);
player.prepare();
} catch (Exception e) {
e.printStackTrace();
}
player.start();
// PendingIntent主要持有的信息是它所包装的Intent和当前Application的Context,
// 正由于PendingIntent中保存有当前Application的Context,使它赋予带他程序一种执行的Intent的能力,
// 就算在执行时当前Application已经不存在了,也能通过存在PendingIntent里的Context照样执行Intent。
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);// 获取通知管理器
int icon = R.drawable.play1;
CharSequence tickerText = "正在播放音乐 . . . ";
long when = System.currentTimeMillis();// 通知发生的时间为系统当前时间
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "正在播放音乐 . . . ";
CharSequence contentText = "";
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);// 当点击消息时就会向系统发送openintent意图
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);// contentIntent,它是PendingIntent中最重要的一个参数,是为了发射意图
mNotificationManager.notify(1, notification);
}
if (player != null) {
player.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// 下一首
player.reset();
if (isLoop == true) {
_id = _id - 1;
}
forward();
}
});
}
new Thread(this).start();
super.onStart();// 调用当前 方法
}
// 图片按钮功能区的实现
private class MyListener implements OnClickListener {
@Override
public void onClick(View v) {
if (v == imageBtnLast) {
// 第一首
player.release();
player = null;
first();// 通过内部类实现
} else if (v == imageBtnRewind) {
// 前一首
player