User:Free Portal/Sandbox/LSL Library/Simple Music Player

From Second Life Wiki
Jump to navigation Jump to search

Simple Random Vendor

간단한 땅의 배경음악 재생기입니다.

스크립트 사용 방법

play_list라는 노트카드와, 아래의 스크립을 작성해서 오브젝트안에 함께 담아주면 됩니다. 현재 23곡을 재생중인, 제땅의 음악 플레이어는 문제 없이 잘 돌아가고 있습니다. 몇곡까지 등록 가능한지 메모리 테스트는 해보지 않았습니다. 만 약 리스트에 음악을 너무 많이 담아서 플레이어가 동작 하지 않는다면, 적게 넣으세요. 그냥 대충 봐도 직관적인 메뉴이므로 별다른 설명은 하지 않겠습니다.플레이어가 동작을 시작했을때, 오브젝트를 터치하면, 다이얼 메뉴가 뜹니다.만 약 플레이리스트에서 21번의 음악을 플레이 하고 싶다면, 숫자 패드를 사용하시면 됩니다. 2를 누르고 다시 1을 누른뒤에 DONE 버튼을 눌러주면 됩니다.

*주의 : 저는 스크립만 제작했을뿐, 땅에 링크되는 음원에 대해서는 책임이 없음을 미리 알려드립니다.

play_list

<lsl>

플레이 리스트 사용 방법
================================================================
제목
음악 url
플레이 타임 (초단위로 입력)
================================================================

제목 01 http://aaa.com/01.mp3 123

================================================================

제목 02 http://aaa.com/02.mp3 123

================================================================

</lsl>

Simple_music_player.lsl

<lsl>// Simple Music Player // Copyright (C) 2010 Free Portal // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

// Simple Music Player // @author Free Portal // ------------------------------------------------------------------------ // May 3, 2009 v0.2 - Notecard play_list add // Apr 13, 2009 v0.1 - Simple Music Player, orig code // ------------------------------------------------------------------------

// ---------------------------------------------------------------------------- // Global Variables: // ----------------------------------------------------------------------------

integer musicCount = 0; integer playerState = 0; integer noteLine = 0; integer playListLine = 0; integer channelDialog; integer listenID; string numberPad = ""; list playList = []; list randomList = []; key toucherID; key kQuery;

// ---------------------------------------------------------------------------- // Constants: // ---------------------------------------------------------------------------- integer PLAYER_LOOP_A = 0; integer PLAYER_LOOP_1 = 1; list MAINMENU = ["<<", ">>", "Loop 1" , "PlayList","Shuffle",

                               "Loop All"];                               

list SUBMENU = ["0", "Back", "Done", "7", "8", "9", "4", "5",

                               "6", "1", "2", "3"];

string msg = "Zukebox"; string playListName = "play_list";

// ---------------------------------------------------------------------------- // User Defined Functions // ----------------------------------------------------------------------------

// ---------------------------------------------------------------------------- // isInventoryExists // ---------------------------------------------------------------------------- // @param: // string name // integer type // // @return: // integer // ---------------------------------------------------------------------------- integer isInventoryExists(string name, integer type) {

   return (llGetInventoryType(name) == type) ^ (!~type);

}

// ---------------------------------------------------------------------------- // selectSubMenu // ---------------------------------------------------------------------------- // @param: // key str // // @return: // null // ---------------------------------------------------------------------------- selectSubMenu (string str) {

   if (~llListFindList(SUBMENU, [str])) {
      llDialog(toucherID, "ZukeBox\n Selected Track -> " + selectMusic(str),
       SUBMENU, channelDialog);
   }
   else
       return;

}

// ---------------------------------------------------------------------------- // makeShuffleLis // ---------------------------------------------------------------------------- // @param: // void // // @return: // null // ---------------------------------------------------------------------------- makeShuffleList() {

   integer i;
   integer j;
   integer maxLength;
  
   randomList = [];
   maxLength = llGetListLength(playList) / 3;
  
   llSetText(llGetObjectName() + "\n Now Loading...", <1.0, 1.0, 0.0>, 1.0);
   for (i = 0; i < maxLength;i++) {
       randomList = llListReplaceList(randomList, [i], i, i);
   }
  
   randomList = llListRandomize(randomList, 1);

}

// ---------------------------------------------------------------------------- // printMusicList // ---------------------------------------------------------------------------- // @param: // void // // @return: // null // ---------------------------------------------------------------------------- printMusicList() {

   integer i;
   string str;
  
   for (i = 0; i < llGetListLength(playList) / 3; i++)
       str = str + "\n " + ((string)(i + 1)) + " : "
               + llList2String(playList,
               (integer)llList2Integer(randomList, i) * 3);
  
   llInstantMessage(toucherID,"\n ====== Play List ======"+ str);

}

// ---------------------------------------------------------------------------- // printTitle // ---------------------------------------------------------------------------- // @param: // void // // @return: // null // ---------------------------------------------------------------------------- printTitle() {

   string tmp;
   if (playerState == PLAYER_LOOP_A)
       tmp = "(∞ A)\n";
   else
       tmp = "(∞ 1)\n";
      
   llSetText(llGetObjectName() + tmp +
       llList2String(playList,
       (integer)llList2Integer(randomList, musicCount) * 3),
       <1.0, 1.0, 0.0>, 1.0);

}

// ---------------------------------------------------------------------------- // playMusic // ---------------------------------------------------------------------------- // @param: // void // // @return: // null // ---------------------------------------------------------------------------- playMusic() {

   printTitle ();
   llSetParcelMusicURL ("-");
   llSetParcelMusicURL (llList2String(playList,
       (integer)llList2Integer(randomList, musicCount) * 3 + 1));
   llSetTimerEvent (llList2Float(playList,
       (integer)llList2Integer(randomList, musicCount) * 3 + 2));   

}

// ---------------------------------------------------------------------------- // playPre // ---------------------------------------------------------------------------- // @param: // void // // @return: // null // ---------------------------------------------------------------------------- playPre() {

   if (musicCount-- < 0)
       musicCount = llGetListLength(playList) / 3;
      
   playMusic();

}

// ---------------------------------------------------------------------------- // playNext // ---------------------------------------------------------------------------- // @param: // void // // @return: // null // ---------------------------------------------------------------------------- playNext() {

   if(musicCount++ > llGetListLength(playList) / 3)
       musicCount = 0;
      
   playMusic();

}

// ---------------------------------------------------------------------------- // playNext // ---------------------------------------------------------------------------- // @param: // string str // // @return: // string // ---------------------------------------------------------------------------- string selectMusic(string str) {

   integer i;
   if (llStringLength(numberPad) == 2) {
       if (str== "Back") {
           string tempStr = numberPad;
          
           for (i = 0; i < llStringLength(tempStr)-1; i++)
               tempStr = llGetSubString (numberPad, i, i);
           numberPad = tempStr;
       }
       else       
           numberPad = str;
   }
   else {
       if (str == "Back")
           numberPad = "";
       else
           numberPad = numberPad + str;
   }
  
   return numberPad;

}

// ---------------------------------------------------------------------------- // selectedMusic // ---------------------------------------------------------------------------- // @param: // void // // @return: // null // ---------------------------------------------------------------------------- selectedMusic() {

   if (((integer)numberPad) -1 > llGetListLength(randomList) ||
           0 >= llStringLength(numberPad))
       numberPad = (string)musicCount;
   else {
       musicCount = ((integer)(numberPad)) -1;
       playMusic();
   }
   numberPad = "";

}

// ---------------------------------------------------------------------------- // init // ---------------------------------------------------------------------------- // @param: // void // // @return: // null // ---------------------------------------------------------------------------- init() {

   channelDialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),
                   -5,-1)));
   musicCount = 0;
   playerState = 0;               
   playerState = PLAYER_LOOP_A;
   makeShuffleList();       
   playMusic();   

}

// ---------------------------------------------------------------------------- // Main Script // ---------------------------------------------------------------------------- default {

   state_entry() {
       llSetText(llGetObjectName() + "\n" + " ", <1.0, 1.0, 0.0>, 1.0);               
       if (llGetListLength(playList) <= 0) {
           if (isInventoryExists(playListName, INVENTORY_NOTECARD))
               state getPlayList;
           else {
               llSetText(llGetObjectName() + "\n" + " ",
                   <1.0, 1.0, 0.0>, 1.0);       
               llOwnerSay(playListName + "가 없습니다.");
           }               
       }
      
       init ();
   }
  
   timer() {
       if (playerState == PLAYER_LOOP_A)
           playNext();
       else if (playerState == PLAYER_LOOP_1)
           playMusic();
   }
   touch_start(integer total_number) {
       toucherID = llDetectedKey(0);
       llDialog(toucherID, msg, MAINMENU, channelDialog);
       listenID = llListen( channelDialog, "", toucherID, "");
   }
  
   changed(integer change) {
       if (change & CHANGED_INVENTORY)
           if(isInventoryExists(playListName, INVENTORY_NOTECARD))
               state getPlayList;
           else {
               playList = [];               
               llSetText(llGetObjectName() + "\n" + " ", <1.0, 1.0, 0.0>, 1.0);
               llOwnerSay(playListName + "가 없습니다.");
           }
   }
  
   listen(integer channel, string name, key id, string choice) {
       if (choice == "-") {
           llDialog(toucherID, msg, MAINMENU, channelDialog);
       } else if (choice == "Shuffle") {
           makeShuffleList();           
           playMusic();
       } else if (choice == "Loop 1") {
           playerState = PLAYER_LOOP_1;
           printTitle();
       } else if (choice == "PlayList") {
           printMusicList ();      
           llDialog(toucherID, "ZukeBox\n Now Playing Track :: " +
               (string)(musicCount + 1) + "\n Title :: " +
               llList2String(playList,
               (integer)llList2Integer(randomList, musicCount) * 3),
               SUBMENU, channelDialog);
       } else if (choice == "Loop All") {
           playerState = PLAYER_LOOP_A;
           printTitle();
       } else if (choice == "<<")
           playPre();
       else if (choice == ">>")
           playNext();
       else if (choice == "Done")
           selectedMusic ();
       else
           selectSubMenu(choice);
   }

}

state getPlayList {

   state_entry() {
       playList = [];
       noteLine = 0;
       playListLine = 0;
                  
       llSetText(llGetObjectName() + "\n Now Loading...",
           <1.0, 1.0, 0.0>, 1.0);
       kQuery = llGetNotecardLine(playListName, noteLine);
   }
  
   dataserver(key query_id, string data) {
        if (query_id == kQuery) {
           if (data != EOF) {
               if (llSubStringIndex(data, ";") != 0) {
                   playList = llListReplaceList(playList, [data],
                               playListLine, playListLine);
                   playListLine++;
               }
               noteLine++;
               kQuery = llGetNotecardLine("play_list", noteLine);
           }
           else
               state default;
       }
   }

}</lsl>