Rumah >hujung hadapan web >tutorial js >Reka Bentuk Peringkat Rendah Aplikasi Pemain Muzik
Mereka bentuk aplikasi pemain muzik memerlukan perancangan dan penstrukturan komponen yang teliti untuk memastikan pengalaman pengguna yang lancar dan cekap.
Fungsi Main Balik:
Pengurusan Senarai Main:
Cari:
Kawalan Media:
Storan:
Aplikasi pemain muzik boleh dipecahkan kepada komponen berikut:
Mari kita lihat reka bentuk peringkat rendah dan pelaksanaan setiap komponen.
Kelas Lagu mewakili satu runut muzik dengan metadatanya.
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; } }
Kelas Senarai Main menguruskan koleksi lagu. Ia membenarkan menambah, mengalih keluar dan mengambil lagu.
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; } }
Kelas MusicPlayer mengendalikan fungsi main balik seperti main, jeda, berhenti dan kawalan kelantangan.
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."); } } }
Kelas SearchService membolehkan pengguna mencari lagu mengikut tajuk, artis atau album.
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; } }
Kelas StorageService mensimulasikan membaca lagu daripada storan tempatan.
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; } }
Atas ialah kandungan terperinci Reka Bentuk Peringkat Rendah Aplikasi Pemain Muzik. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!