-
ArrayLIst 예제JAVA 2022. 7. 19. 16:29
EX_01. MP3 Player
1-1. Music
public class Music { //music(mp3)설계 //노래제목 > String //가수이름 > String //재생시간 > int //경로데이터 > String // private 접근제한자로 감싸준다 private String title; private String name; private String data; private int time; //생성자 만들기(Constructor) public Music(String title, String name, String data, int time) { super(); this.title = title; this.name = name; this.data = data; this.time = time; } //getter , setter public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDate() { return data; } public void setDate(String data) { this.data = data; } public int getTime() { return time; } public void setTime(int time) { this.time = time; } }
1-2. Main
import java.util.ArrayList; import java.util.Scanner; import javazoom.jl.player.MP3Player; public class Main { public static void main(String[] args) { // 입력 도구 불러오기 Scanner sc = new Scanner(System.in); // MP3Player 객체 불러오기 //mp3 노래를 재생할 수 있는 환경이 만들어짐 MP3Player mp3 = new MP3Player(); // play 할때 경로 데이터가 필요하다 // 목록 - 노래제목, 가수, playtime // mp3.play("C:\\Users\\SMHRD\\Desktop\\Java\\MusicPlayer\\player\\Peaches.mp3"); // mp3.stop(); //노래 4개에 대해서 객체 생성하기 //선언과 동시에 데이터 초기화하기(가수이름, 노래제목, 플레이타임, 경로) String data = "C:\\Users\\SMHRD\\Desktop\\Java\\MusicPlayer\\player\\"; //(공통된 경로를 담음) Music music1 = new Music("벚꽃엔딩", "장범준", data+"Blossom.mp3", 60); Music music2 = new Music("버터플라이", "모름", data+"Butterfly.mp3", 60); Music music3 = new Music("peaches", "Justin Bieber", data+"Peaches.mp3", 60); Music music4 = new Music("롤린", "브레이브걸스",data+"Rollin.mp3", 60); ArrayList<Music> musiclist = new ArrayList<Music>(); musiclist.add(music1); musiclist.add(music2); musiclist.add(music3); musiclist.add(music4); boolean play1 = true; int i = 0; //경로에 접근해보자 System.out.println(musiclist.get(0).getDate()); // 1재생 2정지 3이전곡 4다음곡 5종료 while(play1) { System.out.print("[1]재생 [2]정지 [3]이전곡 [4]다음곡 [5]종료"); int num = sc.nextInt(); switch(num) { case 1: System.out.print(musiclist.get(i).getTitle() + "-" + musiclist.get(i).getName()); System.out.println(":" + musiclist.get(i).getTime() / 60 + "분" + musiclist.get(i).getTime() % 60 + "초"); mp3.play(musiclist.get(i).getDate()); break; case 2: if(mp3.isPlaying()) { mp3.stop(); }else { System.out.println("error"); } break; case 3: if(mp3.isPlaying()) mp3.stop(); i--; if(i==-1) i = musiclist.size()-1; System.out.print(musiclist.get(i).getTitle() + "-" + musiclist.get(i).getName()); System.out.println( ":" + musiclist.get(i).getTime() / 60 + "분" + musiclist.get(i).getTime() % 60 + "초"); mp3.play(musiclist.get(i).getDate()); break; case 4: if(mp3.isPlaying()) mp3.stop(); i++; if(i>=musiclist.size()) i = 0; System.out.print(musiclist.get(i).getTitle() + "-" + musiclist.get(i).getName()); System.out.println(":" + musiclist.get(i).getTime() / 60 + "분" + musiclist.get(i).getTime() % 60 + "초"); mp3.play(musiclist.get(i).getDate()); break; case 5: if(mp3.isPlaying()) mp3.stop(); System.out.println("프로그램 종료"); play1 = false; break; default: } } } }