首先,這個播放器不是我想的,我是根據這個作者的部落格教程來寫的咯。。。也算是自己想啦,附上連結 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實作)