我有一项既有趣又麻烦的任务要解决。我需要创建一个播放列表(某种类型的列表),其中包含歌曲和歌曲的其他子播放列表...每个播放列表都有一种播放模式(随机、顺序等)可以创建这样的播放列表吗?我想过破解子播放列表并将从中提取的歌曲添加到主播放列表中,或者为添加到主播放列表中的每首歌曲创建一个子播放列表(我真的不喜欢这个想法),不知何故它解决了这个问题,但是必须保持每个播放列表的播放模式……
例如:
主播放列表(顺序播放模式)具有:(1)具有随机播放模式的歌曲1-1/ (2)子播放列表(歌曲2-1、歌曲2-2、歌曲2-3)/ (3)歌曲1-2
期望的结果:(1) song1-1/ (2) song2-3 (开始随机子播放列表)/ (3) song2-1/ (4) song2-2/ (5) song1-2/
我应该如何处理这个问题?
发布于 2019-02-03 20:12:19
由于我怀疑这是某种家庭作业,所以我将只提供部分实现,以便您了解如何继续。
创建一个抽象类PlaylistElement,稍后它可以是一个Song或另一个Playlist。
abstract class PlaylistElement {
public abstract List<Song> printSongs();
}实现一个扩展PlaylistElement的类Playlist。
class Playlist extends PlaylistElement {
private List<PlaylistElement> elements;
private PlaybackMode playbackMode;
@Override
public List<Song> printSongs() {
if(this.playbackMode == PlaybackMode.RANDOM) {
List<Song> songs = new ArrayList<>();
List<PlaylistElement> shuffleElements = new ArrayList<>();
//Add all PlaylistElements from elements into shuffleElements
//and shuffle the shuffleElements collection
//insert your songs into the songs collection here by sequentially
//going through your
//PlaylistElements and inserting the result of their printSongs()
//implementation (e.g. in a for-loop)
return songs;
}
else if(this.playbackMode == PlaybackMode.SEQUENTIAL) {
//you can do this on your own
}
return null;
}
}实现一个扩展PlaylistElement的类Song。
class Song extends PlaylistElement {
private String title;
private String artist;
.
.
.
@Override
public List<Song> printSongs() {
//return a List with only this Song instance inside
return Arrays.asList(new Song[] { this });
}
}为您的播放列表播放模式创建枚举。
enum PlaybackMode {
SEQUENTIAL, RANDOM;
}希望这篇文章能给你一个大概的概念!为简洁起见,省略了Getters/Setters和其他重要部分。
发布于 2019-02-03 21:32:41
尽管已经有了一些答案,但我承诺会提供一个示例实现。首先,我们有一个公共接口Playable,它是要为组合设计模式实现的类。
public interface Playable {
String getSongName();
}接下来,使用Song类来表示一首歌曲。
public class Song implements Playable {
private String name;
public Song(String name) {
this.name = name;
}
@Override
public String getSongName() {
return name;
}
}在为Playlist类做准备时,使用枚举表示不同的播放模式。
public enum PlayingMode {
SEQUENCE, RANDOM
}现在,最后是playlist类。
public class Playlist implements Playable {
private String name;
private List<Playable> playables = new ArrayList<>();
private PlayingMode mode;
private Playable currentItem;
private List<Playable> next = new ArrayList<>();
public Playlist(String name, PlayingMode mode) {
this.name = name;
this.mode = mode;
}
@Override
public String getSongName() {
if (playables.isEmpty()) {
return null;
}
if (currentItem == null) {
// initialize the playing songs
next.addAll(playables);
if (mode == PlayingMode.RANDOM) {
Collections.shuffle(next);
}
currentItem = next.get(0);
} else {
// if we have a playlist, play its songs first
if (currentItem instanceof Playlist) {
String candidate = currentItem.getSongName();
if (candidate != null) {
return candidate;
}
}
int index = next.indexOf(currentItem);
index++;
if (index < next.size()) {
currentItem = next.get(index);
} else {
currentItem = null;
}
}
return currentItem != null ? currentItem.getSongName() : null;
}
private void addToNext(Playable playable) {
if (currentItem == null) {
return;
}
// if the playlist is playing, add it to those list as well
if (mode == PlayingMode.SEQUENCE) {
next.add(playable);
} else if (mode == PlayingMode.RANDOM) {
int currentIndex = next.indexOf(currentItem);
int random = ThreadLocalRandom.current().nextInt(currentIndex, next.size());
next.add(random, playable);
}
}
public void addPlayable(Playable playable) {
Objects.requireNonNull(playable);
playables.add(playable);
addToNext(playable);
}
}下面是一些例子:
public static void main(String[] args) {
Song song1 = new Song("Song 1");
Song song2 = new Song("Song 2");
Playlist subPlaylist1 = new Playlist("Playlist 1", PlayingMode.RANDOM);
subPlaylist1.addPlayable(new Song("Song A"));
subPlaylist1.addPlayable(new Song("Song B"));
subPlaylist1.addPlayable(new Song("Song C"));
Song song3 = new Song("Song 3");
Playlist main = new Playlist("Main", PlayingMode.SEQUENCE);
main.addPlayable(song1);
main.addPlayable(song2);
main.addPlayable(subPlaylist1);
main.addPlayable(song3);
String songName = main.getSongName();
while (songName != null) {
System.out.println("Current song is: " + songName);
songName = main.getSongName();
}
}可以给出输出:
Current song is: Song 1
Current song is: Song 2
Current song is: Song B
Current song is: Song A
Current song is: Song C
Current song is: Song 3您还可以在播放时添加歌曲:
while (songName != null) {
System.out.println("Current song is: " + songName);
songName = main.getSongName();
// add songs while playing
if ("Song A".equals(songName)) {
subPlaylist1.addPlayable(new Song("Song D"));
subPlaylist1.addPlayable(new Song("Song E"));
subPlaylist1.addPlayable(new Song("Song F"));
}
}这可能会导致:
Current song is: Song 1
Current song is: Song 2
Current song is: Song B
Current song is: Song A
Current song is: Song E
Current song is: Song D
Current song is: Song F
Current song is: Song C
Current song is: Song 3最后的一些注意事项:
getIndex方法的最坏情况运行时间为O(n),如果播放列表中有许多歌曲,这可能是一个问题。更快的Collection如Set或Map将提供更好的性能,但实现有点复杂。发布于 2019-02-03 19:46:16
方法1:创建一个名为playlist和playlist item的类,该类可以保存songIds的列表,可以保存来自不同播放列表或歌曲ids的歌曲集。
class PlayList{
List<PlayListItem> playlistItems;
}
class PlayListItem{
List<String> songIds;
}如果您想要识别通过特定子播放列表添加的歌曲集,则此功能非常有用。然而,与方法2相比,这种方法使迭代变得稍微困难一些
方法2:这里在playlist项中避免了列表,因此在显示playlist时迭代很简单。但是,要识别通过particularSubPlaylist添加的songIds列表,必须进行计算。
class PlayList{
List<PlayListItem> playlistItems;
}
class PlayListItem{
String songId;
String referencePlayListId;
}https://stackoverflow.com/questions/54502404
复制相似问题