基于Android系统的音乐播放器设计与实现含源程序.doc
基于Android系统的音乐播放器设计与实现源程序.Project<?xml version="1.0" encoding="UTF-8"?><projectDescription><name>spMusicPlayer01</name><comment></comment><projects></projects><buildSpec><buildCommand><name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name><arguments></arguments></buildCommand><buildCommand><name>com.android.ide.eclipse.adt.PreCompilerBuilder</name><arguments></arguments></buildCommand><buildCommand><name>org.eclipse.jdt.core.javabuilder</name><arguments></arguments></buildCommand><buildCommand><name>com.android.ide.eclipse.adt.ApkBuilder</name><arguments></arguments></buildCommand></buildSpec><natures><nature>com.android.ide.eclipse.adt.AndroidNature</nature><nature>org.eclipse.jdt.core.javanature</nature></natures></projectDescription>.classpath<?xml version="1.0" encoding="UTF-8"?><classpath><classpathentry kind="src" path="src"/><classpathentry kind="src" path="gen"/><classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/><classpathentry kind="output" path="bin"/></classpath>AndroidManifest.xml<manifest xmlns:android=" package="sled.develop" android:versionCode="1" android:versionName="1.0"> <application android:icon="drawable/icon" android:label="string/app_name" ><activity android:name=".MpsPlayer" android:theme="android:style/Theme.NoTitleBar.Fullscreen" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".LocalMpsList" android:theme="android:style/Theme.Dialog" android:label="string/app_name"> </activity> <activity android:name=".ItrActivity" android:theme="android:style/Theme.Dialog" android:label="string/app_name"> </activity> <service android:name=".service.PlayerSrv"/> <service android:name=".service.ListSrv"/> </application> <uses-sdk android:minSdkVersion="8" /></manifest> LocalMpsList.javapackage sled.develop;import java.util.ArrayList;import java.util.List;import sled.develop.service.ListSrv;import sled.model.MpsIterm;import sled.utils.AdapterUtils;import sled.utils.AppConstant;import sled.utils.FileUtils;import android.app.ListActivity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.Window;import android.widget.ListView;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.SimpleAdapter;public class LocalMpsList extends ListActivityprivate List<MpsIterm> mp3s =null;private RadioGroup radiogroup ; private RadioButton single ;private RadioButton order ;private RadioButton radom ;private int mode = AppConstant.JUMP_MODE_ORDER ;Overrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubsuper.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.locallist);single = (RadioButton)findViewById(R.id.single);order = (RadioButton)findViewById(R.id.order);radom = (RadioButton)findViewById(R.id.radom);radiogroup = (RadioGroup)findViewById(R.id.menu);radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) / TODO Auto-generated method stubif(checkedId = single.getId()mode = AppConstant.JUMP_MODE_SINGLE;else if(checkedId = order.getId()mode = AppConstant.JUMP_MODE_ORDER;else if(checkedId = radom.getId()mode = AppConstant.JUMP_MODE_RADOM;);Overrideprotected void onListItemClick(ListView l, View v, int position, long id) / TODO Auto-generated method stubsuper.onListItemClick(l, v, position, id);/MpsIterm mp3=mp3s.get(position);Intent preintent = new Intent();preintent.putExtra("position", position);preintent.putExtra("cmd", AppConstant.JUMP_CMD_PREPARE);preintent.putExtra("mode", mode);preintent.setClass(LocalMpsList.this, ListSrv.class);startService(preintent);/*Intent intent=new Intent();intent.putExtra("mp3", mp3);intent.setClass(this, MpsPlayer.class);startActivity(intent);*/onStop();Overrideprotected void onResume() / TODO Auto-generated method stubsuper.onResume();updateListView();Overrideprotected void onStop() / TODO Auto-generated method stubsuper.onStop();finish();private void updateListView()FileUtils fileUtils=new FileUtils();mp3s=fileUtils.getMp3Files("mp3");if (mp3s=null)/如果本地目录找不到Mp3文件mp3s=new ArrayList<MpsIterm>();MpsIterm mp3=new MpsIterm();mp3.setName_mp3("没有Mp3文件!");mp3.setSize_mp3(0);mp3s.add(mp3);AdapterUtils au=new AdapterUtils(this,R.layout.mp3info_item,mp3s);SimpleAdapter simpleAdapter=au.buildSimpleAdapter();setListAdapter(simpleAdapter);ItrActivity.javapackage sled.develop;import android.app.Activity;import android.os.Bundle;public class ItrActivity extends ActivityOverrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.introduction);Overrideprotected void onStop() / TODO Auto-generated method stubsuper.onStop();finish();MpsPlayer.javapackage sled.develop;import sled.develop.service.ListSrv;import sled.develop.service.PlayerSrv;import sled.utils.AppConstant;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.media.AudioManager;import android.os.Bundle;import android.os.Handler;import android.view.Menu;import android.view.MenuItem;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.SeekBar;import android.widget.TextView;public class MpsPlayer extends Activity implements AppConstant private AudioManager am = null ;private IntentFilter intentFilter=null;private BroadcastReceiver receiver=null;private Intent playIntent=null;private Intent intentnext = null ;private Thread t = null ;private LinearLayout ll = null;private ImageButton last_btn = null ;private ImageButton pause_btn = null ;private ImageButton play_btn = null ;private ImageButton next_btn = null ;private ImageButton list_btn = null ;private SeekBar seekbar = null ;private TextView time_view = null ;private TextView lrc_view = null ;private TextView mp3info_view = null ;private ImageView song_img = null ;private ImageView volbar = null ;private ImageView logo = null ;private int SysMaxVolume , CurVolume;private int vol_id = R.drawable.vol_0,R.drawable.vol_1,R.drawable.vol_2,R.drawable.vol_3,R.drawable.vol_4,R.drawable.vol_5,R.drawable.vol_6,R.drawable.vol_7;private int logo_id = R.drawable.logo_0,R.drawable.logo_1,R.drawable.logo_2,;int logo_index = 0 ;float downX = 0 ;float downY = 0 ;float upX = 0 ;float upY = 0 ;float relateX ;float relateY ;long tempT = 0 ;long downT = 0 ;long moveT = 0 ;long upT = 0 ;private float CLICK_POINTOR = 80 ;/int count = 0 ;Overrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);ll = (LinearLayout)findViewById(R.id.control_view);last_btn = (ImageButton)findViewById(R.id.last);last_btn.setOnClickListener(new LastOnclickListener();pause_btn = (ImageButton)findViewById(R.id.pause);pause_btn.setOnClickListener(new PauseOnclickListener();play_btn = (ImageButton)findViewById(R.id.play);play_btn.setOnClickListener(new PlayOnclickListener();next_btn = (ImageButton)findViewById(R.id.next);next_btn.setOnClickListener(new NextOnclickListener();list_btn = (ImageButton)findViewById(R.id.list);list_btn.setOnClickListener(new ListOnclickListener();seekbar = (SeekBar)findViewById(R.id.progressbar);seekbar.setOnSeekBarChangeListener(new SeekBarListener();time_view = (TextView)findViewById(R.id.timeview);lrc_view = (TextView)findViewById(R.id.lrcview);mp3info_view = (TextView)findViewById(R.id.mp3_info);song_img = (ImageView)findViewById(R.id.img_of_the_song);volbar = (ImageView)findViewById(R.id.volfasebar);logo = (ImageView)findViewById(R.id.logo);Overrideprotected void onResume() super.onResume();am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);SysMaxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);receiver=new LrcMessBroadcastReceiver();/两个参数,一个是指明操作,一个是指明过滤registerReceiver(receiver,getIntentFilter();initPlayerSrv();initListSrv();dosomething(null,null);Override public boolean onCreateOptionsMenu(Menu menu) / TODO Auto-generated method stub menu.add(0, 1, 1, "操作说明"); menu.add(0, 2, 2, "完全退出"); return super.onCreateOptionsMenu(menu); Override public boolean onOptionsItemSelected(MenuItem item) / TODO Auto-generated method stub if(item.getItemId() = 1) Intent tintent = new Intent(); tintent.setClass(MpsPlayer.this, ItrActivity.class); startActivity(tintent); else if(item.getItemId() = 2) stopService(playIntent); stopService(intentnext); finish(); return true; Overrideprotected void onRestart() super.onRestart();protected void onPause() super.onPause();unregisterReceiver(receiver);/一些基本布局public void dosomething(String mp3info,String img)Bitmap bm ;if(mp3info = null | img = null )mp3info = "NO MUSIC SELECTED"song_img.setImageResource(R.drawable.logo_0);mp3info_view.setText(mp3info);else/System.out.println(img);mp3info = "当前播放:" + mp3info;bm = BitmapFactory.decodeFile("/sdcard/mp3/img/" + img);if(bm = null)song_img.setImageResource(R.drawable.logo_0);elsesong_img.setImageBitmap(bm);mp3info_view.setText(mp3info);/* * 一些按钮和进度条的监听方法 * author Administrator * */class LastOnclickListener implements OnClickListenerOverridepublic void onClick(View v) lastMusic();class PauseOnclickListener implements OnClickListenerOverridepublic void onClick(View v) pauseMp3();/t.start();class PlayOnclickListener implements OnClickListenerOverridepublic void onClick(View v) initPlayerSrv();playMp3();class NextOnclickListener implements OnClickListenerOverridepublic void onClick(View v) nextMusic();public void initListSrv()intentnext = new Intent();intentnext.setClass(MpsPlayer.this, ListSrv.class);public void nextMusic()intentnext.putExtra("cmd", AppConstant.JUMP_CMD_NEXT);startService(intentnext);public void lastMusic()Intent intentnext = new Intent();intentnext.putExtra("cmd", AppConstant.JUMP_CMD_LAST);intentnext.setClass(MpsPlayer.this, ListSrv.class);startService(intentnext);class SeekBarListener implements SeekBar.OnSeekBarChangeListener/进度条事件响应public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) public void onStartTrackingTouch(SeekBar arg0) public void onStopTrackingTouch(SeekBar arg0) if (seekbar.getMax()<1) return;if (playIntent=null)initPlayerSrv();playIntent.putExtra("startTime",arg0.getProgress() );playIntent.putExtra("mediaCmd", AppConstant.MEDIA_CMD_SEEKTO);startService(playIntent);if (receiver=null)receiver=new LrcMessBroadcastReceiver();/两个参数,一个是指明操作,一个是指明过滤registerReceiver(receiver,getIntentFilter();class ListOnclickListener implements OnClickListenerOverridepublic void onClick(View v) Intent intent = new Intent();intent.setClass(MpsPlayer.this, LocalMpsList.class);startActivity(intent);/* * 一些基本操作,为方便代码复用 */开始Serviceprivate void initPlayerSrv()playIntent =new Intent();playIntent.setClass(MpsPlayer.this,PlayerSrv.class);/发送播放操作给Serviceprivate void playMp3() playIntent.putExtra("mediaCmd", AppConstant.MEDIA_CMD_PLAY);startService (playIntent);/发送暂停操作给Serviceprivate void pauseMp3() playIntent.putExtra("mediaCmd", AppConstant.MEDIA_CMD_PAUSE);startService (playIntent);/得到广播的Itentprivate IntentFilter getIntentFilter()if (intentFilter=null)intentFilter=new IntentFilter();intentFilter.addAction(AppConstant.LRC_MESSAGE_ACTION);return intentFilter;/换算歌曲时间private String getTimeStr(long offsetTime,boolean isLongFormat)int remainder=0;int mode=0;remainder=(int)offsetTime) / (60 * 1000);String min=remainder +""mode=(int)offsetTime) % (60 * 1000);remainder=mode / 1000;String sec=remainder +""if (isLongFormat)mode=mode % 1000;String mill=mode/10 +""return min +":" +sec + "." +mill;elsereturn min +":" +sec;/* * 捕获广播和触屏事件的方法 * author Administrator * */更新歌词,时间,进度条等控件class LrcMessBroadcastReceiver extends BroadcastReceiverOverridepublic void onReceive(Context arg0, Intent arg1) logo.setImageResource(logo_idlogo_index+%logo_id.length);int uiCmd=arg1.getIntExtra("uiCmd", AppConstant.UI_CMD_UPDATELRC);if (uiCmd=AppConstant.UI_CMD_UPDATELRC)String lrcMess=arg1.getStringExtra("lrcMess");lrc_view.setText(lrcMess);else if (uiCmd=AppConstant.UI_CMD_UPDATEPLAYING)long currPos=arg1.getLongExtra("currPos", 0);int duration = arg1.