天天看点

最简单的音乐播放器,实现播放器基本功能(一)

首先,这个播放器不是我想的,我是根据这个作者的博客教程来写的咯。。。也算是自己想啦,附上链接 http://blog.csdn.net/wwj_748/article/details/8899699

第一篇写的这些内容很简单,跟链接作者写的几乎一样。

他写的比较复杂,有些实现方式也不一样,所以自己写个博客记录一下...O(∩_∩)O~

这个播放器很简单,如果你是什么Api都不知道,看了这个两天就写完了,你可以边看边查相关的API是干嘛用的,如果之前都了解了相关的API,那么一天就能编完。

做一个播放器的大致思路是什么呢?播放器当然是要有播放的功能咯,这是我在废话。这个播放器是不带下载功能的,所以肯定是从你手机中获取已经存在的音乐。于是第一步就是获取手机中的音乐信息,这个跟播放器的界面没什么联系,那你可以把它写成一个工具类,叫做GetMusicListUtil,专门用来获取手机音乐信息的类。 上代码:

public class GetMusicListUtil {

	/**
	 * 从手机中得到所有的音乐,放在list中保存
	 * 
	 * @param context
	 * @return
	 * @author Yang
	 */
	public static List<Mp3Info> getMusicInfos(Context context)
	{
		Cursor cursor = context.getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		List<Mp3Info> musicInfos = new ArrayList<Mp3Info>();
		for(int i=0; i<cursor.getCount(); i++)
		{
			cursor.moveToNext();
			Mp3Info music = new Mp3Info();
			long musicId = cursor.getLong(cursor
					.getColumnIndex(MediaStore.Audio.Media._ID));
			String musicTitle = cursor.getString(cursor
					.getColumnIndex(MediaStore.Audio.Media.TITLE));
			String musicArtist = cursor.getString(cursor
					.getColumnIndex(MediaStore.Audio.Media.ARTIST));
			long musicTime = cursor.getLong(cursor  
                    .getColumnIndex(MediaStore.Audio.Media.DURATION));          //时长  
            long musicSize = cursor.getLong(cursor  
                    .getColumnIndex(MediaStore.Audio.Media.SIZE));              //文件大小  
            String musicUrl = cursor.getString(cursor  
                    .getColumnIndex(MediaStore.Audio.Media.DATA));              //文件路径  
            int isMusic = cursor.getInt(cursor  
                    .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
           //只把音乐添加到集合中
            if(isMusic != 0)
            {
            	music.setId(musicId);
            	music.setArtist(musicArtist);
            	music.setDuration(musicTime);
            	music.setSize(musicSize);
            	music.setUrl(musicUrl);
            	music.setTitle(musicTitle);
            	musicInfos.add(music);
            }
            
		}
		return musicInfos;
	}
	/**
	 * 将因音乐的时长由毫秒转换成分:秒的格式
	 * 
	 * @param time
	 * @return
	 * @author Yang
	 */
	public static String formatTime(long time) {  
        String min = time / (1000 * 60) + "";  
        String sec = time % (1000 * 60) + "";  
        if (min.length() < 2) {  
            min = "0" + time / (1000 * 60) + "";  
        } else {  
            min = time / (1000 * 60) + "";  
        }  
        if (sec.length() == 4) {  
            sec = "0" + (time % (1000 * 60)) + "";  
        } else if (sec.length() == 3) {  
            sec = "00" + (time % (1000 * 60)) + "";  
        } else if (sec.length() == 2) {  
            sec = "000" + (time % (1000 * 60)) + "";  
        } else if (sec.length() == 1) {  
            sec = "0000" + (time % (1000 * 60)) + "";  
        }  
        return min + ":" + sec.trim().substring(0, 2);  
    }
	
	/**
	 * 将List<Mp3Info>转换成List<HashMap<String,String>>的格式
	 * 为了给SimpleAdapter填充数据
	 * 
	 * @param musicInfos
	 * @return
	 * @author Yang
	 * 
	 */
	public static List<HashMap<String,String>> getMusicHashMaps(
			List<Mp3Info> musicInfos)
	{
		List<HashMap<String,String>> musicHashMaps = 
				new ArrayList<HashMap<String,String>>();
		for(Iterator<Mp3Info> iterator = musicInfos.iterator();iterator.hasNext();)
		{
			Mp3Info music = iterator.next();
			HashMap<String,String> map = new HashMap<String,String>();
			map.put("title", music.getTitle());
			map.put("artist", music.getArtist());
			map.put("size", String.valueOf(music.getSize()));
			map.put("time", formatTime(music.getDuration()));
			map.put("url", music.getUrl());
			musicHashMaps.add(map);
		}
		return musicHashMaps;
	}
}
           

相信大家都能看懂,Cursor对象进行查询,然后存到一个Mp3Info类型的List中,之所以下面还写了一个List<HashMap<String,String>>进行保存,就是为了给SimpleAdapter用的,这个adapter给listView填充数据。

这个和原作者写的没什么区别,另外其中的Mp3Info是一个javabean,如下所示:

package org.com.ViPlayer;

public class Mp3Info
{
	public long id;
	public String title;
	public String artist;
	public long duration;
	public long size;
	public String url;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getArtist() {
		return artist;
	}
	public void setArtist(String artist) {
		this.artist = artist;
	}
	public long getDuration() {
		return duration;
	}
	public void setDuration(long duration) {
		this.duration = duration;
	}
	public long getSize() {
		return size;
	}
	public void setSize(long size) {
		this.size = size;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	
}
           

有了音乐的信息之后,我们可以开始编辑界面了,我的界面里面的那些个图片也是在原创作者页面下载的。但是里面的模式稍微有些不一样,被我给简化了。。。

一共只用到了这么多图片:背景,播放按钮图,下一首按钮图,前一首按钮图,音乐模式按钮图,音乐小图标。

最简单的音乐播放器,实现播放器基本功能(一)
最简单的音乐播放器,实现播放器基本功能(一)
最简单的音乐播放器,实现播放器基本功能(一)
最简单的音乐播放器,实现播放器基本功能(一)
最简单的音乐播放器,实现播放器基本功能(一)
最简单的音乐播放器,实现播放器基本功能(一)
最简单的音乐播放器,实现播放器基本功能(一)
最简单的音乐播放器,实现播放器基本功能(一)

然后就是主界面xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bk1"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <LinearLayout
        android:id="@+id/handleButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

        <Button
            android:id="@+id/repeat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_alignParentLeft="true"
            android:background="@drawable/repeat_none" />
        <Button
            android:id="@+id/previous"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_toRightOf="@+id/repeat"
            android:background="@drawable/previous" />

        <Button
            android:id="@+id/playMusic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_toRightOf="@+id/previous"
            android:background="@drawable/play" />

        <Button
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_toRightOf="@+id/playMusic"
            android:background="@drawable/next" />
        
        <!-- <Button
            android:id="@+id/randomMode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_toRightOf="@+id/next"
            android:background="@drawable/random1" /> -->
    </LinearLayout>

    <ListView
        android:id="@+id/musicList"
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:layout_below="@+id/handleButton" >
	</ListView>
	
    <RelativeLayout 
        android:id="@+id/songInfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/musicList"
        android:layout_alignParentBottom="true"
        >

        <ImageView
            android:id="@+id/musicAlbum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:src="@drawable/viplayer"/>

		<RelativeLayout
		    android:id="@+id/musicInfo"
		    android:layout_width="match_parent"
		    android:layout_height="match_parent"
		    android:layout_alignTop="@+id/musicAlbum"
		    android:layout_toRightOf="@+id/musicAlbum" >

		    <TextView 
            android:id="@+id/titleInfo"
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:layout_alignParentLeft="true"
        	android:text=""
        	android:textSize="20sp"
        	android:textColor="@color/white"
           		 />
	        <TextView 
	            android:id="@+id/artist"
	            android:layout_width="wrap_content"
	        	android:layout_height="wrap_content"
	        	android:layout_alignParentLeft="true"
	        	android:layout_below="@+id/titleInfo"
	        	android:text=""
	        	android:textSize="18sp"
	        	android:textColor="@color/white"
	            />
		</RelativeLayout> 
		<!-- <Button 
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:layout_alignParentBottom="true"
		    android:layout_alignParentRight="true"
		    android:background="@drawable/category"
		    /> -->
    </RelativeLayout>
</RelativeLayout>
           

效果是这样子的:    

最简单的音乐播放器,实现播放器基本功能(一)

最上面放了四个按钮分别是 音乐模式、上一首、播放、下一首。这个界面就生成了,其中用到了一个Color.xml的文件,因为我的背景是黑色的,所以显示文字信息要用白色这种浅色。直接给一个Color文件,放在values文件下就行。这里面包含了不知道多少种颜色,请任意Get!

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<color name="white">#FFFFFF</color><!--白色 -->
	<color name="ivory">#FFFFF0</color><!--象牙色 -->
	<color name="lightyellow">#FFFFE0</color><!--亮黄色-->
	<color name="yellow">#FFFF00</color><!--黄色 -->
	<color name="snow">#FFFAFA</color><!--雪白色 -->
	<color name="floralwhite">#FFFAF0</color><!--花白色 -->
	<color name="lemonchiffon">#FFFACD</color><!--柠檬绸色 -->
	<color name="cornsilk">#FFF8DC</color><!--米绸色 -->
	<color name="seashell">#FFF5EE</color><!--海贝色 -->
	<color name="lavenderblush">#FFF0F5</color><!--淡紫红 -->
	<color name="papayawhip">#FFEFD5</color><!--番木色 -->
	<color name="blanchedalmond">#FFEBCD</color><!--白杏色 -->
	<color name="mistyrose">#FFE4E1</color><!--浅玫瑰色 -->
	<color name="bisque">#FFE4C4</color><!--桔黄色 -->
	<color name="moccasin">#FFE4B5</color><!--鹿皮色 -->
	<color name="navajowhite">#FFDEAD</color><!--纳瓦白 -->
	<color name="peachpuff">#FFDAB9</color><!--桃色 -->
	<color name="gold">#FFD700</color><!--金色 -->
	<color name="pink">#FFC0CB</color><!--粉红色 -->
	<color name="lightpink">#FFB6C1</color><!--亮粉红色-->
	<color name="orange">#FFA500</color><!--橙色 -->
	<color name="lightsalmon">#FFA07A</color><!--亮肉色 -->
	<color name="darkorange">#FF8C00</color><!--暗桔黄色 -->
	<color name="coral">#FF7F50</color><!--珊瑚色 -->
	<color name="hotpink">#FF69B4</color><!--热粉红色 -->
	<color name="tomato">#FF6347</color><!--西红柿色 -->
	<color name="orangered">#FF4500</color><!--红橙色 -->
	<color name="deeppink">#FF1493</color><!--深粉红色 -->
	<color name="fuchsia">#FF00FF</color><!--紫红色 -->
	<color name="magenta">#FF00FF</color><!--红紫色 -->
	<color name="red">#FF0000</color><!--红色 -->
	<color name="oldlace">#FDF5E6</color><!--老花色 -->
	<color name="lightgoldenrodyellow">#FAFAD2</color><!--亮金黄色 -->
	<color name="linen">#FAF0E6</color><!--亚麻色 -->
	<color name="antiquewhite">#FAEBD7</color><!--古董白 -->
	<color name="salmon">#FA8072</color><!--鲜肉色 -->
	<color name="ghostwhite">#F8F8FF</color><!--幽灵白 -->
	<color name="mintcream">#F5FFFA</color><!--薄荷色 -->
	<color name="whitesmoke">#F5F5F5</color><!--烟白色 -->
	<color name="beige">#F5F5DC</color><!--米色 -->
	<color name="wheat">#F5DEB3</color><!--浅黄色 -->
	<color name="sandybrown">#F4A460</color><!--沙褐色-->
	<color name="azure">#F0FFFF</color><!--天蓝色 -->
	<color name="honeydew">#F0FFF0</color><!--蜜色 -->
	<color name="aliceblue">#F0F8FF</color><!--艾利斯兰 -->
	<color name="khaki">#F0E68C</color><!--黄褐色 -->
	<color name="lightcoral">#F08080</color><!--亮珊瑚色 -->
	<color name="palegoldenrod">#EEE8AA</color><!--苍麒麟色 -->
	<color name="violet">#EE82EE</color><!--紫罗兰色 -->
	<color name="darksalmon">#E9967A</color><!--暗肉色 -->
	<color name="lavender">#E6E6FA</color><!--淡紫色 -->
	<color name="lightcyan">#E0FFFF</color><!--亮青色 -->
	<color name="burlywood">#DEB887</color><!--实木色 -->
	<color name="plum">#DDA0DD</color><!--洋李色 -->
	<color name="gainsboro">#DCDCDC</color><!--淡灰色 -->
	<color name="crimson">#DC143C</color><!--暗深红色 -->
	<color name="palevioletred">#DB7093</color><!--苍紫罗兰色 -->
	<color name="goldenrod">#DAA520</color><!--金麒麟色 -->
	<color name="orchid">#DA70D6</color><!--淡紫色 -->
	<color name="thistle">#D8BFD8</color><!--蓟色 -->
	<color name="lightgray">#D3D3D3</color><!--亮灰色 -->
	<color name="lightgrey">#D3D3D3</color><!--亮灰色 -->
	<color name="tan">#D2B48C</color><!--茶色 -->
	<color name="chocolate">#D2691E</color><!--巧可力色 -->
	<color name="peru">#CD853F</color><!--秘鲁色 -->
	<color name="indianred">#CD5C5C</color><!--印第安红 -->
	<color name="mediumvioletred">#C71585</color><!--中紫罗兰色 -->
	<color name="silver">#C0C0C0</color><!--银色 -->
	<color name="darkkhaki">#BDB76B</color><!--暗黄褐色 -->
	<color name="rosybrown">#BC8F8F</color> <!--褐玫瑰红 -->
	<color name="mediumorchid">#BA55D3</color><!--中粉紫色 -->
	<color name="darkgoldenrod">#B8860B</color><!--暗金黄色 -->
	<color name="firebrick">#B22222</color><!--火砖色 -->
	<color name="powderblue">#B0E0E6</color><!--粉蓝色 -->
	<color name="lightsteelblue">#B0C4DE</color><!--亮钢兰色 -->
	<color name="paleturquoise">#AFEEEE</color><!--苍宝石绿 -->
	<color name="greenyellow">#ADFF2F</color><!--黄绿色 -->
	<color name="lightblue">#ADD8E6</color><!--亮蓝色 -->
	<color name="darkgray">#A9A9A9</color><!--暗灰色 -->
	<color name="darkgrey">#A9A9A9</color><!--暗灰色 -->
	<color name="brown">#A52A2A</color><!--褐色 -->
	<color name="sienna">#A0522D</color><!--赭色 -->
	<color name="darkorchid">#9932CC</color><!--暗紫色-->
	<color name="palegreen">#98FB98</color><!--苍绿色 -->
	<color name="darkviolet">#9400D3</color><!--暗紫罗兰色 -->
	<color name="mediumpurple">#9370DB</color><!--中紫色 -->
	<color name="lightgreen">#90EE90</color><!--亮绿色 -->
	<color name="darkseagreen">#8FBC8F</color><!--暗海兰色 -->
	<color name="saddlebrown">#8B4513</color><!--重褐色 -->
	<color name="darkmagenta">#8B008B</color><!--暗洋红 -->
	<color name="darkred">#8B0000</color><!--暗红色 -->
	<color name="blueviolet">#8A2BE2</color><!--紫罗兰蓝色 -->
	<color name="lightskyblue">#87CEFA</color><!--亮天蓝色 -->
	<color name="skyblue">#87CEEB</color><!--天蓝色 -->
	<color name="gray">#808080</color><!--灰色 -->
	<color name="grey">#808080</color><!--灰色 -->
	<color name="olive">#808000</color><!--橄榄色 -->
	<color name="purple">#800080</color><!--紫色 -->
	<color name="maroon">#800000</color><!--粟色 -->
	<color name="aquamarine">#7FFFD4</color><!--碧绿色-->
	<color name="chartreuse">#7FFF00</color><!--黄绿色 -->
	<color name="lawngreen">#7CFC00</color><!--草绿色 -->
	<color name="mediumslateblue">#7B68EE</color><!--中暗蓝色 -->
	<color name="lightslategray">#778899</color><!--亮蓝灰 -->
	<color name="lightslategrey">#778899</color><!--亮蓝灰 -->
	<color name="slategray">#708090</color><!--灰石色 -->
	<color name="slategrey">#708090</color><!--灰石色 -->
	<color name="olivedrab">#6B8E23</color><!--深绿褐色 -->
	<color name="slateblue">#6A5ACD</color><!--石蓝色 -->
	<color name="dimgray">#696969</color><!--暗灰色 -->
	<color name="dimgrey">#696969</color><!--暗灰色 -->
	<color name="mediumaquamarine">#66CDAA</color><!--中绿色 -->
	<color name="cornflowerblue">#6495ED</color><!--菊兰色 -->
	<color name="cadetblue">#5F9EA0</color><!--军兰色 -->
	<color name="darkolivegreen">#556B2F</color><!--暗橄榄绿  -->
	<color name="indigo">#4B0082</color><!--靛青色 -->
	<color name="mediumturquoise">#48D1CC</color><!--中绿宝石 -->
	<color name="darkslateblue">#483D8B</color><!--暗灰蓝色 -->
	<color name="steelblue">#4682B4</color><!--钢兰色 -->
	<color name="royalblue">#4169E1</color><!--皇家蓝 -->
	<color name="turquoise">#40E0D0</color><!--青绿色 -->
	<color name="mediumseagreen">#3CB371</color><!--中海蓝 -->
	<color name="limegreen">#32CD32</color><!--橙绿色 -->
	<color name="darkslategray">#2F4F4F</color><!--暗瓦灰色 -->
	<color name="darkslategrey">#2F4F4F</color><!--暗瓦灰色 -->
	<color name="seagreen">#2E8B57</color><!--海绿色 -->
	<color name="forestgreen">#228B22</color><!--森林绿 -->
	<color name="lightseagreen">#20B2AA</color><!--亮海蓝色 -->
	<color name="dodgerblue">#1E90FF</color><!--闪兰色 -->
	<color name="midnightblue">#191970</color><!--中灰兰色 -->
	<color name="aqua">#00FFFF</color><!--浅绿色 -->
	<color name="cyan">#00FFFF</color><!--青色 -->
	<color name="springgreen">#00FF7F</color><!--春绿色-->
	<color name="lime">#00FF00</color><!--酸橙色 -->
	<color name="mediumspringgreen">#00FA9A</color><!--中春绿色 -->
	<color name="darkturquoise">#00CED1</color><!--暗宝石绿 -->
	<color name="deepskyblue">#00BFFF</color><!--深天蓝色 -->
	<color name="darkcyan">#008B8B</color><!--暗青色 -->
	<color name="teal">#008080</color><!--水鸭色 -->
	<color name="green">#008000</color><!--绿色 -->
	<color name="darkgreen">#006400</color><!--暗绿色 -->
	<color name="blue">#0000FF</color><!--蓝色 -->
	<color name="mediumblue">#0000CD</color><!--中兰色 -->
	<color name="darkblue">#00008B</color><!--暗蓝色 -->
	<color name="navy">#000080</color><!--海军色 -->
	<color name="black">#000000</color><!--黑色 -->

</resources>
           

这个播放器是不需要任何额外权限的。所以Manifest中除了Activity的信息不需要别的注册,Activity是自动生成的,所以就不用管咯......真开心...

现在我们生成了主界面,其实这个播放器因为很简单,所以只有这一个界面,另外有一个布局文件,是为了给主界面的音乐list布局的,名字叫做musi_list_item。里面只有音乐名字,歌手和时间这三项。下面就是这个xml文件咯:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
    android:id="@+id/listView"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#252525" />
    <TextView 
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:textSize="20sp"
    android:text=""
    	/>

 	<TextView
     android:id="@+id/Artist"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
	 android:layout_below="@+id/title"
     android:text=""
     android:textColor="@color/white"
     android:textSize="18sp"
     />
    <TextView
        android:id="@+id/duration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/Artist"
        android:text=""
        android:textSize="18sp"
        android:textColor="@color/white"/>
</RelativeLayout>
           

好了,目前为止,已经把最简单的工作都做完了,接下来就开始正式着手做功能了。。。

首先要把我们获取到的音乐信息显示到主界面的listView控件中,只要在Activity的onCreate函数中使用Adapter对listView进行数据填充:

private SimpleAdapter adapter;

	private List<Mp3Info> musicInfo;
	private List<HashMap<String,String>> musicList;
           
musicInfo = GetMusicListUtil.getMusicInfos(MainActivity.this);
		musicList = GetMusicListUtil.getMusicHashMaps(musicInfo);
		adapter = new SimpleAdapter(MainActivity.this, musicList, R.layout.music_list_item, 
				new String[]{"title","artist","time"}, 
				new int[]{R.id.title, R.id.Artist, R.id.duration});
		listView.setAdapter(adapter);
           

经过这样的赋值,就可以把音乐的信息显示在list列表中了。我们这里用到的SimpleAdapter是最简单的一种,通常做项目的时候会自定义一个Adapter,继承自BaseAdapter,可以在其中进行一些操作,用的最多。比如想在listView列表中加入图片信息,建议自定义一个Adapter然后采用异步加载的方式。废话不多说,接着做。

现在我们已经把列表显示出来了,那么下一步就是点击一下列表,然后实现播放的功能,到这里为止,和原作者采用的方法都一样:

给listView添加OnItemClick事件:

listView.setOnItemClickListener(new MusicItemClickListener());
           

这个MusicItemClickLitener是自定义的一个类:

public class MusicItemClickListener implements OnItemClickListener
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if(musicInfo != null)
			{
				play.setBackgroundResource(R.drawable.pause);
                                Intent intent = new Intent();
		                intent.setAction("org.com.ViPlayer.MUSIC_SERVICE");
		                intent.putExtra("url", music.getUrl());
		                intent.putExtra("msg", 0);
		                startService(intent);
			}
		}
		
	}
           

可以看出来,在item的点击事件中,启动播放音乐的服务,当点击音乐开始播放的时候,将播放按钮变成暂停按钮的图标,其中intent携带的msg这个信息传递的是0,暂定0为播放的意思,这个服务是单独写的一个类,叫做PlayerService,它继承自Service,但是如何知道上面Activity启动的是哪一个service呢,那就是intent.setAction("这个service的名字")这句话起的作用,我们能根据这个名字来调用service,就必须在AndroidManifest中注册这个service:

<service android:name="org.com.ViPlayer.PlayerService">
			<intent-filter>
			    <action android:name="org.com.ViPlayer.MUSIC_SERVICE"/>
			</intent-filter>            
        </service>
           

把这个service服务放在application中就行,service中的android:name=“service在包中的位置”,下面的action中的android:name才是真正引导service行动的指标。

接下来我们实现这个PlayerService类:

package org.com.ViPlayer;

import java.util.List;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.IBinder;

public class PlayerService extends Service {
	private MediaPlayer mediaPlayer;
	//播放文件url
	private String path;
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		//从上级页面取到音乐源
		path = intent.getStringExtra("url");
		int msg = intent.getIntExtra("msg", 0);

		if(msg == 0)
		{
			
		    //从头开始播放
			play(0);
		}
		else if(msg == 1)
		{
			stop();
		}
		else if(msg == 2)
		{
			pause();
		}
		return super.onStartCommand(intent, flags, startId);
	}

	/**
	 * 播放音乐
	 * 
	 * @param position
	 */
	public void play(int position)
	{
		try{
			mediaPlayer.reset();
			mediaPlayer.setDataSource(path);
			mediaPlayer.prepare();
			//注册监听器
			mediaPlayer.setOnPreparedListener(new PreparedListener(position));
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 暂停音乐
	 * 每次暂停isPause标识设为false并得到当前的播放位置
	 */
	public void pause()
	{
		if(mediaPlayer != null)
		{
			mediaPlayer.pause();
		}
	}
	/**
	 * 停止音乐
	 * 这个stop和pause不一样,stop之后再想播放音乐要重新prepare一次
	 * 
	 */
	public void stop()
	{
		if(mediaPlayer != null)
		{
			mediaPlayer.stop();
			try
			{
				mediaPlayer.prepare();
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onDestroy() {
		if(mediaPlayer != null)
		{
			mediaPlayer.stop();
			mediaPlayer.release();
		}
	}
	
	private final class PreparedListener implements OnPreparedListener
	{
		private int position;
		public PreparedListener(int positon)
		{
			this.position = positon;
		}
		@Override
		public void onPrepared(MediaPlayer mp) {
			mediaPlayer.start();
			//如果不是从头开始播放,找到当前位置
			if(position >0)
			{
				mediaPlayer.seekTo(position);
			}
		}
	}
	
}
           

到目前位置,你已经可以点击列表中的一个音乐并实现播放的功能咯。

下次继续实现的内容,下一篇的内容就非常简练化了,只要自己有想法就能直接做了:

1.播放完一个音乐之后,如何继续播放下一个。(给mediaPlayer添加歌曲完成事件)

2.实现前一首和后一首歌的播放。(获取当前点击位置,然后传递(位置-1)或(位置+1))

3.设定歌曲的播放模式。(设定模式标志变量,利用本地broadcast广播给service,实时变换模式。)

4.将当前播放歌曲的信息显示在界面下方。(同理利用licalBroadcastManager实现)