The content written in the first article is very simple, almost the same as what the link author wrote.
What he wrote is more complicated, and some implementation methods are different, so I wrote a blog to record it...O(∩_∩)O~
This player is very simple , if you don’t know what APIs are and you finish writing them in two days after reading this, you can check what the relevant APIs are used for while reading this. If you have understood the relevant APIs before, you can finish it in one day.
What is the general idea of ??making a player? Of course the player must have a playback function, I'm talking nonsense. This player does not have a download function, so it must get the music that already exists on your phone. So the first step is to obtain the music information in the mobile phone. This has nothing to do with the player interface. Then you can write it as a tool class called GetMusicListUtil, a class specifically used to obtain music information on the mobile phone.
Above code:
public class GetMusicListUtil {
/**
* Get all the music from the mobile phone and put it in the list Save in
*
* @param context
* @return
* @author Yang
* /
public static List
{
Cursor cursor = context.getContentResolver().query(
< p> MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); List
for(int i=0; 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)); //Duration< /p> long musicSize = cursor.getLong(cursor .getColumnIndex(MediaStore.Audio.Media.SIZE)); //File size String musicUrl = cursor. getString(cursor .getColumnIndex(MediaStore.Audio.Media.DATA)); //File path int isMusic = cursor.getInt(cursor . getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); //Only add music to the collection 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; } /** * Convert the duration of music from milliseconds to seconds format * * @param time * @return * @author Yang */ public static String formatTime(long time) { String min = time / (1000 * 60) + ""; < /p> 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)) + "";
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);
}
/**
* Change List< Mp3Info>Convert to the format of List
* In order to fill in data for SimpleAdapter
*
* @param musicInfos
* @return
* @author Yang
*
*/
public static List
List
{
List
new ArrayList
for(Iterator
{
Mp3Info music = iterator.next();
HashMap
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;
}< /p>
}
I believe everyone can understand that the Cursor object is queried and then stored in a List of Mp3Info type. The reason why a List
This is no different from what the original author wrote. In addition, Mp3Info is a javabean, as shown below:
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;< /p>
}
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;
}
}
After having the music information, We can start editing the interface. The pictures in my interface are also downloaded from the original author page. But the model inside is slightly different, and I simplified it. . .
One *** only uses so many pictures: background, play button picture, next song button picture, previous song button picture, music mode button picture, and small music icon.
Then there is the main interface xml file:
xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bk1"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id=" @+id/handleButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" p> android:layout_centerHorizontal="true" >
android:id="@+id/musicList" android:layout_width="fill_parent" p> android:layout_height="300dp" android:layout_below="@+id/handleButton" >
< RelativeLayout
android:id="@+id/songInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/musicList"
android:layout_alignParentBottom="true"