我一直在努力,但无法取得成功。我希望此代码转换为Service
,以便我的应用程序可以在foreground
中运行。非常感谢任何帮助。
我有(1)SongsManager.java
我从sdCard访问歌曲并将其放在ArrayList<HashMap<String,String>>
上。
(2)PlayListActivity.java
我将ListView
中的所有歌曲放在SongsManager.java
中,当我点击某个项目时,它会转到(3)HirouMusic.java
并且它将通过来自songIndex
的{{1}}数据会触发要播放的歌曲索引。
这是我的代码。
(1)SongsManager.java
PlayListActicity
(2)PlaListActivity.java
public class SongsManager {
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
public SongsManager(){
}
public ArrayList<HashMap<String, String>> getPlayList(Context c) {
final Cursor mCursor = c.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.DISPLAY_NAME
,MediaStore.Audio.Media.ARTIST
,MediaStore.Audio.Media.DURATION
,MediaStore.Audio.Media.ALBUM_ID
,MediaStore.Audio.Media.DATA
,MediaStore.Audio.Media.MIME_TYPE }
, null
, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
String songs_name = "";
String song_artist= "";
String song_duration= "";
String song_album_id= "";
String mAudioPath = "";
/* run through all the columns we got back and save the data we need into the arraylist for our listview*/
if (mCursor.moveToFirst()) {
do {
String file_type = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE));
songs_name = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
song_artist = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
song_duration = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
song_album_id= mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
mAudioPath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", songs_name);
song.put("songArtist", song_artist);
song.put("songDuration",song_duration);
song.put("songAlbumId", song_album_id);
song.put("songPath", mAudioPath);
songsList.add(song);
} while (mCursor.moveToNext());
}
mCursor.close();
return songsList;
}
}
(3)HirouMusic.java
public class PlayListActivity extends ListActivity {
// Songs list
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
@Override
public void 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(this);
// 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","songArtist"}, new int[] {
R.id.songTitle, R.id.songArtis });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting listitem index
int songIndex = position;
// Starting new intent
Intent in = new Intent(getApplicationContext(),
HirouMusic.class);
// Sending songIndex to PlayerActivity
in.putExtra("songIndex", songIndex);
setResult(100, in);
// Closing PlayListView
finish();
}
});
}
}