6. 为PlayList(播放列表)写ListView
使用列表视图(ListView)来显示播放列表,在drawable文件夹下创建一个xml布局文件并叫做list_selector.xml,这个xml是用于对列表项渐变背景。
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<!-- Selector style for listrow -->
<
item
android:state_selected
=
"false"
android:state_pressed
=
"false"
android:drawable
=
"@drawable/gradient_bg"
/>
<
item
android:state_pressed
=
"true"
android:drawable
=
"@drawable/gradient_bg_hover"
/>
<
item
android:state_selected
=
"true"
android:state_pressed
=
"false"
android:drawable
=
"@drawable/gradient_bg_hover"
/>
</
selector
>
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:orientation
=
"vertical"
>
<
ListView
android:id
=
"@android:id/list"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:divider
=
"#242424"
android:dividerHeight
=
"1dp"
android:listSelector
=
"@drawable/list_selector"
/>
</
LinearLayout
>
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
android:gravity
=
"center"
android:background
=
"@drawable/list_selector"
android:padding
=
"5dp"
>
<
TextView
android:id
=
"@+id/songTitle"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:textSize
=
"16dp"
android:padding
=
"10dp"
android:color
=
"#f3f3f3"
/>
</
LinearLayout
>
|
通过使用以上布局我们可以实现以下列表视图通过将数据加载到它。
7. 写从SDcard读取mp3文件的类
到目前为止,我们已经完成了player的静态布局,现在的实际代码开始。创建一个新的类文件,并将其命名为SongsManager.java。这个类将
从设备上的sdcard阅读所有的文件并且过滤只留带.mp3后缀的文件。
package
com.androidhive.musicplayer;
import
java.io.File;
import
java.io.FilenameFilter;
import
java.util.ArrayList;
import
java.util.HashMap;
publicclass
SongsManager {
// SDCard Path
final
String MEDIA_PATH =
new
String(
"/sdcard/"
);
private
ArrayList<HashMap<String, String>> songsList =
new
ArrayList<HashMap<String, String>>();
// Constructor
public
SongsManager(){
}
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
* */
public
ArrayList<HashMap<String, String>> getPlayList(){
File home =
new
File(MEDIA_PATH);
if
(home.listFiles(
new
FileExtensionFilter()).length >
0
) {
for
(File file : home.listFiles(
new
FileExtensionFilter())) {
HashMap<String, String> song =
new
HashMap<String, String>();
song.put(
"songTitle"
, file.getName().substring(
0
, (file.getName().length() -
4
)));
song.put(
"songPath"
, file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list array
return
songsList;
}
/**
* Class to filter files which are having .mp3 extension
* */
class
FileExtensionFilter
implements
FilenameFilter {
publicboolean
accept(File dir, String name) {
return
(name.endsWith(
".mp3"
) || name.endsWith(
".MP3"
));
}
}
}
|
8. 为PlayList写列表视图
为
播放列表的列表视图创建一个新的活动类,叫做
PlayListActivity.java
,PlayListActivity.java 这个类通过使用SongsManager.java类来显示歌曲列表。
package
com.androidhive.musicplayer;
import
java.util.ArrayList;
import
java.util.HashMap;
import
android.app.ListActivity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.AdapterView;
import
android.widget.AdapterView.OnItemClickListener;
import
android.widget.ListAdapter;
import
android.widget.ListView;
import
android.widget.SimpleAdapter;
publicclass
PlayListActivity
extends
ListActivity {
// Songs list
public
ArrayList<HashMap<String, String>> songsList =
new
ArrayList<HashMap<String, String>>();
@Override
publicvoid
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
ArrayList<HashMap<String, String>> songsListData =
new
ArrayList<HashMap<String, String>>();
SongsManager plm =
new
SongsManager();
// get all songs from sdcard
this
.songsList = plm.getPlayList();
// looping through playlist
for
(
int
i =
0
; i < songsList.size(); i++) {
// creating new HashMap
HashMap<String, String> song = songsList.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
// Adding menuItems to ListView
ListAdapter adapter =
new
SimpleAdapter(
this
, songsListData,
R.layout.playlist_item,
new
String[] {
"songTitle"
},
new
int
[] {
R.id.songTitle });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(
new
OnItemClickListener() {
@Override
publicvoid
onItemClick(AdapterView<?> parent, View view,
int
position,
long
id) {
// getting listitem index
int
songIndex = position;
// Starting new intent
Intent in =
new
Intent(getApplicationContext(),
AndroidBuildingMusicPlayerActivity.
class
);
// Sending songIndex to PlayerActivity
in.putExtra(
"songIndex"
, songIndex);
setResult(
100
, in);
// Closing PlayListView
finish();
}
});
}
}
|
9. 辅助类的功能
创建一个新类,叫作
Utilities.java,用来处理额外的工作像转换时间进度百分比,反之亦然。此外,它具有功能将毫秒定时器
转换为时间字符串显示在播放器的
seekbar上。
package
com.androidhive.musicplayer;
publicclass
Utilities {
/**
* Function to convert milliseconds time to
* Timer Format
* Hours:Minutes:Seconds
* */
public
String milliSecondsToTimer(
long
milliseconds){
String finalTimerString =
""
;
String secondsString =
""
;
// Convert total duration into time
int
hours = (
int
)( milliseconds / (
1000
*
60
*
60
));
int
minutes = (
int
)(milliseconds % (
1000
*
60
*
60
)) / (
1000
*
60
);
int
seconds = (
int
) ((milliseconds % (
1000
*
60
*
60
)) % (
1000
*
60
) /
1000
);
// Add hours if there
if
(hours >
0
){
finalTimerString = hours +
":"
;
}
// Prepending 0 to seconds if it is one digit
if
(seconds <
10
){
secondsString =
"0"
+ seconds;
}
else
{
secondsString =
""
+ seconds;}
finalTimerString = finalTimerString + minutes +
":"
+ secondsString;
// return timer string
return
finalTimerString;
}
/**
* Function to get Progress percentage
* @param currentDuration
* @param totalDuration
* */
publicint
getProgressPercentage(
long
currentDuration,
long
totalDuration){
Double percentage = (
double
)
0
;
long
currentSeconds = (
int
) (currentDuration /
1000
);
long
totalSeconds = (
int
) (totalDuration /
1000
);
// calculating percentage
percentage =(((
double
)currentSeconds)/totalSeconds)*
100
;
// return percentage
return
percentage.intValue();
}
/**
* Function to change progress to timer
* @param progress -
* @param totalDuration
* returns current duration in milliseconds
* */
publicint
progressToTimer(
int
progress,
int
totalDuration) {
int
currentDuration =
0
;
totalDuration = (
int
) (totalDuration /
1000
);
currentDuration = (
int
) ((((
double
)progress) /
100
) * totalDuration);
// return current duration in milliseconds
return
currentDuration *
1000
;
}
}
|