Music not looping

Hi there,

Scratched my head for hours on this one, I have tried multiple things but my music is not looping.

I have checked the loop button in Unity and I have also coded this into monodevelop…

using UnityEngine;
using System.Collections;

public class MusicPlayer : MonoBehaviour {

static MusicPlayer instance = null;
public AudioClip clip;  // Drag the clip here on the editor
private AudioSource au;

void Awake() {
	if (instance != null) {
		Destroy (gameObject);
	} else {
		instance = this;

		GameObject.DontDestroyOnLoad(gameObject);	
	}
}



void Start () {
	au = GetComponent<AudioSource>();
	au.loop = true;
	au.Play();
}
}

however still nothing is working.

driving me insane to say the least, If anyone could guide me in the right direction that would be great…

Thanks, Jordan

Hi @Jordan_Ashton, how many listeners do you have in the scene?

…do you have Play on Awake ticked also, aswell as Loop?

2 Likes

Hey @Rob

I do have it checked. However just testing things earlier and unchecked it. Still refused to loop the track.

Re listeners. Still new to this so could you explain what you mean. Sorry :confused:

Sure, I can try… can you pop up a little screenshot of your hierarchy pane, just so I can see the items listed in it please.

Just a thought.

Do you need the musicPlayer instance in all scenes?

Not on my PC at the moment, but can update post in the morning.

Here is a screenshot of one of my scenes without music player instance in hierarchy

Sorry, I meant just the hierarchy panel (so I could read the words! :D)… not to worry…

MusicPlayer object is only on the Start scene, it doesn’t appear in the hierarchy for any other scenes, because we want the music to start when the game starts, not a specific level.

I had no code in the Start() or Update() methods.

using UnityEngine;
using System.Collections;

public class MusicPlayer : MonoBehaviour {

	static MusicPlayer instance = null;

	// Use this for pre-initialization, executes before Start()
	void Awake () {
		if(instance != null){
			Destroy (gameObject);
		} else {
			instance = this;
			GameObject.DontDestroyOnLoad(gameObject);
		}	
	}

	// Use this for initialization
	void Start () {

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

Settings on the AudioSource component on the MusicPlayer object, with the AudioClip populated (drag sound from Assets etc);

Make sure you have added the MusicPlayer script to the MusicPlayer object in the Start scene.

If you click on the MainCamera object and look in the Inspector you should see a component called Audio Listener;

There should only be one of these, per scene, so check the other objects in hierarchy for each scene and make sure you only have the one.

Check all of the above, and let me know if you spot anything :slight_smile:

I don’t think i have an audio listener on any of the cameras =_=

That will be my issue, will review tomorrow and update based on findings.

Thanks in advance :slight_smile:

Ok Update.

There is an audio listener on all the main cameras,

Below is all my hierarchy for each scene




Really confused now :confused:

Ok, so the one audio listener component on each camera and in each scene is ok. A problem may occur if you had for example another audio listener on a different object as well as the main camera in a scene.

What about the other items I mentioned, did you work through those?

Ran through everything you mentioned, as all is as it should be.

Could it be the file format at all? Using an mp3 at the moment I believe can’t imagine that being an issue as Unity accepted it, but you never know.

Hi @Jordan_Ashton, when you said that the music wasn’t looping I assumed you meant it would play once but then stop. Is it not playing at all?

Wouldn’t hurt to test your thoughts with regards to the .mp3, all good practice/experience. I used the .ogg files as per the course for the sound effects and then I have a .wav from the course that was used for the music - all of which were ok.

If you have other the other file formats to hand you could drop one of those in your Assets directory and drag it on to the AudioSource as the clip etc to test, e.g rule out any code problems with the .ogg file, if it loops, it’s not likely to be your code.

I seem to remember Audacity being recommended, as a free tool, during the course, perhaps grab a copy of that too, you may be able to convert your .mp3 to another file that would work if the .mp3 doesn’t.

Having just done a quick Google search I found this, looks like that may well answer the question.

If you could test though and see, and then maybe convert your file to another format which works, what might be worth doing is updating your topic heading so that it mentions MP3 in case anyone else comes across the same issues. If you get it sorted, please do mark the topic as [Resolved] also - if not, post back and we will continue to check things out :slight_smile:

Music does play once and comes to a complete stop aferr. Track is about 2mins long off freesounds. I’ll have a look in the morning at your suggestions and I’ll skim through that link you posted above now just to see if it has any resemblance to my issue.

Thanks for all your help so far @Rob it has been great :slight_smile:

hmmm, if it is playing once then I doubt it is because it is an .mp3, although might still be worth checking a different format to rule it out etc.

You are more than welcome @Jordan_Ashton, come back to us here after the tests above and let’s see where things are then. :slight_smile:

Converted to .ogg and it worked. why didn’t I do this days ago =_=

Thanks for putting up with my **** @rob haha

1 Like

Hey @Jordan_Ashton, great news! Glad you got it working.

More than happy to try to help so it really isn’t a problem at all. Thanks for marking the topic resolved.

Look forward to seeing your take on the game linked in the Showcare sub-forum in due course :slight_smile:

Privacy & Terms