android 自编音乐播放器源代码

package com.android.lzx.learn; import java.io.File; import java.io.IOException; import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; import android.view.ContextMenu; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.ImageButton; import android.widget.SeekBar; import android.widget.TextView; import android.widget.AdapterView.AdapterContextMenuInfo; public class WTmusicplayer extends ListActivity { private MediaPlayer mMediaPlayer; private static final int CONMENU_PLAY = 0; private static final int CONMENU_CANCEL = 1; private static final int PLAYSTYLE_SINGLEPLAY = 0; private static final int PLAYSTYLE_SINGLECYCLE =1; private static final int PLAYSTYLE_ALLCYCLE = 2; private static final int PLAYSTYLE_RANDOM = 3; private int mPlaystyle; private int mSongID ; private boolean MusicIsBegained = false; private ArrayAdapter<String> mArrayAdapter; private ImageButton imagebuttonPlay,imagebuttonStop,imagebuttonNext,imagebuttonPrevious; private TextView mTimeTextView; //定义seekbar显示 private SeekBar mSeekBar; private Handler mHandler = new Handler(); private HandlerInvocation handlerinvocation=new HandlerInvocation(); private class HandlerInvocation implements Runnable{ public void run() { // TODO Auto-generated method stub int progressvalue=0; if (MusicIsBegained==true) { if(mMediaPlayer.getDuration()>0){ progressvalue=mSeekBar.getMax()*mMediaPlayer.getCurrentPosition()/mMediaPlayer.getDuration(); } } mSeekBar.setProgress(progressvalue); mHandler.postDelayed(handlerinvocation, 1000); } } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTimeTextView = (TextView)findViewById(R.id.TimeText);//暂时不用 mSongID = 1; mPlaystyle = PLAYSTYLE_SINGLEPLAY; InitListView(); InitMediaPlayer(); InitButtons(); InitSeekBar(); registerForContextMenu(getListView()); handlerinvocation.run(); } /** * ****************************************************************************** * name:Bill time:2010-3-29 * TODO: * ******************************************************************************** */ private void InitListView(){ mArrayAdapter = new ArrayAdapter<String>(this,R.layout.list_row); File tFile = new File("/sdcard"); File[] tFiles = tFile.listFiles(new MyFileFilter()); for(int i=0;i<tFiles.length;i++){ mArrayAdapter.add(tFiles[i].getName()); } setListAdapter(mArrayAdapter); } /** * ****************************************************************************** * name:Bill time:2010-3-30 * TODO: * ******************************************************************************** */ private void InitButtons(){ //播放按钮的处理 imagebuttonPlay = (ImageButton)findViewById(R.id.ImgBtn_Playandpause); imagebuttonPlay.setOnClickListener(new OnClickListener(){ public void onClick(View v){ try{ if(mMediaPlayer!=null){ if(MusicIsBegained==true){//是否已经开始播放 if(mMediaPlayer.isPlaying()){//已经开始且正在播放 mMediaPlayer.pause(); imagebuttonPlay.setImageResource(R.drawable.play); } else{//已经开始处于暂停 mMediaPlayer.start(); imagebuttonPlay.setImageResource(R.drawable.pause); } } else{//未开始播放 mMediaPlayer.stop(); mMediaPlayer.prepare(); mMediaPlayer.start(); MusicIsBegained=true; imagebuttonPlay.setImageResource(R.drawable.pause); } } } catch(Exception e){ e.printStackTrace(); } } }); //停止按钮的处理 imagebuttonStop = (ImageButton)findViewById(R.id.ImgBtn_Stop); imagebuttonStop.setOnClickListener(new OnClickListener(){ public void onClick(View v){ try{ if(mMediaPlayer!=null ){ mMediaPlayer.stop(); MusicIsBegained=false; imagebuttonPlay.setImageResource(R.drawable.play); } } catch(Exception e){ e.printStackTrace(); } } }); //下一首按钮的处理 imagebuttonNext =(ImageButton)findViewById(R.id.ImgBtn_Next); imagebuttonNext.setOnClickListener(new OnClickListener(){ public void onClick(View v){ try{ mSongID=(mSongID+1)%mArrayAdapter.getCount();//正循环对8取余 mHandler.removeCallbacks(handlerinvocation); mMediaPlayer.stop(); mMediaPlayer.reset(); mMediaPlayer.setDataSource(getCurrentSongString(mSongID)); mMediaPlayer.prepare(); mMediaPlayer.start(); MusicIsBegained = true; mHandler.postDelayed(handlerinvocation, 1000); imagebuttonPlay.setImageResource(R.drawable.pause); } catch(Exception e){ e.printStackTrace(); } } }); //上一首按钮的处理 imagebuttonPrevious =(ImageButton)findViewById(R.id.ImgBtn_Previous); imagebuttonPrevious.setOnClickListener(new OnClickListener(){ public void onClick(View v){ try{ mSongID=(mSongID+mArrayAdapter.getCount()-1)%mArrayAdapter.getCount();//逆循环+7取余 mHandler.removeCallbacks(handlerinvocation); mMediaPlayer.stop(); mMediaPlayer.reset(); mMediaPlayer.setDataSource(getCurrentSongString(mSongID)); mMediaPlayer.prepare(); mMediaPlayer.start(); MusicIsBegained = true; mHandler.postDelayed(handlerinvocation, 1000); imagebuttonPlay.setImageResource(R.drawable.pause); } catch(Exception e){ e.printStackTrace(); } } }); } /* (non-Javadoc) * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo) */ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, CONMENU_PLAY, 0, "Play this song"); menu.add(0, CONMENU_CANCEL, 0, "Cancel"); } /* (non-Javadoc) * @see android.app.Activity#onContextItemSelected(android.view.MenuItem) */ @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case CONMENU_PLAY: try{ AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo)item.getMenuInfo(); String songNameString = getCurrentSongString(adapterContextMenuInfo.position); mSongID = adapterContextMenuInfo.position;//歌曲的id mHandler.removeCallbacks(handlerinvocation); mMediaPlayer.stop(); mMediaPlayer.reset(); mMediaPlayer.setDataSource(songNameString); mMediaPlayer.prepare(); mMediaPlayer.start(); MusicIsBegained = true; mHandler.postDelayed(handlerinvocation, 1000); imagebuttonPlay.setImageResource(R.drawable.pause); } catch(Exception e){ e.printStackTrace(); } break; case CONMENU_CANCEL: break; default: break; } return super.onContextItemSelected(item); } /* (non-Javadoc) * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent) */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub switch (keyCode) { case KeyEvent.KEYCODE_BACK: AlertDialog.Builder alert = new AlertDialog.Builder(WTmusicplayer.this); alert.setTitle("Exit player ?"); alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub try{ mMediaPlayer.release(); mHandler.removeCallbacks(handlerinvocation); finish(); } catch(Exception e){ e.printStackTrace(); } } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); alert.show(); return false; case KeyEvent.KEYCODE_HOME: return false; default: return false; } /*return super.onKeyDown(keyCode, event);*/ } /** * ****************************************************************************** * name:Bill time:2010-3-30 * TODO: * ******************************************************************************** */ private void InitMediaPlayer(){ mMediaPlayer = new MediaPlayer(); try{ mMediaPlayer.setDataSource(getCurrentSongString(mSongID)); mMediaPlayer.prepare(); } catch(IllegalStateException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } mMediaPlayer.setOnCompletionListener( new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub try{ //mMediaPlayer.release(); switch (mPlaystyle) { case PLAYSTYLE_SINGLEPLAY: mMediaPlayer.stop(); MusicIsBegained=false; imagebuttonPlay.setImageResource(R.drawable.play); break; case PLAYSTYLE_SINGLECYCLE: mMediaPlayer.stop(); mMediaPlayer.prepare(); mMediaPlayer.start(); break; case PLAYSTYLE_ALLCYCLE: mSongID=(mSongID+1)%mArrayAdapter.getCount();//正循环对8取余 mHandler.removeCallbacks(handlerinvocation); mMediaPlayer.stop(); mMediaPlayer.reset(); mMediaPlayer.setDataSource(getCurrentSongString(mSongID)); mMediaPlayer.prepare(); mMediaPlayer.start(); mHandler.postDelayed(handlerinvocation, 1000); imagebuttonPlay.setImageResource(R.drawable.pause); break; case PLAYSTYLE_RANDOM: mSongID=(mSongID+7)%mArrayAdapter.getCount();//逆循环+7取余 mHandler.removeCallbacks(handlerinvocation); mMediaPlayer.stop(); mMediaPlayer.reset(); mMediaPlayer.setDataSource(getCurrentSongString(mSongID)); mMediaPlayer.prepare(); mMediaPlayer.start(); mHandler.postDelayed(handlerinvocation, 1000); imagebuttonPlay.setImageResource(R.drawable.pause); break; default: break; } } catch(Exception e){ e.printStackTrace(); } } } ); mMediaPlayer.setOnErrorListener( new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { // TODO Auto-generated method stub try{ mMediaPlayer.release(); } catch(Exception e){ e.printStackTrace(); } return false; } } ); } /** * ****************************************************************************** * name:Bill time:2010-3-30 * TODO: * ******************************************************************************** */ private void InitSeekBar(){ mSeekBar=(SeekBar)findViewById(R.id.SeekBar01); /*mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){ public void onProgressChanged(SeekBar mSeekBar,int progress,boolean fromtouch){ Log.v("onProgressChanged",String.valueOf(progress)+','+String.valueOf(fromtouch)); } public void onStartTrackingTouch(SeekBar mSeekBar){ Log.v("onStartTrackingTouch", String.valueOf(mSeekBar.getProgress())); } public void onStopTrackingTouch(SeekBar mSeekBar){ Log.v("onStopTrackingTouch", String.valueOf(mSeekBar.getProgress())); if (MusicIsBegained==true) { mMediaPlayer.pause(); int timespan = mMediaPlayer.getDuration(); int timerate = mSeekBar.getProgress(); mMediaPlayer.seekTo(timerate*timespan/100); mMediaPlayer.start(); } else { try { mMediaPlayer.prepare(); int timespan = mMediaPlayer.getDuration(); int timerate = mSeekBar.getProgress(); mMediaPlayer.seekTo(timerate*timespan/100); mMediaPlayer.start(); MusicIsBegained=true; } catch (Exception e) { // TODO: handle exception } } } });*/ } /* (non-Javadoc) * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu) */ @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.options_menu, menu); return super.onCreateOptionsMenu(menu); } /** * ****************************************************************************** * name:Bill time:2010-3-30 * TODO: * ******************************************************************************** */ private String getCurrentSongString(int position){ String songString = "/sdcard/" + mArrayAdapter.getItem(position).toString(); return songString; } /* (non-Javadoc) * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem) */ @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.OPTMENU_SINGLEPLAY: mPlaystyle = PLAYSTYLE_SINGLEPLAY; break; case R.id.OPTMENU_SINGLECYCLE: mPlaystyle = PLAYSTYLE_SINGLECYCLE; break; case R.id.OPTMENU_ALLCYCLE: mPlaystyle = PLAYSTYLE_ALLCYCLE; break; case R.id.OPTMENU_RANDOM: mPlaystyle = PLAYSTYLE_RANDOM; break; case R.id.OPTMENU_EXITPLAYER: mMediaPlayer.release(); mHandler.removeCallbacks(handlerinvocation); finish(); break; default: break; } return super.onOptionsItemSelected(item); } }

你可能感兴趣的:(exception,android,Random,dialog,音乐,menu)