본문 바로가기

스크랩

[스크랩] XML연동하여 mp3 player 만들기

. XML연동하여 mp3 player 만들기
 
 
 
xml 문서에다 mp3 음악 목록을 작성한뒤, 이것을 바탕으로 flash에서 재생해보는 것을 구현해 보겠습니다. 다음은 xml 문서입니다.

<?xml version="1.0" encoding="euc-kr"?>
<list>
<song>
<title>날 울리지마</title>
<artist>이정</artist>
<url>music/이정 - 날울리지마.mp3</url>
</song>
<song>
<title>언제라도</title>
<artist>박화요비</artist>
<url>music/박화요비 - 언제라도.mp3</url>
</song>
<song>
<title>Not Going Anywhere</title>
<artist>Keren Ann</artist>
<url>music/Keren Ann- Not Going Anywhere.mp3</url>
</song>
<song>
<title>꿈에</title>
<artist>이수영</artist>
<url>music/이수영-꿈에.mp3</url>
</song>
<song>
<title>fragile</title>
<artist>Sting</artist>
<url>music/Sting - fragile.mp3</url>
</song>
</list>
 
<song></song>의 태그안에 노래가 정의되어 있으며,그 안에는 title,artist,url으로 구성돼어 있습니다.그럼 액션에 빠져보십다..
 
_root.onLoad = function() {
 load_xml();
};

function load_xml() {
 System.useCodepage = true;
 myXML = new XML();
 myXML.ignoreWhite = true;
 myXML.onLoad = function(success) {
  total_num = myXML.firstChild.childNodes.length;
 
 //<song>태그의 갯수-즉 노래의 총 갯수를 알아보기 위한 구문입니다.
  
 current_num = random(total_num);  
 
//처음 시작하면 total_num 범위 안의 랜덤 번호를 발생시켜서
// 정의된 xml 문서 중에서 랜덤하게 노래를 가져오기 위함입니다. 
//current_num는 현재 재생중인 곡 번호(노드)를 의미합니다

  mp3_sound();
 };
 myXML.load("list.xml");
}

stop_btn.onRelease = function() {
 stopAllSounds();
 mp3_info.text = "stop";
};
play_btn.onRelease = function() {
 stopAllSounds;
 mp3_sound();
};
back_btn.onRelease = function() {
 current_num--;
 if (current_num<0) {
  current_num = total_num-1;
 
//이전 버튼을 눌렀을때, 곡의 젤 첫부분에 해당하면 마지막 부분을 불러옵니다.

 }
 stopAllSounds;
 mp3_sound();
};
forward_btn.onRelease = function() {
 current_num++;
 if (current_num == total_num) {
  current_num = 0;
 
//다음 버튼을 눌렀을때, 곡의 마지막 부분에 해당하면 처음 부분을 불러옵니다.

 }
 stopAllSounds;
 mp3_sound();
};

function mp3_sound() {
 mp3_url = myXML.firstChild.childNodes[current_num].childNodes[2].firstChild;
 music_title = myXML.firstChild.childNodes[current_num].childNodes[0].firstChild;
 music_artist = myXML.firstChild.childNodes[current_num].childNodes[1].firstChild;
 mp3_info.text = (current_num+1)+". "+music_artist+" - "+music_title;
 
//텍스트필드에 곡번호와 음악가, 제목을 표시합니다. xml 노드는 0부터 시작하기에 +1을 해줍니다.

 
mySound = new Sound();
 mySound.loadSound(mp3_url, true);
 mySound.onSoundComplete = function() {
 current_num++;
 if (current_num == total_num) {
 current_num = 0;
  }
 
//한곡의 재생이 끝나면 다음곡으로 넘어갑니다.

 
mp3_sound();
 };
}


xml 문서에서 정의한 노래목록을 불러와서 간단히 정보를 나타내고 노래를 들려주는 기능을 구현해 보았습니다. 그 밖에 positon 등의 값을 이용해서 현재 재생시간을 나타내거나, title이나 artist등에 정보가 기재되어 있지 않다면 mp3의 id3 값을 얻어와 나타낼수도 있을것 같네요.
 
.xml      .fla    출처 - 네이버 플사모 카페
 
. 적용사이트 - http://home.kosha.net/~pogo111
 
[출처 - pogo11]

 
가져온 곳: [♥생을 그리는 작업실♥]  글쓴이: 글짱 바로 가기