无法实施"最近播放"音乐播放器中的播放列表

时间:2017-12-20 10:17:25

标签: android audio-player android-sharedpreferences android-music-player

我试图实施"最近播放"我的音乐播放器中的播放列表,显示最近由用户播放的那些歌曲(在recyclerView中)。但我的代码所做的是,它只存储最后播放的歌曲。

我想要的: 如果我的音乐播放器中有2首歌曲,则说A和B.我先播放歌曲A.然后它应该被添加到最近播放的列表(在第一个位置)。之后,如果我播放歌曲B,它也应该被添加到列表中(不替换A)但是按下A并且B占据第一个位置。因此,最终列表应如下所示:B位于顶部,A位于其下方。

但我的应用程序做了什么: ONly存储最后播放并替换其他任何内容,即如果我播放歌曲B播放歌曲A,歌曲A将被歌曲B替换,并且列表中只有一首歌曲。

最后我的问题如上所述,app只存储一首歌曲(最后一首歌曲)。顺便说一句,我正在使用共享偏好。

代码:

 public void storeAudioPlaylist(SongInfoModel songInfoModel) {
    preferences = context.getSharedPreferences(STORAGE, 
 Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    Gson gson = new Gson();
    playlistStore.add(songInfoModel);
    String json = gson.toJson(playlistStore);
    editor.putString("audioPlayList", json);
    editor.apply();
}

 public ArrayList<SongInfoModel> loadAudioPlaylist() {
    preferences = context.getSharedPreferences(STORAGE, 
Context.MODE_PRIVATE);
    Gson gson = new Gson();
    String json = preferences.getString("audioPlayList", null);
    Type type = new TypeToken<ArrayList<SongInfoModel>>() {
    }.getType();
    return gson.fromJson(json, type);
}

以上2个函数用于将歌曲对象存储到数组并分别检索数组。

代码:

  public void onClickListener(SongInfoModel song, int position) {

storageUtil.storeAudioPlaylist(song); // this calls the method and passes 
                                         the song model(ID,name,artist..etc)


   }

0 个答案:

没有答案
相关问题