1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
| // 歌曲类
class Song {
private String title;
private String artist;
private String album;
private int duration; // 秒
private String genre;
public Song(String title, String artist, String album, int duration, String genre) {
this.title = title;
this.artist = artist;
this.album = album;
this.duration = duration;
this.genre = genre;
}
@Override
public String toString() {
return title + " - " + artist + " (" + formatDuration(duration) + ")";
}
private String formatDuration(int seconds) {
int minutes = seconds / 60;
int secs = seconds % 60;
return String.format("%d:%02d", minutes, secs);
}
// Getters
public String getTitle() { return title; }
public String getArtist() { return artist; }
public String getAlbum() { return album; }
public int getDuration() { return duration; }
public String getGenre() { return genre; }
}
// 播放模式枚举
enum PlayMode {
SEQUENTIAL, // 顺序播放
SHUFFLE, // 随机播放
REPEAT_ONE, // 单曲循环
REPEAT_ALL // 列表循环
}
// 播放列表迭代器接口
interface PlaylistIterator extends Iterator<Song> {
void setPlayMode(PlayMode mode);
PlayMode getPlayMode();
Song getCurrentSong();
void reset();
boolean hasPrevious();
Song previous();
}
// 顺序播放迭代器
class SequentialPlaylistIterator implements PlaylistIterator {
private List<Song> playlist;
private int currentIndex;
private PlayMode playMode;
public SequentialPlaylistIterator(List<Song> playlist) {
this.playlist = new ArrayList<>(playlist);
this.currentIndex = 0;
this.playMode = PlayMode.SEQUENTIAL;
}
@Override
public boolean hasNext() {
switch (playMode) {
case SEQUENTIAL:
return currentIndex < playlist.size();
case REPEAT_ALL:
return !playlist.isEmpty();
case REPEAT_ONE:
return currentIndex < playlist.size();
default:
return currentIndex < playlist.size();
}
}
@Override
public Song next() {
if (playlist.isEmpty()) {
throw new NoSuchElementException("播放列表为空");
}
Song song;
switch (playMode) {
case SEQUENTIAL:
if (currentIndex >= playlist.size()) {
throw new NoSuchElementException("播放列表已结束");
}
song = playlist.get(currentIndex++);
break;
case REPEAT_ALL:
if (currentIndex >= playlist.size()) {
currentIndex = 0; // 循环到开始
}
song = playlist.get(currentIndex++);
break;
case REPEAT_ONE:
if (currentIndex < playlist.size()) {
song = playlist.get(currentIndex);
// 单曲循环不增加索引
} else {
throw new NoSuchElementException("播放列表已结束");
}
break;
default:
song = playlist.get(currentIndex++);
}
System.out.println("播放下一首:" + song);
return song;
}
@Override
public boolean hasPrevious() {
return currentIndex > 0 || playMode == PlayMode.REPEAT_ALL;
}
@Override
public Song previous() {
if (playlist.isEmpty()) {
throw new NoSuchElementException("播放列表为空");
}
Song song;
switch (playMode) {
case SEQUENTIAL:
if (currentIndex <= 0) {
throw new NoSuchElementException("已是第一首");
}
song = playlist.get(--currentIndex);
break;
case REPEAT_ALL:
if (currentIndex <= 0) {
currentIndex = playlist.size();
}
song = playlist.get(--currentIndex);
break;
case REPEAT_ONE:
song = playlist.get(currentIndex);
break;
default:
song = playlist.get(--currentIndex);
}
System.out.println("播放上一首:" + song);
return song;
}
@Override
public void setPlayMode(PlayMode mode) {
this.playMode = mode;
System.out.println("播放模式切换为:" + mode);
}
@Override
public PlayMode getPlayMode() {
return playMode;
}
@Override
public Song getCurrentSong() {
if (currentIndex > 0 && currentIndex <= playlist.size()) {
return playlist.get(currentIndex - 1);
}
return null;
}
@Override
public void reset() {
currentIndex = 0;
System.out.println("播放位置已重置");
}
@Override
public void remove() {
throw new UnsupportedOperationException("播放时不支持删除歌曲");
}
}
// 随机播放迭代器
class ShufflePlaylistIterator implements PlaylistIterator {
private List<Song> playlist;
private List<Integer> shuffledIndices;
private int currentPosition;
private Random random;
public ShufflePlaylistIterator(List<Song> playlist) {
this.playlist = new ArrayList<>(playlist);
this.random = new Random();
shuffle();
}
private void shuffle() {
shuffledIndices = new ArrayList<>();
for (int i = 0; i < playlist.size(); i++) {
shuffledIndices.add(i);
}
Collections.shuffle(shuffledIndices, random);
currentPosition = 0;
System.out.println("播放列表已随机打乱");
}
@Override
public boolean hasNext() {
return currentPosition < shuffledIndices.size();
}
@Override
public Song next() {
if (!hasNext()) {
// 重新洗牌
shuffle();
if (!hasNext()) {
throw new NoSuchElementException("播放列表为空");
}
}
int songIndex = shuffledIndices.get(currentPosition++);
Song song = playlist.get(songIndex);
System.out.println("随机播放:" + song);
return song;
}
@Override
public boolean hasPrevious() {
return currentPosition > 0;
}
@Override
public Song previous() {
if (!hasPrevious()) {
throw new NoSuchElementException("没有上一首");
}
int songIndex = shuffledIndices.get(--currentPosition);
Song song = playlist.get(songIndex);
System.out.println("随机上一首:" + song);
return song;
}
@Override
public void setPlayMode(PlayMode mode) {
if (mode != PlayMode.SHUFFLE) {
throw new UnsupportedOperationException("随机迭代器只支持随机模式");
}
}
@Override
public PlayMode getPlayMode() {
return PlayMode.SHUFFLE;
}
@Override
public Song getCurrentSong() {
if (currentPosition > 0 && currentPosition <= shuffledIndices.size()) {
int songIndex = shuffledIndices.get(currentPosition - 1);
return playlist.get(songIndex);
}
return null;
}
@Override
public void reset() {
shuffle();
}
@Override
public void remove() {
throw new UnsupportedOperationException("随机播放时不支持删除");
}
}
// 播放列表类
class Playlist implements Iterable<Song> {
private String name;
private List<Song> songs;
private PlayMode defaultPlayMode;
public Playlist(String name) {
this.name = name;
this.songs = new ArrayList<>();
this.defaultPlayMode = PlayMode.SEQUENTIAL;
}
public void addSong(Song song) {
songs.add(song);
System.out.println("添加歌曲到播放列表 \"" + name + "\":" + song);
}
public void removeSong(Song song) {
if (songs.remove(song)) {
System.out.println("从播放列表移除:" + song);
}
}
public void removeSong(int index) {
if (index >= 0 && index < songs.size()) {
Song removed = songs.remove(index);
System.out.println("从播放列表移除:" + removed);
}
}
@Override
public Iterator<Song> iterator() {
return createIterator(defaultPlayMode);
}
public PlaylistIterator createIterator(PlayMode playMode) {
switch (playMode) {
case SHUFFLE:
return new ShufflePlaylistIterator(songs);
default:
SequentialPlaylistIterator iterator = new SequentialPlaylistIterator(songs);
iterator.setPlayMode(playMode);
return iterator;
}
}
// 按艺术家过滤
public Iterator<Song> iteratorByArtist(String artist) {
return songs.stream()
.filter(song -> song.getArtist().equalsIgnoreCase(artist))
.iterator();
}
// 按专辑过滤
public Iterator<Song> iteratorByAlbum(String album) {
return songs.stream()
.filter(song -> song.getAlbum().equalsIgnoreCase(album))
.iterator();
}
// 按风格过滤
public Iterator<Song> iteratorByGenre(String genre) {
return songs.stream()
.filter(song -> song.getGenre().equalsIgnoreCase(genre))
.iterator();
}
public void showPlaylist() {
System.out.println("=== 播放列表:" + name + " ===");
if (songs.isEmpty()) {
System.out.println("播放列表为空");
} else {
for (int i = 0; i < songs.size(); i++) {
System.out.println((i + 1) + ". " + songs.get(i));
}
}
System.out.println("总计:" + songs.size() + " 首歌曲");
System.out.println("=========================");
}
// Getters and Setters
public String getName() { return name; }
public void setDefaultPlayMode(PlayMode mode) { this.defaultPlayMode = mode; }
public PlayMode getDefaultPlayMode() { return defaultPlayMode; }
public int size() { return songs.size(); }
public boolean isEmpty() { return songs.isEmpty(); }
}
// 音乐播放器
class MusicPlayer {
private Playlist currentPlaylist;
private PlaylistIterator iterator;
private Song currentSong;
private boolean isPlaying;
public void loadPlaylist(Playlist playlist, PlayMode playMode) {
this.currentPlaylist = playlist;
this.iterator = playlist.createIterator(playMode);
this.isPlaying = false;
System.out.println("已加载播放列表:" + playlist.getName() + " (模式:" + playMode + ")");
}
public void play() {
if (iterator != null && iterator.hasNext()) {
currentSong = iterator.next();
isPlaying = true;
System.out.println("🎵 正在播放:" + currentSong);
} else {
System.out.println("没有歌曲可播放");
}
}
public void playNext() {
if (iterator != null && iterator.hasNext()) {
currentSong = iterator.next();
System.out.println("🎵 播放下一首:" + currentSong);
} else {
System.out.println("没有下一首歌曲");
isPlaying = false;
}
}
public void playPrevious() {
if (iterator != null && iterator.hasPrevious()) {
currentSong = iterator.previous();
System.out.println("🎵 播放上一首:" + currentSong);
} else {
System.out.println("没有上一首歌曲");
}
}
public void stop() {
isPlaying = false;
System.out.println("⏹️ 停止播放");
}
public void changePlayMode(PlayMode mode) {
if (currentPlaylist != null) {
iterator = currentPlaylist.createIterator(mode);
System.out.println("播放模式已切换为:" + mode);
}
}
public void showStatus() {
System.out.println("=== 播放器状态 ===");
System.out.println("当前歌曲:" + (currentSong != null ? currentSong : "无"));
System.out.println("播放状态:" + (isPlaying ? "播放中" : "停止"));
System.out.println("播放模式:" + (iterator != null ? iterator.getPlayMode() : "未设置"));
System.out.println("================");
}
}
// 使用示例
public class MusicPlayerExample {
public static void main(String[] args) throws InterruptedException {
// 创建播放列表
Playlist myPlaylist = new Playlist("我的收藏");
// 添加歌曲
myPlaylist.addSong(new Song("夜曲", "周杰伦", "十一月的萧邦", 237, "流行"));
myPlaylist.addSong(new Song("青花瓷", "周杰伦", "我很忙", 228, "中国风"));
myPlaylist.addSong(new Song("稻香", "周杰伦", "魔杰座", 223, "流行"));
myPlaylist.addSong(new Song("告白气球", "周杰伦", "周杰伦的床边故事", 203, "流行"));
myPlaylist.addSong(new Song("晴天", "周杰伦", "叶惠美", 269, "流行"));
myPlaylist.showPlaylist();
// 创建音乐播放器
MusicPlayer player = new MusicPlayer();
// 顺序播放模式
System.out.println("\n=== 顺序播放模式 ===");
player.loadPlaylist(myPlaylist, PlayMode.SEQUENTIAL);
player.play();
Thread.sleep(1000);
player.playNext();
Thread.sleep(1000);
player.playNext();
Thread.sleep(1000);
player.playPrevious();
// 随机播放模式
System.out.println("\n=== 随机播放模式 ===");
player.changePlayMode(PlayMode.SHUFFLE);
player.play();
Thread.sleep(1000);
player.playNext();
Thread.sleep(1000);
player.playNext();
// 单曲循环模式
System.out.println("\n=== 单曲循环模式 ===");
player.changePlayMode(PlayMode.REPEAT_ONE);
player.play();
Thread.sleep(1000);
player.playNext(); // 还是同一首
Thread.sleep(1000);
player.playNext(); // 还是同一首
// 按艺术家过滤
System.out.println("\n=== 按艺术家过滤 ===");
Iterator<Song> artistIterator = myPlaylist.iteratorByArtist("周杰伦");
while (artistIterator.hasNext()) {
Song song = artistIterator.next();
System.out.println("周杰伦的歌曲:" + song);
}
player.showStatus();
}
}
|