Background Music Player

Checking the Unity forums, “Alienchild” has supplied a fantastic script for a background music player…


#pragma strict

var RandomStart : boolean = false;

var SongList : AudioClip[];

private var SongNumber : int = 0;

private var Music : AudioSource;

private var timer : float;

private var play : boolean = true;

function Start()

{

    gameObject.AddComponent(AudioSource);

    if(RandomStart)

    {

        SongNumber = Random.Range(0,SongList.Length);

    }

    Music = gameObject.GetComponent(AudioSource);

    Music.loop = false;

    Music.playOnAwake = true;

    Music.clip = SongList[SongNumber];

    Music.volume = 0.6;

    audio.Play();

}

function Update()

{

    timer = timer + Time.deltaTime;

    if(timer > (audio.clip.length + 1) && play == true)

    {

        play = false;

        if(SongNumber < (SongList.Length-1))

        {

            SongNumber = SongNumber + 1;

        }

        else

        {

            SongNumber = 0;

        }

        Music.clip = SongList[SongNumber];

        audio.Play();

        timer = 0;

        play = true;

    }

}

And here is a version in C# modified by RaskVann

using UnityEngine;

public class MusicPlayer : MonoBehaviour

{

    public bool RandomStart = false;

    public AudioClip[] SongList;

    private int SongNumber = 0;

    private AudioSource Music;

    private float timer;

    private bool play = true;

    void Start()

    {

        Music = gameObject.AddComponent();

        //Switch to GetComponent if you are fine with using a pre-existing AudioSource

        //Music = gameObject.GetComponent();

        if(RandomStart)

        {

            SongNumber = Random.Range(0,SongList.Length);

        }

        Music.loop = false;

        Music.playOnAwake = true;

        if(SongList.Length > 0)

        {

            Music.clip = SongList[SongNumber];

        }

        Music.volume = 0.6f;

        Music.audio.Play();

    }

    void Update()

    {

        timer = timer + Time.deltaTime;

        if(SongList.Length > 0 && timer > (Music.clip.length + 1) && play == true)

        {

            play = false;

            if(SongNumber < (SongList.Length-1))

            {

                SongNumber = SongNumber + 1;

            }

            else

            {

                SongNumber = 0;

            }

            Music.clip = SongList[SongNumber];

            Music.audio.Play();

            timer = 0;

            play = true;

        }

    }

}
Advertisements

About AK Studio Development

We are a small indie development group from New Zealand. We have been dabbling in iPhone app and game development since 2010 and are continuously learning new tricks. As we learn, we create. We look forward to sharing all our ideas through our current and future apps! AK Studio

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: