免费注册 查看新帖 |

Chinaunix

广告
  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3760 | 回复: 6
打印 上一主题 下一主题

自己用shell写了个播放列表管理并支持mplayer播放 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-11 01:05 |只看该作者 |倒序浏览
  1. #!/bin/bash
  2. : <<StoreFormat
  3. 用':'将所有播放列表分开 用'|'将播放列表名和歌曲分开 用':'分开的第一项标示当前正在播放的列表和该列表当前正在播放的歌曲
  4. 比如”2|1:aaa|a.mp3 b.mp3 c.mp3:bbb|b.mp3 m.mp3:ccc|c.mp3 d.mp3“表示有3个播放列表,而bbb播放列表的b.mp3正在播放
  5. StoreFormat

  6. #createCommandList
  7. #===================================================
  8. #定义3个数组,
  9. #listNameArray:表示所有播放列表的名字
  10. #listSizeArray:表示每个播放列表包含歌曲数目
  11. #allSongs:表示所有歌曲。并且是按照播放列表0-N排列。DataSource


  12. ######################全局变量定义##################
  13. OPERATEPLAYLISTSOVER="false"
  14. EXITSTATE="false"
  15. EXITAPLAYLIST="false"
  16. DATAFILE="data.txt"
  17. ##################################################
  18. #================================
  19. #检查是否安装了mplayer
  20. #########################
  21. checkEnv(){
  22.         which mplayer &>/dev/null
  23.         if [ $? -ne 0 ]
  24.         then
  25.                 echo "没有安装mplayer,请先安装mplayer"
  26.                 exit 1
  27.         fi
  28. }
  29. #参数 $1:数据源文件,包含所有播放列表和歌曲的文件
  30. initDataSourceFromFile(){
  31.         if [ ! -e $1 ]
  32.         then
  33.                 touch $1
  34.                 echo "0|0">>$1
  35.         fi
  36.         local OLDIFS=$IFS
  37.         IFS=":"
  38.         local index=0
  39.         local k=0;
  40.         local i;
  41.         local flarr;
  42.         for i in $(<$1)
  43.         do
  44.                        
  45.                 listNameArray[$index]=$(echo $i | cut -d'|' -f1)
  46.                 fileLists=$(echo $i | cut -d'|' -f2)
  47.                 OLDIFS_2=IFS
  48.                 IFS=" "
  49.                 flarr=($fileLists)        #转化为数组 或者 (${fileLists[@]})
  50.                 allSongs=(${allSongs[@]} ${fileLists[@]})
  51.                 listSizeArray[$index]=${#flarr[@]}
  52.                 IFS=$OLDIFS_2
  53.                 let "index++"
  54.         done
  55.         IFS=$OLDIFS
  56. }


  57. ####################################################
  58. #参数说明: $1表示要写入的文件名字
  59. writeToFile(){
  60.         echo "" > $1
  61.         local allStr=""
  62.         local plName   
  63.         local song
  64.         local i=0
  65.         local size=0
  66.         local iterator=0;
  67.         for plName in "${listNameArray[@]}"   #####这里加上引号是因为如果array里面包含一项有空格,那么这里输出会出错,比如“new playlist"这种相会变成两项
  68.         do
  69.                 allStr="$allStr$plName|"
  70.                 size=${listSizeArray[$i]}
  71.                 let "size+=0"
  72.                 local count=0;
  73.                 while [ "$count" -ne "$size" ]
  74.                 do
  75.                         allStr="$allStr${allSongs[$iterator]} "
  76.                         let "iterator++"
  77.                         let "count++"
  78.                 done
  79.                 let "i++"
  80.         allStr="$allStr:"
  81.         done
  82.         echo "$allStr" > $1
  83. }
  84. ###########################################################################
  85. #播放列表操作
  86. #判读播放列表序号是否越界
  87. #参数:$1表示播放列表序号
  88. #返回:1表示错误,0表示正确
  89. private_isPlayListIndexOverFlow(){
  90.         local isOverFlow=0
  91.         local playListIndex=$1
  92.         let "playListIndex+=0"
  93.         if [ $? -ne 0 ] || [ $playListIndex -lt 1 ] || [ $playListIndex -gt $((${#listNameArray[@]}-1)) ]
  94.         then
  95.                 #echo "needPlayListIndex == $needPlayListIndex"
  96.                 #echo "${#listNameArray[@]}"
  97.                 echo "播放列表Index 错误,index 应该 1 到 $((${#listNameArray[@]}-1))"
  98.                 return 1
  99.         fi
  100.         return 0;
  101. }
  102. #新建播放列表
  103. #参数 $1 表示播放列表名字 判断播放列表是否存在
  104. private_isPlayListExist(){
  105. #        echo $1 =================**************${#listNameArray[@]} ******
  106.         local ii
  107.         for ii in ${listNameArray[@]}
  108.         do
  109.                 if [ "$1" == "$ii" ]
  110.                 then
  111.                         return 1
  112.                 fi
  113.         done
  114.         return 0
  115. }
  116. #====================================
  117. #参数介绍$1表示需要添加的播放列表index
  118. #向播放列表添加歌曲

  119. addASongToPlayList(){
  120.         showAllPlayLists
  121.         echo "请输入单个歌曲和包含mp3的目录(使用绝对路径): 使用路径时,可以指定搜索深度 如果不指定搜索深度将按照递归查找该目录下的mp3文件"
  122.         echo "单个文件用法:xxx.mp3"
  123.         echo "目录用法: Dir 或者 Dir:1"
  124.         local arg
  125.         read arg
  126.         echo "arg ===================$arg"
  127.         local OLDIFS=$IFS
  128.         IFS=":"
  129.         local arr=($arg)
  130.         local arglen=${#arr[@]}
  131.         let "arglen+=0"
  132.         if [ "$arglen" -gt 2 ] || [ "$arglen" -lt 1 ]
  133.         then
  134.                 echo 参数个数错误
  135.                 IFS=$OLDIFS
  136.                 return
  137.         else
  138.                 local playlistIndex=$1
  139.                 let "playlistIndex+=0"
  140.                 if [ $? -ne 0 ] || [ $playlistIndex -eq 0 ]
  141.                 then
  142.                         IFS=$OLDIFS
  143.                         echo "index 错误,index 应该 1 到 $((${#listNameArray[@]}-1))"
  144.                         return
  145.                 fi
  146.                 local playlistStr=""
  147.                 local dirOrFile=${arr[0]}
  148.                 local findResult
  149.                 if [ ! -e "$dirOrFile" ]
  150.                 then
  151.                         echo "${arr[@]}"
  152.                         IFS=$OLDIFS
  153.                         echo "$dirOrFile 文件或目录不存在"
  154.                         return
  155.                 fi
  156.                 if [ $arglen -eq 1 ]
  157.                 then       
  158.                         if [ -d "$dirOrFile" ]
  159.                         then
  160.                                 local OLDIFSINIF=$IFS
  161.                                 IFS=\n'
  162.                                 local findResult=($(find $dirOrFile -name "*.mp3" -type f))
  163.                                 echo "找到 ${#findResult[@]} 首歌曲"
  164.                        
  165.                                 IFS=$OLDIFSINIF
  166.                         else
  167.                                 echo "参数是单个文件  is $dirOrFile"
  168.                                 if [ ${#dirOrFile} -gt 4 ] || [ "${$dirOrFile:-4}" != ".mp3" ]
  169.                                 then
  170.                                         echo "必须mp3文件"
  171.                                         IFS=$OLDIFS
  172.                                         return
  173.                                 fi
  174.                                 findResult[0]="$dirOrFile"
  175.                         fi
  176.                
  177.                 elif [ "$arglen" -eq 2 ]
  178.                 then
  179.                         echo "参数个数是3"
  180.                
  181.                         if [ ! -d "$dirOrFile" ]
  182.                         then
  183.                                 echo "当参数有3个的时候,第二个参数必须是目录"
  184.                                 IFS=$OLDIFS
  185.                                 return
  186.                         fi
  187.                         local depth=${arr[1]}
  188.                         let "depth+=0" 2>/dev/null
  189.                         if [ $? -ne 0 ]
  190.                         then
  191.                                 echo "当有3个参数时,第3个参数必须是整数"
  192.                                 IFS=$OLDIFS
  193.                                 return
  194.                         fi
  195.                         local OLDIFSINSECONDIF=$IFS
  196.                         IFS=\n'
  197.                         local findResult=($(find $dirOrFile -maxdepth $depth -name "*.mp3" -type f))
  198.         #                echo "------------------------------------------------找到 ${#findResult[@]} 首歌曲"
  199.                        
  200.                         IFS=$OLDIFSINSECONDIF
  201.                
  202.                 else
  203.                         echo "参数错误"
  204.                         IFS=$OLDIFS
  205.                         return
  206.                 fi
  207.                 #echo "-----------------------------findResult  count = ${#findResult[@]}"
  208.                 local OLDIFSSave=$IFS
  209.                 IFS=\n'
  210.                 if [ "${#findResult[@]}" -eq 0 ]
  211.                 then
  212.                         echo "没找到任何歌曲"
  213.                         IFS=$OLDIFS #IFS使用要谨慎,记得要恢复。特别是分支多了,容易漏掉
  214.                         return
  215.                 fi
  216.                 local file
  217.                 echo "是否将所有包含空格的文件名中的空格替换为'_'(注意该项如果选择yes,会真正改变硬盘中文件的名字,选择no只会将不包含空格的mp3文件选择出来):[yes/no]?"
  218.                 read replace
  219.                 if [ "$replace" != "yes" ]
  220.                 then
  221.                         echo "暂不支持包含空格的文件名字,所以不会将包含空格的文件添加进播放列表"       
  222.                 fi
  223.                 local sublen=0
  224.                 local arr;
  225.                 echo "------------------------------------------------找到 ${#findResult[@]} 首歌曲"
  226.                 for file in ${findResult[@]}
  227.                 do       
  228.                         local OLDIFS_4;
  229.                         OLDIFS_4=$IFS
  230.                         IFS=" "
  231.                         arr=($file)
  232.                         sublen=${#arr[@]}
  233.                         let "sublen+=0"
  234.                         IFS=$OLDIFS_4
  235.                         local newName
  236.                         newName=$file
  237.                         if [ $sublen -ne 1 ] #如果长度不是1 ,表示里面有空格:
  238.                         then
  239.                        
  240.                                 if [ "$replace" == "yes" ]
  241.                                 then
  242.                                         newName=${file// /_}
  243.                                         $(mv "$file" "$newName")
  244.                                 else
  245.                                         continue
  246.                                        
  247.                                 fi
  248.                
  249.                 ${listSizeArray[$needPlayListIndex]}        else       
  250.                                 echo "没有空格"
  251.                         fi
  252.                
  253.                         playlistStr="$playlistStr$newName "
  254.                 done
  255.                 #echo "playlistStr ==========================$playlistStr"
  256.                 IFS=$OLDIFSSave
  257.                 local OLDIFS_1=$IFS
  258.                 IFS=" "
  259. #                echo "playindex ========= $playlistIndex==========arrsize ==================${listSizeArray[@]}"
  260.                 local insertIndex=0
  261.                 local step=0
  262.                 while [ "$step" -le "$playlistIndex" ]
  263.                 do
  264. #                        echo "step =============$step"
  265.                         let "insertIndex+=${listSizeArray[$step]}"
  266.                         let "step++"
  267.                 done
  268. #####################################################################
  269. # 将歌曲插入列表
  270.         #        echo  "the position is $insertIndex"  //取得应该插入的index
  271. #                echo "allSongs ==========${#allSongs[@]}"
  272.                 local afterIndexArr=${allSongs[@]:$insertIndex} #先取后面就不用 let "insertIndex++"
  273. #                echo "afterIndexArr ==========${afterIndexArr[@]}"
  274.                 local beforeIndexArr=${allSongs[@]:0:$insertIndex}
  275. #                echo "beforeIndexArr ==========${beforeIndexArr[@]}"
  276. #                echo "playliststr = ${playlistStr[@]}"
  277.                 allSongs=(${beforeIndexArr[@]} ${playlistStr[@]} ${afterIndexArr[@]})
  278. #                echo "all songs ================================="${allSongs[@]}
  279. #插入歌曲结束
  280. ################################################################
  281. ###############################################################
  282. #改变相应列表的大小
  283.                 local playlistStrArr=($playlistStr)
  284. #                echo "playlistIndex =========$playlistIndex        playlistStr  Size = ${#playlistStr[@]}"
  285.                 let "listSizeArray[$playlistIndex]+=${#playlistStrArr[@]}"
  286. #                echo "...........................${listSizeArray[@]}"
  287. #改变相应列表的大小 over
  288. ###############################################################
  289.                 IFS=$OLDIFS_1
  290.                 echo
  291.                 writeToFile $DATAFILE
  292.         fi       
  293.         IFS=$OLDIFS
  294. }


  295. #==============================================
  296. #判断一个播放列表中的歌曲index是否越界
  297. #参数:$1表示歌曲在一个播放列表中的index,$2表示播放列表Index
  298. private_isSongIndexInPlayListOverFlow(){
  299.         local songIndex=$1
  300.         local playlistIndex=$2
  301.         echo "playlistIndex==========$playlistIndex"
  302.         private_isPlayListIndexOverFlow $playlistIndex
  303.         if [ $? -eq 1 ]
  304.         then
  305.                 return 1
  306.         fi
  307.         let "songIndex+=0"
  308.         if [ $? -ne 0 ] || [ $songIndex -lt  0 ] || [ $songIndex -gt ${listSizeArray[$playlistIndex]} ]
  309.         then
  310.                 echo "songIndex == $songIndex"
  311.                 echo "sizearr=========${listSizeArray[@]}"
  312.                 echo "歌曲Index($songIndex) 错误,index 应该 1 到 ${listSizeArray[$playlistIndex]}"
  313.                 return 1
  314.         fi
  315.         return 0;
  316. }

  317. #==============================================
  318. #显示一个播放列表的详细信息
  319. #参数:$1表示要显示歌曲的播放列表的index
  320. private_showAPlayListBasicInfo(){
  321.         local playlistName="${listNameArray[$1]}"
  322.         local playlistSize="${listSizeArray[$1]}"
  323.         echo "================================================"
  324.         echo "                播放列表名字: $playlistName"
  325.         echo "                播放列表大小: $playlistSize"       
  326.         echo "================================================"
  327. }

  328. #==============================================
  329. #从播放列表删除一些歌曲
  330. #参数:$1表示要显示详细信息的播放列表的index
  331. delleteSomeSongsFromThePlayList(){
  332.         showAPlayListAllSongs $1
  333.         echo "输入要删除的歌曲index范围"
  334.         echo "用法:index1:index2(0<index1<index2<${listSizeArray[$1]}"
  335.         echo "输入删除范围"
  336.         local range
  337.         read range
  338.         local OLDIFS=$IFS
  339.         IFS=":"
  340.         local argsArr=($range)
  341.         local arglen=${#argsArr[@]}
  342.         let "arglen+=0"
  343.         if [ "$arglen" -ne 2 ]
  344.         then
  345.                 echo 参数个数错误
  346.                 IFS=$OLDIFS
  347.                 return
  348.         fi
  349.         local deleteFromIndex=${argsArr[0]}
  350.         local deleteToIndex=${argsArr[1]}
  351.         IFS="$OLDIFS"
  352.         private_isSongIndexInPlayListOverFlow $deleteFromIndex $1
  353.         if [ $? -eq 1 ]
  354.         then
  355.                 echo "deleteFromIndex($deleteFromIndex) error"
  356.                 return 1
  357.         fi
  358.         echo "..............delete index=$deleteToIndex===......................$1"
  359.         private_isSongIndexInPlayListOverFlow $deleteToIndex $1
  360.         if [ $? -eq 1 ]
  361.         then
  362.                 return 1
  363.         fi
  364.         let "deleteFromIndex-=1"
  365.         let "deleteToIndex-=1"
  366.         if [ $deleteFromIndex -gt $deleteToIndex ] #如果from大于to,交换两个值
  367.         then
  368.                 local tmpIndex=$deleteFromIndex
  369.                 deleteFromIndex=$deleteToIndex
  370.                 deleteToIndex=$tmpIndex
  371.         fi
  372.         local beginIndex=0
  373.         local endIndex=0
  374.         local step=0
  375.         while [ "$step" -lt "$1" ]
  376.         do
  377.                 echo "step =============$step"
  378.                 let "beginIndex+=${listSizeArray[$step]}"
  379.                 let "step++"
  380.         done
  381.         echo "删除的开始index==========$beginIndex   endIndex======$endIndex"
  382.         let "beginIndex+=deleteFromIndex"
  383.         let "endIndex=beginIndex+deleteToIndex+1"
  384.         local deleteSize=0
  385.         let "deleteSize=deleteToIndex-deleteFromIndex+1"
  386.         echo "删除的开始index==========$beginIndex   endIndex======$endIndex"
  387. ###############################删除歌曲#####################
  388.         local afterIndexArr=${allSongs[@]:$endIndex} #先取后面就不用 let "insertIndex++"
  389. #        echo "afterIndexArr ==========${afterIndexArr[@]}"
  390.         local beforeIndexArr=${allSongs[@]:0:$beginIndex}
  391. #        echo "beforeIndexArr ==========${beforeIndexArr[@]}"
  392. #        echo "playliststr = ${playlistStr[@]}"
  393.         allSongs=(${beforeIndexArr[@]} ${afterIndexArr[@]})
  394.        
  395.         let "listSizeArray[$1]-=deleteSize"
  396.         writeToFile $DATAFILE       
  397. }
  398. #====================================
  399. #格式化输出所有播放列表       
  400. #====================================
  401. private_showPlaylist(){
  402.         local plName
  403.         local k=1
  404. #        echo "before createNew Playlist size :" ${#listNameArray[@]}
  405.         for plName in "${listNameArray[@]:1}"
  406.         do
  407.                 echo "$k-----$plName"
  408.                 let "k++"
  409.         done
  410. }

  411. #显示当前播放列表
  412. showCurrentPlayList(){
  413.         echo "hel"
  414. }
  415. #######################显示一个播放列表的所有歌曲 #########
  416. #===============================
  417. #显示$1播放列表的所有歌曲
  418. #参数解释:$1表示播放列表index
  419. showAPlayListAllSongs(){
  420.         private_showAPlayListBasicInfo $1
  421.         local thePlayListStartIndex=0
  422.         local thePlayListEndIndex=${listSizeArray[$1]}
  423.         local step=0
  424.         while [ $step -lt $1 ]
  425.         do
  426.                 let "thePlayListStartIndex+=${listSizeArray[$step]}"
  427.                 let "step++"
  428.         done
  429.         let "thePlayListEndIndex+=thePlayListStartIndex"
  430.         local aSongName
  431.         local songIndex=0
  432.         for aSongName in ${allSongs[@]:$thePlayListStartIndex:$thePlayListEndIndex}
  433.         do
  434.                 let "songIndex++"
  435.                 echo "$songIndex-----$aSongName"
  436.                
  437.         done
  438. }
  439. #=============================
  440. #新建播放列表
  441. createPlaylist(){
  442.         echo "please input playList name:"
  443.         read plName
  444.         private_isPlayListExist $plName
  445.         if [ $? -eq 1 ]
  446.         then
  447.                 echo " playlist exist"
  448.                 return 1
  449.         else
  450. #                echo "playlist doesnot exit"
  451. #                echo "before createNew Playlist size :" ${#listNameArray[@]}
  452.                 listNameArray=("${listNameArray[@]}" "$plName")
  453. #                echo "after createNew Playlist size :" ${#listNameArray[@]}
  454.                 ##########为新建的播放列表在sizArray中指定一个大小0#####
  455.                 listSizeArray[${#listSizeArray[*]}]=0
  456.                 echo "新建播放列表$plName成功"
  457.                 return 0
  458.         fi
  459. }


  460. ###############################################
  461. #参数解释:$1表示 playingListindex:mode:startIndex
  462. private_playAPlaylist(){
  463.         local plName
  464.         local OLDIFS=$IFSen
  465.         IFS=":"
  466.         local arr=($1)
  467.         local arglen=${#arr[@]}
  468.         let "arglen+=0"
  469.         if [ "$arglen" -ne 3 ]
  470.         then
  471.                 echo "参数个数错误"       
  472.                 IFS=$OLDIFS
  473.                 return
  474.         fi
  475.         local needPlayListIndex=${arr[0]}
  476.         local mode=${arr[1]}
  477.         local lastStartIndex=${arr[2]}
  478.         local playingListSize=${listSizeArray[needPlayListIndex]}
  479.         IFS=$OLDIFS
  480.         if [ $playingListSize -eq 0 ]
  481.         then
  482.                 echo "没有歌曲可播放"
  483.                 return;
  484.         fi
  485. ###########判断播放列表index是否合法####################
  486.         let "needPlayListIndex+=0"
  487.         if [ $? -ne 0 ] || [ $needPlayListIndex -lt 1 ] || [ $needPlayListIndex -gt $((${#listNameArray[@]}-1)) ]
  488.         then
  489.                 #echo "needPlayListIndex == $needPlayListIndex"
  490.                 #echo "${#listNameArray[@]}"
  491.                 echo "index 错误,index 应该 1 到 $((${#listNameArray[@]}-1))"
  492.                 return
  493.         fi
  494. ##########判断模式是否合法################################
  495.         let "mode+=0"
  496.         if [ $? -ne 0 ] || [ $mode -lt 0 ] || [ $mode -gt 1 ]
  497.         then
  498.                 echo "mode==$mode"
  499.                 if [ $mode -ne 0 ]
  500.                 then
  501.                         echo "模式错误,必须是0或1"
  502.                         return
  503.                 fi
  504.                
  505.                
  506.         fi
  507. ########## startindex 是否合法################################
  508.         if [ $lastStartIndex -ne 0 ]
  509.         then
  510.                 let "lastStartIndex=lastStartIndex+0"
  511.                 if [ $? -ne 0 ] || [ $lastStartIndex -lt 0 ] || [ $lastStartIndex -gt ${listSizeArray[$needPlayListIndex]} ]
  512.                 then
  513.                         echo "{listSizeArray[$needPlayListIndex]} ================${listSizeArray[$needPlayListIndex]}"
  514.                         echo "lastStartIndex==$lastStartIndex"
  515.                         echo "startIndex 错误"
  516.                         return
  517.                 fi
  518.         fi
  519.         listNameArray[0]=$needPlayListIndex
  520.        
  521.         local startIndex=0
  522.         local m=0
  523.         local startIndex=0
  524.         local endIndex=${listSizeArray[$needPlayListIndex]}
  525.                
  526.         while [ $m -lt $needPlayListIndex ]
  527.         do
  528.                 let "startIndex+=${listSizeArray[$m]}"
  529.                 let "m++"
  530.         done
  531.         let "startIndex+=lastStartIndex"
  532.         let "endIndex+=startIndex"
  533.         local playFilePath
  534.         local reacordCurrentPlayingSongIndex=$lastStartIndex
  535.         if [ $mode -eq 0 ]
  536.         then
  537.                 echo "顺序播放"
  538.                 for playFilePath in ${allSongs[@]:$startIndex:endIndex}
  539.                 do
  540.                         allSongs[0]=$reacordCurrentPlayingSongIndex;
  541.                         writeToFile $DATAFILE
  542.                         echo "playFilePath ====$playFilePath"
  543.                         let "reacordCurrentPlayingSongIndex++"       
  544.                         echo "reacordCurrentPlayingSongIndex == $reacordCurrentPlayingSongIndex"
  545.                         $(mplayer $playFilePath </dev/null &>/dev/null)
  546.                 done
  547.         else
  548.                 echo "循环播放"
  549.                 while true
  550.                 do
  551.                         for playFilePath in ${allSongs[@]:$startIndex:endIndex}
  552.                         do
  553.                                 allSongs[0]=$reacordCurrentPlayingSongIndex;
  554.                                 writeToFile $DATAFILE
  555.                                 echo "playFilePath ====$playFilePath"
  556.                                 let "reacordCurrentPlayingSongIndex++"       
  557.                                 echo "reacordCurrentPlayingSongIndex == $reacordCurrentPlayingSongIndex"
  558.                                 $(mplayer $playFilePath </dev/null &>/dev/null)
  559.                         done
  560.                 done
  561.                                
  562.         fi
  563.                
  564.        
  565. }
  566. myplayer.tar.gz (6.54 KB, 下载次数: 29)
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-11-11 01:06 |只看该作者
#参数介绍:$1 表示一个播放列表的index
PlayAPlaylist(){
        private_showPlaylist
        echo "==========================================================="
                echo "             输入播放列表的index和播放模式"
                echo "             播放模式:0--顺序播放   1--循环播放"
                echo "             用法:1:0"
        echo "==========================================================="
        echo "请输入要播放的列表index和模式"
        local args
        read args
        private_playAPlaylist "$args:0"
}

#================================
#进入某个播放列表
#参数1表示进入的播放列表index
EnterAPlaylist(){
       
        echo "你现在处于播放列表$1中,可执行以下操作"
        select command in "显示该播放列表所有歌曲" "添加歌曲" "删除歌曲" "播放该列表" "排序"  "合并相同名字的歌曲" "回到上一级"  
        do
                echo " you selected $command"
        break
        done
        case $command in
                "显示该播放列表所有歌曲"
                        showAPlayListAllSongs $1 | more       
                ;;
                "添加歌曲"
                        addASongToPlayList $1
                ;;
                "删除歌曲"
                        delleteSomeSongsFromThePlayList $1
                ;;
                "播放该列表"
                        local args="$1:1:0"
                        private_playAPlaylist $args
                ;;
                "回到上一级"
                        EXITAPLAYLIST="true"
                ;;
        esac
}

#===============================
#删除一个播放列表
DeleteAplaylist(){
        showAllPlayLists
        echo "注意,该删除操作不会删除硬盘文件,只会删除播放列表中的引用"
        echo "请输入需要删除播放列表的index:"
        local deleteIndex
        read deleteIndex
        private_isPlayListIndexOverFlow $deleteIndex
        if [ $? -eq 1 ]
        then
                return
        fi

        local deleteStartIndex=0
        local m=0
#        local deleteEndIndex=${listSizeArray[$deleteIndex]}       
        echo "before delte all songs size =======+"${#allSongs[@]}
        while [ $m -lt $deleteIndex ]
        do
                let "deleteStartIndex+=${listSizeArray[$m]}"
                let "m++"
        done
        let "deleteStartIndex+=0"
        let "deleteEndIndex+=deleteStartIndex"
###########删除播放列表名字###############
        unset listNameArray[$deleteIndex]
##########删除播放列表对应的歌曲#############
        allSongs=(${allSongs[@]:0deleteStartIndex} ${allSongs[@]deleteEndIndex})
        local currentPlayingIndex=${listNameArray[0]}
        if [ $currentPlayingIndex -eq $deleteIndex ]
        then
                listNameArray[0]=0
                listSizeArray[0]=0
        fi
        echo "删除成功!!!!!"       
        writeToFile $DATAFILE
}

#=================================================
#显示所有播放列表
showAllPlayLists(){
        echo "============================================="
        private_showPlaylist
        echo "============================================="
}

#=========================================
#操作播放列表
OperatePlaylists(){
        select command in "新建播放列表" "播放一个列表" "进入一个播放列表" "删除播放列表" "查看所有播放列表"  "回到上一级"
        do
                echo " you selected $command"
        break
        done
        case $command in
                "新建播放列表"
                        createPlaylist $plName
                        if [ $? -eq 0 ]
                        then
                                writeToFile $DATAFILE
                        fi       
                ;;
                "进入一个播放列表"
                        showAllPlayLists
                        echo "选择要进入的播放列表序号"
                        local enterPlaylistIndex
                        read enterPlaylistIndex
                        private_isPlayListIndexOverFlow $enterPlaylistIndex
                        if [ $? -eq 1 ]
                        then
                                return
                        fi
                        EXITAPLAYLIST="false"
                        while [ $EXITAPLAYLIST == "false" ]
                        do
                                EnterAPlaylist $enterPlaylistIndex
                        done
                ;;
                "播放一个列表"
                        PlayAPlaylist
                ;;
                "删除播放列表"
                        DeleteAplaylist
                ;;
                "查看所有播放列表"
                        showAllPlayLists
                ;;
                "回到上一级")
                        OPERATEPLAYLISTSOVER="true"
                ;;
        esac
}
#播放列表操作 over
###############################################################################
#########################################################################
#歌曲操作begin

#显示当前播放的歌曲
showCurrentPlayingSong(){
        local currentPlayinglistIndex=${listNameArray[0]}
        local currentPlayingSongIndex=${allSongs[0]}
       
#################播放列表index检查#########################
        if [ $currentPlayinglistIndex -eq 0 ]
        then
                echo "没有记录"
                return
        fi

        if [ $currentPlayingSongIndex -eq 0 ]
        then
                echo "没有记录"
                return
        fi
        let "currentPlayinglistIndex+=0"
        if [ $? -ne 0 ]
        then
                echo "存储格式错误,播放列表存储首项应该是数字"
                return
        fi
        if [ "$currentPlayinglistIndex" -gt ${#listNameArray[@]} ]
        then
                echo "存储格式错误,播放列表数组越界"
                return
        fi
#################播放歌曲index检查#########################
        let "currentPlayingSongIndex+=0"
        if [ $? -ne 0 ]
        then
                echo "存储格式错误,播放歌曲存储首项应该是数字"
                return
        fi
        if [ "$currentPlayingSongIndex" -gt ${#allSongs[@]} ]
        then
                echo "存储格式错误,播放歌曲数组越界"
                return
        fi
        local currentPlayinglistName=${listNameArray[$currentPlayinglistIndex]}
        local m=0
        local sum=0
       
        while [ $m -lt $currentPlayinglistIndex ]
        do
                let "sum+=${listSizeArray[$m]}"
                let "m++"
        done
        local currentPlayingSongInAllSongsIndex=0
        let "currentPlayingSongInAllSongsIndex=sum+currentPlayingSongIndex"
        echo "currentPlayingSongInAllSongsIndex==============$sum"
        local currentPlayingSongName=${allSongs[$currentPlayingSongInAllSongsIndex]}
        echo "currentPlayingSongInAllSongsIndex===========$currentPlayingSongInAllSongsIndex"
        echo "=================================================================="
        echo "当前正在播放的歌曲信息第$currentPlayinglistIndex个播放列表的第$currentPlayingSongIndex首歌曲"
        echo "播放列表名currentPlayinglistName"
        echo "歌曲名currentPlayingSongName"
        echo "=================================================================="
       
}

#######################################################################
#显示帮助

showHelp(){
        echo "======================================================================"
        echo "       该脚本只提供一个播放列表管理,通过调用mpleyer来完成播放,"
        echo "       当处于播放的时候,该脚本失去对播放的控制,不能进行任何操作,"
        echo "       我也试着想改进,但没找到好的方案。"
        echo
        echo "       如果想扩展脚本功能,只要在select里面添加 功能选单,然后 "
        echo "       对数据源进行相应操作就行了"
        echo
        echo "       第一次运行脚本,先选择2来新建个播放列表,因为并没有默认播放列表"
        echo
        echo "       特别注意,对歌曲的添加删除等操作需要选择进入某个播放列表才能进行!"
        echo "       几乎所有的操作都是输入index(数字)进行的!并不支持名字!!!"
        echo
        echo "       最后,该版本可能会有点bug,欢迎指正和改进"
        echo
        echo "       That All______________________^_^______________Have Fun!!!"
        echo "                                          email:zybdfdz@gmail.com"

        echo "======================================================================"
}

#===========================================
#继续上次的播放
GoOn(){
        local args;
        local lastPlayingListIndex=${listNameArray[0]}
        local lastPlayingSongIndex=${allSongs[0]}
        if [ $lastPlayingListIndex -eq 0 ]
        then
                echo "没有记录"
                return
        fi
        args="$lastPlayingListIndex:1lastPlayingSongIndex"
        private_playAPlaylist $args
}

exitPlayer(){
echo "EXITSTATE in ExitPlayer:" $EXITSTATE
EXITSTATE="true"
}
createCommandList(){
        PS3='请选择命令: '
        select command in "继续" "操作播放列表"     "显示当前正在播放的歌曲" "帮助" "退出"
        do
        echo " you selected $command"
        break
        done
        case $command in
        "显示当前正在播放的歌曲")
                showCurrentPlayingSong
        ;;
       
        "操作播放列表")
                OPERATEPLAYLISTSOVER="false"
                while [ "$OPERATEPLAYLISTSOVER" == "false" ]
                do
                        OperatePlaylists
                done
        ;;
        "继续")
                GoOn
        ;;
        "帮助")
                showHelp
        ;;
        "退出")
                exitPlayer
        ;;
        esac
}


#=============================================Main Begin===============
checkEnv
initDataSourceFromFile $DATAFILE
while [ "$EXITSTATE" == "false" ]
do
createCommandList
done

论坛徽章:
0
3 [报告]
发表于 2011-11-11 08:39 |只看该作者
辛苦,先顶了!
提出两点意见:
1.不要在代码中出现中文,很别扭!
2.代码还可以进一步浓缩和优化!

论坛徽章:
0
4 [报告]
发表于 2011-11-11 09:15 |只看该作者
回复 3# crulat


    论坛里 代码注释 上点中文 不更好吗 ?

     beta 版

论坛徽章:
0
5 [报告]
发表于 2011-11-11 10:40 |只看该作者
回复 3# crulat


1.    我开始也是用的英文的,但是贴上来的时候改成中文了,只是为了让它更容易让人明白。
2.   代码肯定还可以优化的,呵呵。

论坛徽章:
0
6 [报告]
发表于 2011-11-11 10:45 |只看该作者
顺便问个问题,我用 let "a+=0"
来判断a是否是整数,基本都能很正常的工作,但是当a=0的时候,居然 $?是1 ,让人郁闷阿?不知各位还有没有好方法?

let "a+=0"
if [ "$?" -eq 0 ];then
     echo "sucess"
else
    echo "failed"
fi

论坛徽章:
0
7 [报告]
发表于 2011-11-11 12:42 |只看该作者
174 行的样子有个错误  应该改成 “if [ ${#dirOrFile} -le 4 ] || [ ${#dirOrFile} -gt 4 ] && [ "${dirOrFile-4)}" != ".mp3" ]”
255行多余的。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP