public class PlayerMusicService extends Service{ private static MediaPlayer mMediaPlayer; public static final String NEXT_MUSIC="com.zs.shao.PlayerMusicService"; private static int notificationId=0; NotificationManager manager; Notification notification; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Intent intent=new Intent(); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); notification=new Notification(); notification.icon=R.drawable.icon; notification.when=System.currentTimeMillis(); notification.setLatestEventInfo(this, "Ω静听", null, contentIntent); manager.notify(notificationId,notification); mMediaPlayer = new MediaPlayer(); //mMediaPlayer.setOnPreparedListener(); mMediaPlayer.setOnCompletionListener(new OnCompletionListener()//当一首歌播放完之后调用该方法 { @Override public void onCompletion(MediaPlayer mp) { sendBroadcast(new Intent(NEXT_MUSIC)); } }); } @Override public void onDestroy() { super.onDestroy(); manager.cancel(notificationId); System.exit(0); } public static void playMusic(String path) { try{ mMediaPlayer.reset(); mMediaPlayer.setDataSource(path); mMediaPlayer.prepare(); start(); }catch(Exception e) { e.printStackTrace(); } } public static void start() { mMediaPlayer.start(); } public static void pause() { mMediaPlayer.pause(); } public static void stop() { mMediaPlayer.stop(); } public static boolean isPlaying() { return mMediaPlayer.isPlaying(); } }
IntentFilter filter = new IntentFilter(); filter.addAction(PlayerMusicService.NEXT_MUSIC); registerReceiver(recerverInfo, filter);
class PlayerUtil { static PlayerActivity activity=new PlayerActivity(); //把文件名称放在列表里面的方法 public static ArrayList<String> musicList(ContentResolver cr,Cursor cursor) { ArrayList<String> resultList=new ArrayList<String>(); cursor=cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER); if(cursor==null) { return resultList; } else{ while(cursor.moveToNext()) { String musicName=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); resultList.add(musicName); } return resultList; } } //把歌曲时间放在列表里面的方法 public static ArrayList<String> musicTimeList(ContentResolver cr,Cursor cursor) { ArrayList<String> resultList=new ArrayList<String>(); cursor=cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER); if(cursor==null) { return resultList; } else{ while(cursor.moveToNext()) { String musicName=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); resultList.add(musicName); } return resultList; } } //把放在arraylist中的歌名放进二维数组中的方法 public static String[][] musicString(ContentResolver cr,Cursor cursor) { ArrayList<String> musicName=new ArrayList<String>(); ArrayList<String> musicTime=new ArrayList<String>(); musicName=musicList(cr,cursor);//歌曲名字 musicTime=musicTimeList(cr,cursor);//歌曲时间 String[][]result=new String[musicName.size()][]; if(musicName.size()==0) { return result; } else { for(int i=0;i<musicName.size();i++) { String[]tempCol=new String[3]; tempCol[0]=(i+1)+""; //歌曲名称 tempCol[1]=musicName.get(i); if(tempCol[1].length()>11) { tempCol[1]=tempCol[1].substring(0,11)+"..."; } //歌曲时间 if((Integer.parseInt(musicTime.get(i).trim())/1000)%60<10) { tempCol[2]=Integer.parseInt(musicTime.get(i).trim())/1000/60+":"+"0"+(Integer.parseInt(musicTime.get(i).trim())/1000)%60; } else{ tempCol[2]=Integer.parseInt(musicTime.get(i).trim())/1000/60+":"+(Integer.parseInt(musicTime.get(i).trim())/1000)%60; } result[i]=tempCol; } return result; } } }
之前说过,歌曲列表放在一个自己开发的表格里面,由于某些问题,我也不知道能不能粘贴源代码,这里就先不粘贴了,不过大家可以自己写一个,也可以用listView代替。这里只能贴图了,这是原来的图,新图还没有抓呢。
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout01" android:background="#eef5fd" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > </LinearLayout>
setContentView(R.layout.musicname); table= new Table( PlayerActivity.this, width,//表示列宽 musicname,//表示列名 height,//表头高度 fontSize,//表头字体大小 fontColor,//表头字体颜色 frameColor,//边框颜色 headColor,//表头背景颜色 oddColor,//表体单数行背景颜色 evenColor,//表体偶数行背景颜色 biaotiColor,//表体文字颜色 tableContent,//表体内容 number,//每页显示的行数 moveNumber,//每次移动的行数 isNumber//表示是否是数字串 ); LinearLayout ll=(LinearLayout)findViewById(R.id.LinearLayout01); ll.addView(table); //实现表格的监听方法 table.addTableClickListener( new TableClickListener(){ @Override public void clickItem(Table eventSource, int row) { currentMusic=row;//当前歌曲的索引等于列表的当前行数。 musicName=eventSource.tableContent[currentMusic][1].trim(); if(musicName.length()>11) { musicName=musicName.substring(0, 11)+"..."; } startPauseButton=1;//让播放按钮为播放状态 new Thread() { public void run() { playMusic(currentMusic); } }.start(); hd.sendEmptyMessage(GOPLAYMUSIC); }} );
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background" > <LinearLayout android:orientation="horizontal" android:layout_width="320dip" android:layout_height="64dip" android:layout_marginTop="5dip" > <ImageView android:id="@+id/ImageView01" android:layout_width="64dip" android:layout_height="64dip" android:layout_marginLeft="5dip" android:background="@drawable/yinyue" > </ImageView> <TextView android:id="@+id/TextView01" android:text="" android:layout_width="240dip" android:layout_height="50dip" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="15dip" > </TextView> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" > <ImageButton android:id="@+id/ImageButton01" android:layout_width="45dip" android:layout_height="45dip" android:layout_marginLeft="5dip" android:background="@drawable/start_button" > </ImageButton> <ImageButton android:id="@+id/ImageButton02" android:layout_width="45dip" android:layout_height="45dip" android:layout_marginLeft="5dip" android:background="@drawable/fron_button" > </ImageButton> <ImageButton android:id="@+id/ImageButton03" android:layout_width="45dip" android:layout_height="45dip" android:layout_marginLeft="5dip" android:background="@drawable/stop_button" > </ImageButton> <ImageButton android:id="@+id/ImageButton04" android:layout_width="45dip" android:layout_height="45dip" android:layout_marginLeft="5dip" android:background="@drawable/next_button" > </ImageButton> <ImageButton android:id="@+id/ImageButton05" android:layout_width="90dip" android:layout_height="45dip" android:layout_marginLeft="25dip" android:background="@drawable/list" > </ImageButton> </LinearLayout> </LinearLayout>
setContentView(R.layout.main); //初始默认第一首歌 musicName();//歌曲名字的方法; ImageButton startButton=(ImageButton)findViewById(R.id.ImageButton01); ImageButton frontButton=(ImageButton)findViewById(R.id.ImageButton02); ImageButton stopButton=(ImageButton)findViewById(R.id.ImageButton03); ImageButton nextButton=(ImageButton)findViewById(R.id.ImageButton04); ImageButton listButton=(ImageButton)findViewById(R.id.ImageButton05); switch(startPauseButton) {//0代表暂停状态,1代表播放状态 case 0: startButton.setBackgroundResource(R.drawable.start_button); break; case 1: startButton.setBackgroundResource(R.drawable.pause_button); break; } startButton.setOnClickListener(//开始按钮的监听 new OnClickListener() { public void onClick(View v) { ImageButton startButton=(ImageButton)findViewById(R.id.ImageButton01); if(PlayerMusicService.isPlaying()) { startPauseButton=0; PlayerMusicService.pause(); startButton.setBackgroundResource(R.drawable.start_button); } else { startPauseButton=1; if(clicknumber==0) { new Thread() { public void run() { playMusic(currentMusic); } }.start(); musicName();//歌曲名字的方法; startButton.setBackgroundResource(R.drawable.pause_button); } else if(clicknumber>0) { PlayerMusicService.start(); musicName();//歌曲名字的方法 startButton.setBackgroundResource(R.drawable.pause_button); } } clicknumber++;// 点击次数增加 } } ); frontButton.setOnClickListener(//上一首歌按钮监听器 new OnClickListener() { @Override public void onClick(View v) { clicknumber++;// 点击次数增加 ImageButton startButton=(ImageButton)findViewById(R.id.ImageButton01); frontMusic(); musicName();//歌曲名字的方法 startButton.setBackgroundResource(R.drawable.pause_button); } }); stopButton.setOnClickListener(//停止按钮监听器 new OnClickListener(){ @Override public void onClick(View v) { clicknumber=0;//点击次数设为0 PlayerMusicService.stop(); ImageButton startButton=(ImageButton)findViewById(R.id.ImageButton01); startButton.setBackgroundResource(R.drawable.start_button); TextView nameTV=(TextView)findViewById(R.id.TextView01); nameTV.setText(""); } }); nextButton.setOnClickListener(//下一首歌按钮监听器 new OnClickListener(){ @Override public void onClick(View v) { clicknumber++;// 点击次数增加 ImageButton startButton=(ImageButton)findViewById(R.id.ImageButton01); nextMusic(); musicName();//歌曲名字的方法 startButton.setBackgroundResource(R.drawable.pause_button); } }); listButton.setOnClickListener(//播放列表按钮监听器 new OnClickListener(){ @Override public void onClick(View v) { clicknumber++;// 点击次数增加 hd.sendEmptyMessage(GOMUSCILIST); } });
cursor=cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
//播放相应索引歌曲的方法 public void playMusic(int index) { cursor.moveToPosition(index); String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); PlayerMusicService.playMusic(path); } public void musicName()//设置歌曲名字的方法 { TextView nameTV=(TextView)findViewById(R.id.TextView01); nameTV.setTextColor(Color.BLUE); nameTV.setTextSize(20); if(tableContent.length>0) { musicName=tableContent[currentMusic][1].trim(); if(musicName.length()>11) { musicName=musicName.substring(0, 11)+"..."; } nameTV.setText(musicName); } } public void nextMusic()//播放下一首歌曲 { if(++currentMusic>tableContent.length-1) { currentMusic=0; new Thread() { public void run() { playMusic(currentMusic); } }.start(); }else { new Thread() { public void run() { playMusic(currentMusic); } }.start(); } } public void frontMusic()//播放上一首播个歌的方法 { if(--currentMusic<0) { currentMusic=tableContent.length-1; new Thread() { public void run() { playMusic(currentMusic); } }.start(); }else { new Thread() { public void run() { playMusic(currentMusic); } }.start(); } }