如何在不完成ListView活动的情况下播放ListView中的音乐?

时间:2014-03-08 15:40:45

标签: android android-listview

我正在使用service开发简单的音乐播放器。我有一个名为“MusicPlayerActivity”的主要活动和一个Service类“MusicService”。当我点击“MusicPlayerActivity”类上的一个按钮时,打开一个包含列表视图的“ListviewActivity”类在SD卡中出现的所有歌曲中,当我从列表中选择一首歌曲然后“ListviewActivity”关闭并返回“MusicPlayerActivity”活动并使用Intent播放该歌曲。 但我的问题是“如何在不回到主要活动的情况下播放所选歌曲?” 谢谢你们,我在等你的回复。

这是PlayListActivity类..

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();

    // 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
        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(),
                    AndroidBuildingMusicPlayerActivity.class);
            // Sending songIndex to PlayerActivity
            in.putExtra("songIndex", songIndex);
            setResult(100, in);
            // Closing PlayListView
            finish();

        }
    });

}

}

0 个答案:

没有答案