package com.liuyang.ClassTest04;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
/**
* 音乐播放器界面类
* @author Administrator
*
*/
public class MusicPlayerActivity extends Activity {
private ImageButton startOrPauseButton;
private ImageButton stopButton;
private ImageButton frontButton;
private ImageButton nextButton;
private Button listButton;
private TextView songInfoText;
private TextView songTimeText;
private TextView songVoiceText;
/**工具类的引用*/
private MusicPlayerService mpService;
/**游标的引用,将会在后面通过赋值让它包含歌曲列表信息*/
private Cursor cursor;
/**歌曲播放时间刷新的信号*/
private static final int REFRESHTIME = 0x10000;
/**歌曲的音量,初始值为0.5f*/
private float musicVolume = 0.5f;
/**
* 开启后台服务
*/
private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
mpService = ((MusicPlayerService.LocalBinder)binder).getService();
}
public void onServiceDisconnected(ComponentName className) {
mpService = null;
}
};
/**
* 接受广播
*/
protected BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String actionString = intent.getAction();
//收到的广播消息为:音乐播放准备完成
if (actionString.equals(MusicPlayerService.MUSIC_PREPARED_END)) {
startOrPauseButton.setImageResource(R.drawable.pause);
} else { //收到的广播消息为:当前音乐已经播放完毕
startOrPauseButton.setImageResource(R.drawable.start);
//跳到下一曲
nextMusic();
}
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//给游标赋值,cursor将包含歌曲列表的信息
cursor = MusicListActivity.mCursor;
//开启后台服务
try {
startService(new Intent(this, MusicPlayerService.class));
bindService(new Intent(this, MusicPlayerService.class), serviceConnection, Context.BIND_AUTO_CREATE);
} catch(Exception e) {
Toast.makeText(this, "开启服务失败", Toast.LENGTH_LONG).show();
}
//组件的实例化
startOrPauseButton = (ImageButton)this.findViewById(R.id.start_or_pause_btn);
stopButton = (ImageButton)this.findViewById(R.id.stop_btn);
nextButton = (ImageButton)this.findViewById(R.id.next_btn);
frontButton = (ImageButton)this.findViewById(R.id.front_btn);
listButton = (Button)this.findViewById(R.id.list_btn);
songInfoText = (TextView)this.findViewById(R.id.song_info_text);
songTimeText = (TextView)this.findViewById(R.id.song_time_text);
songVoiceText = (TextView)this.findViewById(R.id.song_voice_text);
//开始/暂停 按钮的事件监听
startOrPauseButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (mpService != null && mpService.isPlaying()) {
mpService.pauseMusic();
startOrPauseButton.setImageResource(R.drawable.start);
} else if (mpService != null && MusicListActivity.path != null) {
mpService.setDataPath(MusicListActivity.path);
mpService.startMusic();
//显示当前播放歌曲的曲名
songInfoText.setText(getSongInfo());
//显示播放音量
songVoiceText.setText(String.valueOf((int)(musicVolume * 10)));
startOrPauseButton.setImageResource(R.drawable.pause);
}
}
});
//停止播放按钮事件监听
stopButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (mpService != null) {
songTimeText.setText("00:00");
startOrPauseButton.setImageResource(R.drawable.start);
mpService.stopMusic();
}
}
});
//上一曲按钮事件监听
frontButton.setOnClickListener(new ImageButton.OnClickListener() {
public void onClick(View v) {
if (mpService != null) {
frontMusic();
songInfoText.setText(getSongInfo());
}
}
});
//下一曲按钮事件监听
nextButton.setOnClickListener(new ImageButton.OnClickListener() {
public void onClick(View v) {
if (mpService != null) {
nextMusic();
songInfoText.setText(getSongInfo());
}
}
});
//显示列表按钮事件监听
listButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//跳到MusicListActivity,该界面显示歌曲列表信息
Intent intent = new Intent();
intent.setClass(MusicPlayerActivity.this, MusicListActivity.class);
startActivity(intent);
MusicPlayerActivity.this.finish();
}
});
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MusicPlayerService.MUSIC_COMPLETED);
intentFilter.addAction(MusicPlayerService.MUSIC_PREPARED_END);
//注册广播接收器
registerReceiver(broadcastReceiver, intentFilter);
//开启线程,更新歌曲播放的时间信息
new Thread(new Runnable() {
public void run() {
while(true) {
Message msg = new Message();
msg.what = MusicPlayerActivity.REFRESHTIME;
//通过Handler发送消息
//此处若直接在线程内更新,则会报错,因为线程不是安全的(自己暂时这样肤浅的理解吧)
MusicPlayerActivity.this.myHandler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch(Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
//接受消息,更新歌曲播放时间信息
Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == MusicPlayerActivity.REFRESHTIME) {
if(mpService != null && mpService.isPlaying()) {
songTimeText.setText(makeTimeToString(mpService.getCurrentPosition()));
}
}
}
};
//按键弹起事件监听,用于调节音量
public boolean onKeyUp(int keyCode, KeyEvent keyEvent ) {
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_UP: //增大音量
if (mpService != null) {
musicVolume += 0.1;
mpService.setVolume(musicVolume, musicVolume);
songVoiceText.setText(String.valueOf((int)(musicVolume * 10)));
}
return false;
case KeyEvent.KEYCODE_DPAD_DOWN: //减少音量
if (mpService != null) {
musicVolume -= 0.1;
mpService.setVolume(musicVolume, musicVolume);
songVoiceText.setText(String.valueOf((int)musicVolume * 10));
}
return false;
}
return false;
}
/**
* 下一曲
*/
public void nextMusic() {
if ( ++ MusicListActivity.currentPosition >= cursor.getCount()) {
//当前播放的歌曲为最后一曲
//跳到第一曲
cursor.moveToFirst();
MusicListActivity.currentPosition = 0;
} else {
cursor.moveToPosition(MusicListActivity.currentPosition);
}
//得到下一首歌的文件路径
String uri = cursor.getString(
cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
mpService.setDataPath(uri);
mpService.startMusic();
}
/**
* 上一曲
* 具体参见nextMusic()的实现
*/
public void frontMusic() {
if (MusicListActivity.currentPosition ==0) {
cursor.moveToLast();
MusicListActivity.currentPosition = cursor.getCount() - 1;
} else {
cursor.moveToPosition(Mu
- 1
- 2
- 3
- 4
- 5
前往页