Building an audioplayer

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:

// The AudioPlayerScript  

var gSkin : GUISkin;
//private var showAudioPlayer = false;
var showAudioPlayer = false	;
// The actual AudioPlayer Window -> 400x300
var audioPlayerRect = Rect (Screen.width/2-200, Screen.height/2-170, 400,300);

//For Scrollview -The variable to control where the scrollview 'looks' into its child elements.
var scrollPosition : Vector2;

//an image for the track
var clipPic : Texture2D;

// AudioLibrary
var tracks : AudioClip[];


function OnGUI() { 
 	if (gSkin) 
  		GUI.skin = gSkin; 
 	else 
  		Debug.Log("AudioPlayerGUI: GUI Skin object missing!"); 
  		
  	
  	if(showAudioPlayer)
		audioPlayerRect = GUILayout.Window (0, audioPlayerRect, AudioPlayerWindow, "AudioPlayer");				
  		
}//OnGUI

// Make the contents of the AudioPlayerWindow 
function AudioPlayerWindow (windowID : int) {
	
	//AudioPlayer Group
	GUILayout.BeginVertical();
		
		//a centered Pic for the track
		GUILayout.BeginHorizontal();
			GUILayout.FlexibleSpace();
			GUILayout.Label(clipPic, GUILayout.MaxWidth(100), GUILayout.MaxHeight(100));//the pic
			GUILayout.FlexibleSpace();	
		GUILayout.EndHorizontal();
		
		// Begin a scroll view. All rects are calculated automatically 
		scrollPosition = GUILayout.BeginScrollView (scrollPosition, GUILayout.Width (100), GUILayout.Height (100));
		//create buttons for all audioclips 
		for (var track: AudioClip in tracks){
			audio.clip = track;
			trackName = audio.clip.name;
			
			//NOT WORKING YET!!!!!!!! Play the track of the button
			
			if (GUILayout.Button(trackName,GUILayout.MaxWidth(350))){
				audio.clip = track;
				print(trackName);
				audio.Play();
			   	audio.loop= false;
			}//if	
		
		}//for	
		
		// End the scrollview we began above.
		GUILayout.EndScrollView ();

		
		GUILayout.Space(10);
		
		GUILayout.BeginHorizontal ();
		
			GUILayout.FlexibleSpace();
		
			if (GUILayout.Button ("","prevButton", GUILayout.MaxWidth (30),GUILayout.MaxHeight (30))) {
				//step back in tracks array ??
			}//if
		
			GUILayout.Space(10);
		
			if (GUILayout.Button ("","stopButton", GUILayout.MaxWidth (30),GUILayout.MaxHeight (30))) {
				audio.Stop();	
			}//if
		
			if (GUILayout.Button ("","pauseButton", GUILayout.MaxWidth (30),GUILayout.MaxHeight (30))) {
				audio.Pause();
			}//if
		
		
			if (GUILayout.Button ("","playButton", GUILayout.MaxWidth (30),GUILayout.MaxHeight (30))) {
				audio.Play();
			}//if

			GUILayout.Space(10);
				
			if (GUILayout.Button ("","nextButton", GUILayout.MaxWidth (30),GUILayout.MaxHeight (30))) {
				//step forward in tracks array	??	
			}//if

			GUILayout.FlexibleSpace();
			GUILayout.FlexibleSpace();

			// close Window
			if (GUILayout.Button ("Close", GUILayout.MaxWidth (70))) 
				showAudioPlayer = false; 				
			
		
		GUILayout.EndHorizontal ();
		
	GUILayout.EndVertical();
}//AudioPlayerWindow

function OnTriggerEnter () {
	showAudioPlayer = true;
}

function OnTriggerExit () {
	showAudioPlayer = false;
}

@script ExecuteInEditMode
@script RequireComponent(AudioSource)

Let me know if anything I have done with mine can help you, and I’ll give you my code:

http://teamuv.net/jessy/music/

Do you have something assigned to the audio clip? Are your speakers turned on? :slight_smile: Because that code will work as-is.

–Eric

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 :smile: )

Hi Eric

yes my speakers are on :lol: 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

91831--3605--$screenshot_163.jpg