首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检索最新播放的歌曲?

如何检索最新播放的歌曲?
EN

Stack Overflow用户
提问于 2017-11-19 14:08:36
回答 1查看 321关注 0票数 1

我正在创建一个最近在其中播放过歌曲的音乐播放器播放列表。我正在存储歌曲的数组列表和正在共享首选项中播放的歌曲的索引。要获取最近播放的歌曲,我要做的是从共享首选项中检索数组列表和歌曲索引,并将其保存在另一个数组列表中。但问题是recyclerView一次只能显示一首歌曲。

例如,如果我播放歌曲A,recyclerView应该在第一位置显示歌曲A,如果我播放歌曲B,recyclerView应该在第一位置显示歌曲B,在第二位置显示歌曲A。但它只显示了第一个位置。

RecentlyPLayedsongs.java

代码语言:javascript
复制
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LayoutInflater inflater = LayoutInflater.from(this);
    View view6 = inflater.inflate(R.layout.activity_recently_played, null);
    FrameLayout container6 = (FrameLayout) findViewById(R.id.container);
    container6.addView(view6);

    recyclerView_recently_played = findViewById(R.id.recyclerView_recently_played);

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView_recently_played.setLayoutManager(linearLayoutManager);

    StorageUtil storageUtil2 = new StorageUtil(getApplicationContext());

    SongList=storageUtil2.loadAudio();

    pos = storageUtil2.loadAudioIndex();

    songInfoModel = SongList.get(pos);

    RecentlyPlayedList.add(songInfoModel);

    adapter1 = new Playlist_Recently_Added_Adapter(RecentlyPlayedList, getApplicationContext());
    recyclerView_recently_played.setAdapter(adapter1);

}
EN

回答 1

Stack Overflow用户

发布于 2017-11-19 14:17:02

您可以通过以下两种方式之一来完成此操作

  1. 在传递歌曲列表之前,您需要根据时间进行排序。在这种情况下,您还需要在播放歌曲时节省时间。(好吧,为了实现这一点,您需要进行大量的编码,而它的maintainance)
  2. This方法要简单得多。您可以使用数组列表的add()方法,并且可以指定其中的位置,如下面的代码所述

list.add(o,currentSong);

这里传递的是0,这意味着当你播放一首歌曲时,这首歌将被添加到索引为0的位置,这意味着你最近播放的歌曲将总是在最前面。希望这对你有帮助。

注意:我已经用下面的代码验证了第二个解决方案,它可以正常工作

代码语言:javascript
复制
List<Integer> list = new ArrayList();
    list.add(1);
    list.add(2);
    list.add(3);

    Log.d(TAG, "firs time");
    for (int a=0; a < list.size();a++ ) {
        Log.d(TAG, "" + list.get(a));
    }

    list.add(0,4);

    Log.d(TAG, "second time");
    for (int a=0; a < list.size();a++ ) {
        Log.d(TAG, "" + list.get(a));
    }

日志如下所述

代码语言:javascript
复制
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: firs time
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 1
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 2
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 3
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: second time
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 4
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 1
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 2
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 3

看看第二次编号在'0‘索引处被添加的日志,而不是在第一个位置上,剩余的项目被下推。希望这能说得通。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47374182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档