进度列表视图项目背景颜色取决于音乐播放器进度



我有一个歌曲列表,并希望将当前正在播放的歌曲的背景颜色与搜索栏或进度条一样。当我的歌曲完成并转到下一首时,它应该能够进步背景颜色。请给出如何实现此功能的任何想法或源代码。提前感谢您的帮助。

主要思想是以毫秒为单位获取歌曲长度,并将其用作进度条的 100%。

MediaPlayer允许您执行此操作:

public long getSoundDuration(Context context, String file){
    AssetFileDescriptor afd;
    try {
        afd = context.getAssets().openFd(file);
        MediaPlayer player = new MediaPlayer();
        player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
        player.prepare();
        int duration = player.getDuration();
        player.release();
        return duration;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 0; 
}

所以现在当你有持续时间时,它很容易转换为进度条值。

希望对您有所帮助

最新更新