android音樂(lè)播放器常見操作
/*變量聲明*/ private ImageButton playBtn = null;//播放、暫停 private ImageButton latestBtn = null;//上一首 private ImageButton nextButton = null;//下一首 private ImageButton forwardBtn = null;//快進(jìn) private ImageButton rewindBtn = null;//快退 private TextView playtime = null;//已播放時(shí)間 private TextView durationTime = null;//歌曲時(shí)間 private SeekBar seekbar = null;//歌曲進(jìn)度 private Handler handler = null;//用于進(jìn)度條 private Handler fHandler = null;//用于快進(jìn) private int currentPosition;//當(dāng)前播放位置 /*獲得列表傳過(guò)來(lái)的數(shù)據(jù)*/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.play); Intent intent = this.getIntent(); Bundle bundle = intent.getExtras(); _ids = bundle.getIntArray("_ids"); //獲得保存音樂(lè)文件_ID的數(shù)組 position = bundle.getInt("position"); //獲得應(yīng)該播放的音樂(lè)的號(hào)數(shù),既播放第幾首 //代碼未完,見下面的代碼 } /*初始化控件*/ playtime = (TextView)findViewById(R.id.playtime); //顯示已經(jīng)播放的時(shí)間 durationTime = (TextView)findViewById(R.id.duration); //顯示歌曲總時(shí)間 playBtn = (ImageButton)findViewById(R.id.playBtn); //開始播放、暫停播放按鈕 latestBtn = (ImageButton)findViewById(R.id.latestBtn); //播放上一首按鈕 nextButton = (ImageButton)findViewById(R.id.nextBtn); //播放下一首按鈕 forwardBtn = (ImageButton)findViewById(R.id.forwardBtn); //快進(jìn)按鈕 rewindBtn = (ImageButton)findViewById(R.id.rewindBtn); //快退按鈕 seekbar = (SeekBar)findViewById(R.id.seekbar); //播放進(jìn)度條 /*定義各控件的回調(diào)函數(shù)*/ playBtn.setOnClickListener(new View.OnClickListener() { //點(diǎn)擊“播放、暫停”按鈕時(shí)回調(diào) @Override public void onClick(View v) { if (mp.isPlaying()){ //如果正在播放則暫停 pause(); playBtn.setBackgroundResource( R.drawable.play_selecor); //更改按鍵狀態(tài)圖標(biāo) } else{ //如果沒(méi)有播放則恢復(fù)播放 play(); playBtn.setBackgroundResource( R.drawable.pause_selecor); //更改按鍵狀態(tài)圖標(biāo) } } }); latestBtn.setOnClickListener(new View.OnClickListener() {//點(diǎn)擊“播放上一首”按鈕時(shí)回調(diào) @Override public void onClick(View v) { int num = _ids.length; //獲得音樂(lè)的數(shù)目 if(position==0){ //如果已經(jīng)時(shí)第一首則播放最后一首 position=num-1; }else{ //否則播放上一首 position-=1; } int pos = _ids[position]; //得到將要播放的音樂(lè)的_ID setup(); //做播放前的準(zhǔn)備工作 play(); //開始播放 } }); nextButton.setOnClickListener(new View.OnClickListener(){//點(diǎn)擊“播放下一首”按鈕時(shí)回調(diào) @Override public void onClick(View v) { int num = _ids.length; //獲得音樂(lè)的數(shù)目 if (position==num-1){ //如果已經(jīng)是最后一首,則播放第一首 position=0; }else{ position+=1; //否則播放下一首 } int pos = _ids[position]; //得到將要播放的音樂(lè)的_ID setup(); //做播放前的準(zhǔn)備工作 play(); //開始播放 } }); forwardBtn.setOnTouchListener(new OnTouchListener() { //點(diǎn)擊“快進(jìn)”按鈕時(shí)回調(diào) @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: fHandler.post(forward); //此處使用handler對(duì)象更新進(jìn)度條 mp.pause(); //點(diǎn)擊快進(jìn)按鈕時(shí),音樂(lè)暫停播放 break; case MotionEvent.ACTION_UP: fHandler.removeCallbacks(forward); mp.start(); //松開快進(jìn)按鈕時(shí),音樂(lè)暫恢復(fù)播放 playBtn.setBackgroundResource( R.drawable.pause_selecor); break; } return false; } }); rewindBtn.setOnTouchListener(new OnTouchListener() { //點(diǎn)擊“快退”按鈕時(shí)回調(diào) @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: fHandler.post(rewind); mp.pause(); //點(diǎn)擊快退按鈕時(shí),音樂(lè)暫暫停播放 break; case MotionEvent.ACTION_UP: fHandler.removeCallbacks(rewind); mp.start(); //松開快退按鈕時(shí),音樂(lè)暫恢復(fù)播放 playBtn.setBackgroundResource( R.drawable.pause_selecor); break; } return false; } }); seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { mp.start(); //停止拖動(dòng)進(jìn)度條時(shí),音樂(lè)開始播放 } @Override public void onStartTrackingTouch(SeekBar seekBar) { mp.pause(); //開始拖動(dòng)進(jìn)度條時(shí),音樂(lè)暫停播放 } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if(fromUser){ mp.seekTo(progress); //當(dāng)進(jìn)度條的值改變時(shí),音樂(lè)播放器從新的位置開始播放 } } |
posted on 2011-04-28 20:06 haojinlian 閱讀(1308) 評(píng)論(0) 編輯 收藏