Dose anyone have any idae why this code is not working for my background music to constantly loop??
In load Content:
backgroundMusic.Play(0.3f, 0.0f, 0.0f); //play background music (first float number is for volume)
In update:
SoundEffectInstance instance = backgroundMusic.CreateInstance(); //creates instance for backgroundMusic
instance.IsLooped = true; //states that instance should loop meaning background music loops and never stops
Thanks in advance
Edit: I now have this:
Content Load:
Song backgroundMusic = Content.Load<Song>("backgroundMusic");
and then seperately:
public void PlayMusicRepeat(Song backgroundMusic)
{
MediaPlayer.Play(backgroundMusic);
MediaPlayer.IsRepeating = true;
}
If you need to manage the backgroud music you should use the Song class, SoundEffect should be used only for sound effects.
Something like this:
public void PlayMusicRepeat(Song song)
{
MediaPlayer.Play(song);
MediaPlayer.IsRepeating = true;
}
Of course the loading should be like this:
Song music = Game.Content.Load<Song>("background");
You are playing the SoundEffect, but looping the SoundEffectInstance, that won't work.
And following this article from Microsoft (http://msdn.microsoft.com/en-us/library/dd940203.aspx), you have to set the IsLooped property BEFORE playing the sound.
So your code should look like this:
In LoadContent:
instance = backgroundMusic.CreateInstance();
instance.IsLooped = true;
In Update:
if(instance.State == SoundState.Stopped)
instance.Play();
Related
i am making a quiz and in each form i have background music and if they get an answer correct i want to play a sound effect. When i do this is stops the background music and plays the sound effect then no sound is playing, does anyone know how do it?
public static System.Media.SoundPlayer player = new System.Media.SoundPlayer();
public static void sound(string form)
{
switch (form)
{
case "Login":
player.Stop();
player.Stream = Properties.Resources._2marioloadscreen;
player.PlayLooping();
break;
}
}
Here's an example of how to do it, assuming you have two wav files (one for background music, and one for the correct answer sound effect). The key is to know when the sound effect is done playing, then continue with the background music again.
I wrote it as a simple C# Console App.
class Program
{
private static SoundPlayer player;
static void Main(string[] args)
{
using (player = new SoundPlayer())
{
player.SoundLocation = "bkgnd.wav";
player.PlayLooping();
Console.WriteLine("bkgnd.wav is now playing in a loop. (Hit ENTER key to start the hoorayyouwon.wav)");
Console.ReadLine();
Console.WriteLine("hoorayyouwon.wav is now playing.");
player.Stop();
player.SoundLocation = "hoorayyouwon.wav";
player.PlaySync(); // this ensures the hoorayyouwon.wav plays completely before it gets to the next line of code.
Console.WriteLine("bkgnd.wav is now continuing (actually, starting over). (Hit ENTER key to exit)");
player.SoundLocation = "bkgnd.wav";
player.PlayLooping();
Console.ReadLine();
}
}
}
I am making a Music Player app for Android in Unity3d. For show_currentPlayTime() function, I want to get AS.time value to show current playTime. For seek() function, I want to set AS.time value to get to that specific point of AudioClip. But, the problem is that AS.time is giving me 0.
I have tried setting AudioClip in AudioSource. It works that way, but the audio becomes distorted and shows abnormal behavior.
Variable Declaration:
public AudioSource AS;
[Range(0.0f, 1.0f)]
public Slider Volume;
public Slider slider;
bool isPlaying;
public List<AudioClip> AC = new List<AudioClip>();
int currentSong = 0;
Play Function:
public void Play()
{
AS.Stop();
AS.PlayOneShot(AC[currentSong]);
//AS.clip = AC[currentSong]; ---
//AS.Play(); ---
//AS.clip = AC[currentSong]; ---
//AS.PlayOneShot(AS.clip); ---
clipInfo.text = AC[currentSong].name;
Debug.Log(AC[currentSong].name);
CancelInvoke();
Invoke("Next", AC[currentSong].length);
isPlaying = true;
}
The program runs with using --- lines, in above code, but with that, the audio becomes distorted.
Music Seek function:
public void MusicSlider()
{
AS.time = AC[currentSong].length * slider.value;
slider.value = AS.time / AC[currentSong].length;
Debug.Log(AS.time);
}
AS.time gives me value = 0.
The AudioSource.Stop function stops the currently set Audio clip from playing
so you shouldn't be using PlayOneShot since this doesn't assign the clip value and isn't stopped but played parallel. It is usually more used for playing soundeffects which may occure at the same time.
From AudioSource
You can play a single audio clip using Play, Pause and Stop You can also adjust its volume while playing using the volume property, or seek using time. Multiple sounds can be played on one AudioSource using PlayOneShot.
AudioSource.time from the examples also only seems to work using Play instead of PlayOneShot for the same reason.
In the commented code you are also mixing both Play and PlayOneShot. I guess what you called distorted is actually the clip playing twice at the same time. Your code should rather simply be
public void Play()
{
AS.Stop();
AS.clip = AC[currentSong];
AS.Play();
clipInfo.text = AC[currentSong].name;
Debug.Log(AC[currentSong].name);
// Don't know ofcourse what those methods do...
CancelInvoke();
Invoke("Next", AC[currentSong].length);
isPlaying = true;
}
In my Unity Project, there are different menus, and each menu is a different scene. Each scene has button for controlling the music.
public void musicToggle(GameObject Off){
if (PlayerPrefs.GetInt ("Music") == 1) {
musicPlayer.Stop ();
Debug.Log ("Stop 2");
PlayerPrefs.SetInt ("Music", 0);
Off.SetActive(true);
}
else {
musicPlayer.Play ();
Debug.Log ("Play 2");
PlayerPrefs.SetInt ("Music", 1);
Off.SetActive (false);
}
}
This is my musicToogle function. In every scene the music restarts, and in every scene when I want to turn on/off the music, I click a button which deploys this code. However, I don't want the music to restart every scene change, I want it to resume, and I want to be able to control the music(turn on/off) in every scene. How can I do this ?
My answer assumes that musicPlayer variable is a type of AudioSource.
There are really two ways to do this:
1.Instead of using musicPlayer.Stop to stop the music,
Use musicPlayer.Pause(); to pause then music then use musicPlayer.UnPause(); to un-pause it. This will make sure that the music resumes instead of restarting it.
In this case, you use DontDestroyOnLoad(gameObject); on each GameObject with the AudioSource so that they are not destroyed when you are on the next scene.
2.Store the AudioSource.time value. Load it next time then apply it to the AudioSource before playing it.
You can use PlayerPrefs to do this but I prefer to store with json and the DataSaver class.
Save:
AudioSource musicPlayer;
AudioStatus aStat = new AudioStatus(musicPlayer.time);
//Save data from AudioStatus to a file named audioStat_1
DataSaver.saveData(aStat, "audioStat_1");
Load:
AudioSource musicPlayer;
AudioStatus loadedData = DataSaver.loadData<AudioStatus>("audioStat_1");
if (loadedData == null)
{
return;
}
musicPlayer.time = loadedData.audioTime;
musicPlayer.Play();
Your AudioStatus class:
[Serializable]
public class AudioStatus
{
public float audioTime;
public AudioStatus(float currentTime)
{
audioTime = currentTime;
}
}
You will need to create a singleton class and instantiate it on your first scene, then you can pass it around your various scenes as described in this unity answer: http://answers.unity3d.com/answers/12176/view.html
I try to write a splash screen in my first cocossharp game as write splash in android application. However, it show a black screen and then directly goto gameplayscene. So what should I change? Thank you very much!
public class SplashScene : CCScene
{
CCSprite splashImage1;
CCSprite splashImage2;
CCLayer splashLayer;
public SplashScene (CCWindow mainWindow) : base(mainWindow)
{
splashLayer = new CCLayer ();
this.AddChild (splashLayer);
splashImage1 = new CCSprite ("Splash1");
splashImage1.Position = ContentSize.Center;
splashImage1.IsAntialiased = false;
splashImage2 = new CCSprite ("Splash2");
splashImage2.Position = ContentSize.Center;
splashImage2.IsAntialiased = false;
}
public void PerformSplash()
{
splashLayer.AddChild (splashImage1);
Thread.Sleep(3000);
splashLayer.RemoveChild(splashImage1);
splashLayer.AddChild (splashImage2);
Thread.Sleep(2000);
splashLayer.RemoveChild (splashImage2);
GameAppDelegate.GoToGameScene ();
}
}
The game loop must be running for, well, any game framework to display and update. Calls to Thread.Sleep pause the execution of the thread.
If you want to display a splash screen for an interval the best way would be to simply create the splash scene as you are, and then schedule an action sequence
Something like this will wait for 2s, then remove the splashLayer and go to the game scene.
auto seq = Sequence::create(
DelayTime::create(2.0),
CallFunc::create([=](){
splashLayer->removeFromParent();
GameAppDelegate.GoToGameScene();
}),
nullptr);
runAction(seq);
My code:
Variables:
//Menu Song
Song mainMenuTheme;
bool songstart = false;
LoadContent() method:
//Load the song
mainMenu = Content.Load<Song>("musics\\mainMenuTheme");
MediaPlayer.IsRepeating = true;
In the Update() method:
//Start the song
if (!songstart)
{
MediaPlayer.Play(mainMenuTheme);
songstart = true;
}
So the song is in .wma format, 6.32MB and the duration is 00:01:36. How to make the WHOLE song loop? Because when I start debugging it cuts off after a while and loops again. Any suggestions?
Try using .mp3 instead of .wma for Song class.
Your code seems corrert.