Android的第三次作业
- 实现的功能
- 实现播放,暂停,停止,播放上一首,下一首功能
- 显示播放列表
- 至少可以播放3首歌曲
- 博客作业要求
对音乐播放器界面截
視頻链接:
https://v.youku.com/v_show/id_XMzk5NjkwNjYyMA==.html?x&sharefrom=android&sharekey=be3737a455bfef9ef706922bba271a823
功能的代码:
public class MainActivity extends Activity implements OnClickListener{
public static final String TAG = "om.example.administrator.musicplayer.MAIN_ACTIVITY";
public static SharedPreferences.Editor editor;//保存播放模式
private int[] modepic = {R.drawable.normal,R.drawable.repeat,R.drawable.single};
private ListView lvSongs;
private SeekBar pbDuration;
private TextView tvCurrentMusic;
private List<MusicInfo> musicList;
private int currentMusic;
private int currentPosition;
private int currentposition=-1;
private int currentMax;
private Button btnStartStop;
private Button btnNext;
private Button btnPrevious;
private Button btnMode;
private Intent intent = null;
private boolean isplay=false;
private ProgressReceiver progressReceiver;
private NatureBinder natureBinder;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
natureBinder = (NatureBinder) service;
}
};
private void connectToNatureService(){
Intent intent = new Intent(MainActivity.this, NatureService.class);
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//Log.v(TAG, "OnCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MusicLoader musicLoader = MusicLoader.instance(getContentResolver());
musicList = musicLoader.getMusicList();
connectToNatureService();
initComponents();
}
public void onResume(){
//Log.v(TAG, "OnResume register Progress Receiver");
super.onResume();
registerReceiver();
if(natureBinder != null){
if(natureBinder.isPlaying()){
btnStartStop.setBackgroundResource(R.drawable.pause);
}else{
btnStartStop.setBackgroundResource(R.drawable.play);
}
natureBinder.notifyActivity();
}
}
public void onPause(){
super.onPause();
unregisterReceiver(progressReceiver);
}
public void onStop(){
super.onStop();
}
public void onDestroy(){
super.onDestroy();
if(natureBinder != null){
unbindService(serviceConnection);
}
}
private void initComponents(){
pbDuration = (SeekBar) findViewById(R.id.pbDuration);
pbDuration.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) {
if(fromUser){
natureBinder.changeProgress(progress);
}
}
});
tvCurrentMusic = (TextView) findViewById(R.id.tvCurrentMusic);
btnStartStop = (Button)findViewById(R.id.btnStartStop);
btnStartStop.setOnClickListener(this);
btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
btnPrevious = (Button) findViewById(R.id.btnPrevious);
btnPrevious.setOnClickListener(this);
btnMode = (Button) findViewById(R.id.btnMode);
btnMode.setOnClickListener(this);
MusicAdapter adapter = new MusicAdapter();
lvSongs = (ListView) findViewById(R.id.lvSongs);
lvSongs.setAdapter(adapter);
lvSongs.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
currentMusic = position;
natureBinder.startPlay(currentMusic,0);
if(natureBinder.isPlaying()){
btnStartStop.setBackgroundResource(R.drawable.pause);
}
}
});
}
private void registerReceiver(){
progressReceiver = new ProgressReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(NatureService.ACTION_UPDATE_PROGRESS);
intentFilter.addAction(NatureService.ACTION_UPDATE_DURATION);
intentFilter.addAction(NatureService.ACTION_UPDATE_CURRENT_MUSIC);
registerReceiver(progressReceiver, intentFilter);
}
class MusicAdapter extends BaseAdapter{
@Override
public int getCount() {
return musicList.size();
}
@Override
public Object getItem(int position) {
return musicList.get(position);
}
@Override
public long getItemId(int position) {
return musicList.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null){
convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.music_item, null);
ImageView pImageView = (ImageView) convertView.findViewById(R.id.albumPhoto);
TextView pTitle = (TextView) convertView.findViewById(R.id.title);
TextView pDuration = (TextView) convertView.findViewById(R.id.duration);
TextView pArtist = (TextView) convertView.findViewById(R.id.artist);
viewHolder = new ViewHolder(pImageView, pTitle, pDuration, pArtist);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.imageView.setImageResource(R.drawable.audio);
viewHolder.title.setText(musicList.get(position).getTitle());
viewHolder.duration.setText(FormatHelper.formatDuration(musicList.get(position).getDuration()));
viewHolder.artist.setText(musicList.get(position).getArtist());
return convertView;
}
}
class ViewHolder{
public ViewHolder(ImageView pImageView, TextView pTitle, TextView pDuration, TextView pArtist){
imageView = pImageView;
title = pTitle;
duration = pDuration;
artist = pArtist;
}
ImageView imageView;
TextView title;
TextView duration;
TextView artist;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStartStop:
play(currentMusic,R.id.btnStartStop);
break;
case R.id.btnNext:
if(currentMusic<musicList.size())
natureBinder.toNext();
if(currentMusic==musicList.size()-1)
natureBinder.startPlay(currentMusic,0);
break;
case R.id.btnPrevious:
if(currentMusic!=0)
natureBinder.toPrevious();
if(currentMusic==0)
natureBinder.startPlay(musicList.size()-1,currentMusic);
break;
case R.id.btnMode:
if(clicktimes==0) {
natureBinder.changeMode();
btnMode.setBackgroundResource(R.drawable.normal);
}else if( clicktimes==1)
{
natureBinder.changeMode();
btnMode.setBackgroundResource(R.drawable.single);
}else
{
natureBinder.changeMode();
btnMode.setBackgroundResource(R.drawable.repeat);
}
break;
}
}
private void play(int position, int resId){
if(natureBinder.isPlaying()){
natureBinder.stopPlay();
btnStartStop.setBackgroundResource(R.drawable.play);
}else{
natureBinder.startPlay(position,currentPosition);
btnStartStop.setBackgroundResource(R.drawable.pause);
}
}
private void play(int position)
{
natureBinder.startPlay(position,currentPosition);
}
private int clicktimes = 0;
public void changeMode(View view) {
switch (clicktimes){
case 0://随机 --> 顺序
clicktimes++;
changeMode(clicktimes);
btnMode.setBackgroundResource(R.drawable.normal);
break;
case 1://顺序 --> 单曲
clicktimes++;
changeMode(clicktimes);
btnMode.setBackgroundResource(R.drawable.single);
break;
case 2://单曲 --> 随机
clicktimes = 0;
changeMode(clicktimes);
btnMode.setBackgroundResource(R.drawable.repeat);
break;
default:
break;
}
}
private void changeMode(int playmode) {
editor.putInt("play_mode",playmode).commit();
btnMode.setBackgroundResource(modepic[playmode]);
}
class ProgressReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(NatureService.ACTION_UPDATE_PROGRESS.equals(action)){
int progress = intent.getIntExtra(NatureService.ACTION_UPDATE_PROGRESS, 0);
if(progress > 0){
currentPosition = progress; // Remember the current position
pbDuration.setProgress(progress / 1000);
}
}else if(NatureService.ACTION_UPDATE_CURRENT_MUSIC.equals(action)){
currentMusic = intent.getIntExtra(NatureService.ACTION_UPDATE_CURRENT_MUSIC, 0);
tvCurrentMusic.setText(musicList.get(currentMusic).getTitle());
}else if(NatureService.ACTION_UPDATE_DURATION.equals(action)){
currentMax = intent.getIntExtra(NatureService.ACTION_UPDATE_DURATION, 0);
int max = currentMax / 1000;
pbDuration.setMax(currentMax / 1000);
}
}
}
}
代码链接:https://git.dev.tencent.com/zyx_a935516c0e34dddd/music.git
apk地址:https://git.dev.tencent.com/zyx_a935516c0e34dddd/music.git