1. 程式人生 > >unity3d簡單的音樂播放器

unity3d簡單的音樂播放器

這幾天用u3d做應用,感覺沒效率,儘管它可以用C#寫,但是感覺不行,所以我還是早點收手,不做應用了,把這幾天做的一個小東西給大家玩玩,音樂播放器~~~

我就直接貼程式碼了哈,裡面有一些註釋的,呵呵~~~~但是手機上的編碼問題沒解決哦~~~有知道的同學,麻煩指點一下,我的qq:397319689(有點笨);

import System.IO;
import System.Collections;
import System.Text;
var www : WWW;
var file:ArrayList;
var scrollPosition :Vector2= Vector2.zero;
var scrollVelocity:float  = 0f;
var timeTouchPhaseEnded:float  = 0f;
var inertiaDuration:float = 0.5f;
var lastDeltaPos:Vector2 ;
var skin:GUISkin;

function Start () {
    file=new ArrayList();
    he("C:\\","*.mp3",file);  
}

var play:Texture;
var pause:Texture;
var next:Texture;
var last:Texture;
var sound:Texture;

function OnGUI(){
  GUI.skin=skin;
  var y:int=0;   
  scrollPosition = GUI.BeginScrollView( Rect(0, 0, Screen.width, Screen.height), scrollPosition,  Rect(0, 0,Screen.width, file.Count*60), false, false);
   for(var f:String in file){      
      //把系統編碼轉成utf-8,因為android上是utf-8  
      var utf:Encoding =Encoding.UTF8;//utf-8
      var gb:Encoding =Encoding.Default;//系統的編碼
      var temp:byte[] = gb.GetBytes(f);
      var temp1:byte[] = Encoding.Convert(gb, utf, temp);
      var f1:String=utf.GetString(temp1);
      
      //用lastindexof和substring方法獲取檔名(不包括路徑)
      var p:int=f1.LastIndexOf('\\',f1.Length-1); //必須是'\\',如果是‘\’會報錯的。如果是手機要寫‘/’      
      var f2:String=f1.Substring(p+1,f1.Length-1-p);
      var b=GUI.Button(Rect(0,y,Screen.width,60),"   "+f2);
      
      if(b){
        www=new WWW("file://"+f);    //記住這裡面的“//”別寫成“\\”就行哈; 
        audio.clip=www.audioClip;
       }
       y+=60;     
    }
  GUI.EndScrollView();
 
}


function Update (){
  if(Input.touchCount > 0){
    if(Input.GetTouch(0).phase == TouchPhase.Moved){
      scrollPosition.y += Input.GetTouch(0).deltaPosition.y;
      lastDeltaPos = Input.GetTouch(0).deltaPosition;
    } 
    else if (Input.GetTouch(0).phase == TouchPhase.Ended){
      print ("End:"+lastDeltaPos.y+"|"+Input.GetTouch(0).deltaTime);
      if(Mathf.Abs(lastDeltaPos.y)> 20.0f){
         scrollVelocity = lastDeltaPos.y * 0.5/ Input.GetTouch(0).deltaTime;
         print(scrollVelocity);
       }
      timeTouchPhaseEnded = Time.time;
    }

  }

  else{
     if(scrollVelocity != 0.0f){
       var t:float = (Time.time - timeTouchPhaseEnded)/inertiaDuration;
       var frameVelocity:float = Mathf.Lerp(scrollVelocity, 0, t);
       scrollPosition.y += frameVelocity * Time.deltaTime;
       if (t >= inertiaDuration)
           scrollVelocity = 0;
     }

  }
  if(Input.GetKey(KeyCode.Escape)){
      Application.Quit();
   }
   if(!audio.isPlaying)
   audio.Play();
}

function he(path:String,pattern:String,al:ArrayList){
 if(path!=null){    
   var f1:String[]=Directory.GetFiles(path,pattern); 
   var d1:String[];        
   for(var f11:String in f1){
      al.Add(f11);
   }  
   try{ 
   d1=Directory.GetDirectories(path);
   for(var d11:String in d1){
      try{ he(d11,pattern,al);}
      catch(Exception){}                 
    }     
   }catch(Exception){} 

  }
}