Jukebox for Game Music

Here’s a C# class that provides very basic jukebox functionality in a game. It can be used to manage multiple persistent audio clips.

[Edit: This wiki had moved and this is the updated link.]

I was just looking at your code on the wiki. Nice job. and thanks!

I am playing around with your code, but my C# skills stink. How would I implement this in my game?

Here is some quick code I put together… but it does not do what I want it to do. … Play a song.

+++++++++++++++++++++++++++
using UnityEngine;
using System.Collections;

public class JukeBoxTest : MonoBehaviour {
AudioClip myclip;

// Use this for initialization
void Start () {
JukeboxController jukebox = new JukeboxController();
jukebox.AddClip(“mysong”, myclip);
jukebox.PlayClip(“mysong”);
//jukebox.StopClip();
}

// Update is called once per frame
void Update () {

}
}
+++++++++++++++

Does it require a string path instead of a game audio object?

Could I call this code through Javascript? If I can what would be the change from C#?

Thanks in advance.

There are many ways to skin the cat, but the jukebox object is primarily meant to live in a game manager. See this script as an example of something that can be used as the basis for a game manager:

In your code snippet, you have the jukebox object lasting only until the Start() method concludes. Things like sound managers need to persist.

It looks like an easy fix (here, at least) would be to move your declaration/definition up to be a class variable. For example,

. . .
JukeboxController jukebox = new JukeboxController();
public class JukeBoxTest : MonoBehaviour {
. . .

[Edit] TYPO ALERT

The above should be:

. . .
public class JukeBoxTest : MonoBehaviour {
    JukeboxController jukebox = new JukeboxController();
. . .

Sorry about the error. :sweat_smile:

Thanks tsphillips!

I will give it a shoot.

See the above edit. I made an error and transposed the lines. Sorry about that. :sweat_smile: