設計音樂播放器應用程式需要仔細規劃和建構組件,以確保無縫且高效的使用者體驗。
播放功能:
播放清單管理:
搜尋:
媒體控制:
儲存:
音樂播放器應用程式可分為以下組件:
讓我們看看每個元件的底層設計和實作。
Song 類別代表單一音樂曲目及其元資料。
public class Song { private String id; private String title; private String artist; private String album; private double duration; // in seconds public Song(String id, String title, String artist, String album, double duration) { this.id = id; this.title = title; this.artist = artist; this.album = album; this.duration = duration; } // Getters and setters public String getId() { return id; } public String getTitle() { return title; } public String getArtist() { return artist; } public String getAlbum() { return album; } public double getDuration() { return duration; } }
Playlist 類別管理歌曲集合。它允許添加、刪除和獲取歌曲。
import java.util.ArrayList; import java.util.List; public class Playlist { private String name; private List<Song> songs; public Playlist(String name) { this.name = name; this.songs = new ArrayList<>(); } public void addSong(Song song) { songs.add(song); } public void removeSong(Song song) { songs.remove(song); } public List<Song> getSongs() { return songs; } public String getName() { return name; } }
MusicPlayer 類別處理播放、暫停、停止和音量控制等播放功能。
public class MusicPlayer { private Song currentSong; private boolean isPlaying; public void play(Song song) { this.currentSong = song; this.isPlaying = true; System.out.println("Playing: " + song.getTitle() + " by " + song.getArtist()); } public void pause() { if (isPlaying) { isPlaying = false; System.out.println("Paused: " + currentSong.getTitle()); } else { System.out.println("No song is currently playing."); } } public void stop() { if (currentSong != null) { System.out.println("Stopped: " + currentSong.getTitle()); currentSong = null; isPlaying = false; } else { System.out.println("No song is currently playing."); } } public void resume() { if (currentSong != null && !isPlaying) { isPlaying = true; System.out.println("Resumed: " + currentSong.getTitle()); } else { System.out.println("No song to resume."); } } }
SearchService 類別允許使用者按標題、藝術家或專輯搜尋歌曲。
import java.util.ArrayList; import java.util.List; public class SearchService { private List<Song> songs; public SearchService(List<Song> songs) { this.songs = songs; } public List<Song> searchByTitle(String title) { List<Song> results = new ArrayList<>(); for (Song song : songs) { if (song.getTitle().equalsIgnoreCase(title)) { results.add(song); } } return results; } public List<Song> searchByArtist(String artist) { List<Song> results = new ArrayList<>(); for (Song song : songs) { if (song.getArtist().equalsIgnoreCase(artist)) { results.add(song); } } return results; } public List<Song> searchByAlbum(String album) { List<Song> results = new ArrayList<>(); for (Song song : songs) { if (song.getAlbum().equalsIgnoreCase(album)) { results.add(song); } } return results; } }
StorageService 類別模擬從本機儲存讀取歌曲。
public class Song { private String id; private String title; private String artist; private String album; private double duration; // in seconds public Song(String id, String title, String artist, String album, double duration) { this.id = id; this.title = title; this.artist = artist; this.album = album; this.duration = duration; } // Getters and setters public String getId() { return id; } public String getTitle() { return title; } public String getArtist() { return artist; } public String getAlbum() { return album; } public double getDuration() { return duration; } }
import java.util.ArrayList; import java.util.List; public class Playlist { private String name; private List<Song> songs; public Playlist(String name) { this.name = name; this.songs = new ArrayList<>(); } public void addSong(Song song) { songs.add(song); } public void removeSong(Song song) { songs.remove(song); } public List<Song> getSongs() { return songs; } public String getName() { return name; } }
以上是音樂播放器應用程式的底層設計的詳細內容。更多資訊請關注PHP中文網其他相關文章!