Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Building an audioplayer

Discussion in 'Scripting' started by R.A.E., Sep 5, 2008.

  1. R.A.E.

    R.A.E.

    Joined:
    Apr 24, 2008
    Posts:
    44
    I try to build an audioplayer. It will appear if the player has triggered it by hitting a collider.In the moment the autonaming of the buttons works (although the apostroph gets replaced with an underscore).

    But the main thing(playing the actual audio) is unfortunatly not working.

    Also i need a way of stepping tracks. Thought of increasing or decreasing the index of the tracks array, but i need a little help here.

    Later i want to have a pic for each track(but first i have to get the audiopart working).

    I´m thankful for any hint or advice.

    This is the code so far:

    Code (csharp):
    1.  
    2. // The AudioPlayerScript  
    3.  
    4. var gSkin : GUISkin;
    5. //private var showAudioPlayer = false;
    6. var showAudioPlayer = false ;
    7. // The actual AudioPlayer Window -> 400x300
    8. var audioPlayerRect = Rect (Screen.width/2-200, Screen.height/2-170, 400,300);
    9.  
    10. //For Scrollview -The variable to control where the scrollview 'looks' into its child elements.
    11. var scrollPosition : Vector2;
    12.  
    13. //an image for the track
    14. var clipPic : Texture2D;
    15.  
    16. // AudioLibrary
    17. var tracks : AudioClip[];
    18.  
    19.  
    20. function OnGUI() {
    21.     if (gSkin)
    22.         GUI.skin = gSkin;
    23.     else
    24.         Debug.Log("AudioPlayerGUI: GUI Skin object missing!");
    25.        
    26.    
    27.     if(showAudioPlayer)
    28.         audioPlayerRect = GUILayout.Window (0, audioPlayerRect, AudioPlayerWindow, "AudioPlayer");             
    29.        
    30. }//OnGUI
    31.  
    32. // Make the contents of the AudioPlayerWindow
    33. function AudioPlayerWindow (windowID : int) {
    34.    
    35.     //AudioPlayer Group
    36.     GUILayout.BeginVertical();
    37.        
    38.         //a centered Pic for the track
    39.         GUILayout.BeginHorizontal();
    40.             GUILayout.FlexibleSpace();
    41.             GUILayout.Label(clipPic, GUILayout.MaxWidth(100), GUILayout.MaxHeight(100));//the pic
    42.             GUILayout.FlexibleSpace(); 
    43.         GUILayout.EndHorizontal();
    44.        
    45.         // Begin a scroll view. All rects are calculated automatically
    46.         scrollPosition = GUILayout.BeginScrollView (scrollPosition, GUILayout.Width (100), GUILayout.Height (100));
    47.         //create buttons for all audioclips
    48.         for (var track: AudioClip in tracks){
    49.             audio.clip = track;
    50.             trackName = audio.clip.name;
    51.            
    52.             //NOT WORKING YET!!!!!!!! Play the track of the button
    53.            
    54.             if (GUILayout.Button(trackName,GUILayout.MaxWidth(350))){
    55.                 audio.clip = track;
    56.                 print(trackName);
    57.                 audio.Play();
    58.                 audio.loop= false;
    59.             }//if  
    60.        
    61.         }//for 
    62.        
    63.         // End the scrollview we began above.
    64.         GUILayout.EndScrollView ();
    65.  
    66.        
    67.         GUILayout.Space(10);
    68.        
    69.         GUILayout.BeginHorizontal ();
    70.        
    71.             GUILayout.FlexibleSpace();
    72.        
    73.             if (GUILayout.Button ("","prevButton", GUILayout.MaxWidth (30),GUILayout.MaxHeight (30))) {
    74.                 //step back in tracks array ??
    75.             }//if
    76.        
    77.             GUILayout.Space(10);
    78.        
    79.             if (GUILayout.Button ("","stopButton", GUILayout.MaxWidth (30),GUILayout.MaxHeight (30))) {
    80.                 audio.Stop();  
    81.             }//if
    82.        
    83.             if (GUILayout.Button ("","pauseButton", GUILayout.MaxWidth (30),GUILayout.MaxHeight (30))) {
    84.                 audio.Pause();
    85.             }//if
    86.        
    87.        
    88.             if (GUILayout.Button ("","playButton", GUILayout.MaxWidth (30),GUILayout.MaxHeight (30))) {
    89.                 audio.Play();
    90.             }//if
    91.  
    92.             GUILayout.Space(10);
    93.                
    94.             if (GUILayout.Button ("","nextButton", GUILayout.MaxWidth (30),GUILayout.MaxHeight (30))) {
    95.                 //step forward in tracks array  ?? 
    96.             }//if
    97.  
    98.             GUILayout.FlexibleSpace();
    99.             GUILayout.FlexibleSpace();
    100.  
    101.             // close Window
    102.             if (GUILayout.Button ("Close", GUILayout.MaxWidth (70)))
    103.                 showAudioPlayer = false;               
    104.            
    105.        
    106.         GUILayout.EndHorizontal ();
    107.        
    108.     GUILayout.EndVertical();
    109. }//AudioPlayerWindow
    110.  
    111. function OnTriggerEnter () {
    112.     showAudioPlayer = true;
    113. }
    114.  
    115. function OnTriggerExit () {
    116.     showAudioPlayer = false;
    117. }
    118.  
    119. @script ExecuteInEditMode
    120. @script RequireComponent(AudioSource)
    121.  
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Do you have something assigned to the audio clip? Are your speakers turned on? :) Because that code will work as-is.

    --Eric
     
  4. R.A.E.

    R.A.E.

    Joined:
    Apr 24, 2008
    Posts:
    44
    Thanks for your answers, still need a little help

    Hi Jessy,

    thanks for the offer. Would definitly like to check your code since it has the functionality (looping, skipping, tracktitle etc.) I´m looking for in it and is well done (nice music btw :D )

    Hi Eric

    yes my speakers are on :D but still no audio playing.

    Also it is always the last track in the array that is diplayed in the AudioSource (but not playing either). I get that one clip playing if i check Play on Awake.
    Seems that something with the assigning of the tracks to the AudioSource is not working as I want it.

    Here comes a screenshot of what i try to achieve.
    under the clipPic shold come the Title Display and a progressbar like in Jessys player
     

    Attached Files: