1. Model (๋ชจ๋ธ)
package ex03_๋ฎค์งํ๋ ์ด์ด;
public class Music {
// Model : ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด
private String title; // ๋
ธ๋์ ๋ชฉ
private String singer; // ๊ฐ์
private String path; // ๋
ธ๋ ์ ์ฅ ๊ฒฝ๋ก
public Music(String title, String singer, String path) {
this.title = title;
this.singer = singer;
this.path = path;
}
// (2) getter ๋ฉ์๋
public String getTitle() {
return title;
}
public String getSinger() {
return singer;
}
public String getPath() {
return path;
}
}
2. View (๋ทฐ)
package ex03_๋ฎค์งํ๋ ์ด์ด;
import java.util.ArrayList;
import java.util.Scanner;
import javazoom.jl.player.MP3Player;
public class MusicMain2 {
// ์ ์ง๋ณด์ ์ฝ๊ฒ ์ฝ๋ ๋ฆฌํฉํ ๋ง
// ๋ชจ๋ - ํด๋์ค 1๊ฐ;
// View : ์๊ฐํ(์
์ถ๋ ฅ ์ญํ )
public static void main(String[] args) {
// jar(ํด๋์คํ์ผ๋ค์ ์งํฉ) ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ
// ํ๋ก์ ํธ ์ ํ --> ๋ง์ฐ์ค ์ฐํด๋ฆญ --> build path
// --> configure build path --> ํญ์ค์ libraries
// --> class path ์ต์
์ ํ --> add jars
Scanner sc = new Scanner(System.in);
MusicController con = new MusicController();
while (true) {
System.out.print("[1]๋
ธ๋์ฌ์ [2]๋ค์๊ณก [3]์ด์ ๊ณก [4]์ ์ง [5]์ข
๋ฃ >> ");
int input = sc.nextInt();
if (input == 1) {
// ๋
ธ๋์ฌ์
Music m = con.play();
// (2) ๋
ธ๋์ ๋ํ ์ ๋ณด๋ฅผ ์ถ๋ ฅ
System.out.println("====์ฌ์์ค์ธ ๋
ธ๋====");
System.out.println("๋
ธ๋ ์ ๋ชฉ \t ๊ฐ์");
System.out.print(m.getTitle() + " \t ");
System.out.println(m.getSinger());
} else if (input == 2) {
// ๋ค์๊ณก
Music m = con.next();
// ๋ค์๊ณก์ด ์์์ ๊ฒฝ์ฐ์๋ง index 1์ฆ๊ฐ์ํค๊ณ ๋
ธ๋๋ฅผ ์ฌ์
if (m != null) {
// (2) musicList ๋๋ฒ์งธ ๋ฐฉ์ ์๋ ๋
ธ๋ ์ ๋ณด ์ถ๋ ฅ
System.out.println("====์ฌ์์ค์ธ ๋
ธ๋====");
System.out.println("๋
ธ๋ ์ ๋ชฉ \t ๊ฐ์");
System.out.print(m.getTitle() + " \t ");
System.out.println(m.getSinger());
} else {
System.out.println("๋ค์๊ณก์ด ์์ต๋๋ค.");
}
} else if (input == 3) {
// ์ด์ ๊ณก
Music m = con.pre();
if (m != null) {
System.out.println("====์ฌ์์ค์ธ ๋
ธ๋====");
System.out.println("๋
ธ๋ ์ ๋ชฉ \t ๊ฐ์");
System.out.print(m.getTitle() + " \t ");
System.out.println(m.getSinger());
} else {
System.out.println("์ด์ ๊ณก์ด ์์ต๋๋ค.");
}
} else if (input == 4) {
// ์ ์ง
con.stop();
System.out.println("๋
ธ๋๋ฅผ ์ ์งํฉ๋๋ค.");
} else if (input == 5) {
con.stop();
System.out.println("ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.");
break;
}
}
}
}
2. Controller (์ปจํธ๋กค๋ฌ)
package ex03_๋ฎค์งํ๋ ์ด์ด;
import java.util.ArrayList;
import javazoom.jl.player.MP3Player;
public class MusicController {
// Controller : ๋ฐ์ดํฐ์ ํ๋ฆ ์ ์ด
// (์์
์ฌ์, ๋ค์๊ณก ๋๊ธฑ๊ธฐ, ์ด์ ๊ณก ๋๊ธฐ๊ธฐ, ์์
์ ์ง)
// 1. ํ๋
private MP3Player mp3 = new MP3Player();
private String comPath = "C:\\Users\\USER\\Desktop\\JavaStudy\\ex0706\\src\\player\\";
private ArrayList<Music> musicList = new ArrayList<Music>();
private int index = 0;
// 2. ๋ฉ์๋
// ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์๊ฐ์ ๋ฎค์งํ๋ ์ด๋ฆฌ์คํธ๋ฅผ ์ถ๊ฐํ๊ธฐ
// --> ๊ธฐ๋ณธ ์์ฑ์ ์ฌ์ฉ
// ์์ฑ์๋ ๋ฆฌํดํ์
์ง์ X;
// ๋ฉ์๋๋ช
์ ํด๋์ค๋ช
์ด๋ ํ ์ ํ ํจ๋ ์ํ๋ฆฌ๊ณ ๊ฐ์์ผํจ.
// ๋งค๊ฐ๋ณ์(x)
public MusicController() {
musicList.add(new Music("Attention", "๋ด์ง์ค", comPath + "Attention.mp3"));
musicList.add(new Music("Butterfly", "์ ์ํธ", comPath + "Butterfly.mp3"));
musicList.add(new Music("LoveDive", "์์ด๋ธ", comPath + "Lovedive.mp3"));
musicList.add(new Music("Nxde", "์์ด๋ค", comPath + "Nxde.mp3"));
musicList.add(new Music("RushHour", "ํฌ๋ฌ์ฌ", comPath + "Rushhour.mp3"));
}
// ์ฌ์ํ๊ธฐ
public Music play() {
stop();
mp3.play(musicList.get(index).getPath());
return musicList.get(index);
}
// ์ ์งํ๊ธฐ
// ๋ฆฌํดํ์
x, ๋ฉ์๋๋ช
stop
public void stop() {
if (mp3.isPlaying()) {
mp3.stop();
}
}
// ๋ค์๊ณก ์ฌ์ํ๊ธฐ
public Music next() {
if (index < musicList.size() - 1) {
index++;
return play();
// play();
// return musicList.get(index);
} else {
stop();
return null;
}
}
// ์ด์ ๊ณก ์ฌ์ํ๊ธฐ
// ๋ฉ์๋๋ช
pre
public Music pre() {
if (index > 0) {
index--;
return play();
} else {
stop();
return null;
}
}
}