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();
}
}
}
Related
I am using timer to connect several audio files together. There are a list of audio files and the timer will read files one by one. Once there is an audio read, it will be played. The goal is to play the audio file right after the previous one when it is finished(without any break).
(I am using Naudio)
Here is the code:
private void timer_key_Tick(object sender, EventArgs e)
{
if(!isPlaying)
{
//play the current audio
DirectSoundOut dso = sounds2[key_indexer];
dso.Play();
targetMusics.Add(dso);
}
else
{
foreach (DirectSoundOut dso in targetMusics)
{//stop the current audio
dso.Stop();
}
targetMusics.Clear();
key_indexer++; //switch to the next audio
if (key_indexer >= myMT.Keys.Count)
{
key_indexer = 0;
timer_key.Stop();
}
}
isPlaying = !isPlaying;
}
However, the fact is, when the first music finished, the second one didn't play at once. It comes after one second break. Is this the problem about timer itself? How should I change it?
Thanks for the help from #HansPassant. I made a mistake about logic in this timer. Here is the correct code after I applied his suggestion:
//stop the current playing
foreach(DirectSoundOut d in targetMusics)
{
d.Stop();
}
targetMusics.Clear();
//stop the entire process
if (key_indexer >= myMT.Keys.Count)
{
key_indexer = 0;
timer_key.Stop();
}
else
{ //play the current audio
DirectSoundOut dso = sounds2[key_indexer];
targetMusics.Add(dso);
dso.Play();
key_indexer++;
}
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 am currently making windows media player and facing the problem with pause and play function in one button.
Here is my Code its not working except pausing the video. how can i resume the video again by same (pause) button ?
In Class :
public void play(AxWMPLib.AxWindowsMediaPlayer p1)
{
p1.Ctlcontrols.play();
}
public void pause(AxWMPLib.AxWindowsMediaPlayer p1)
{
p1.Ctlcontrols.pause();
}
myClass1 mc1 = new myClass1();
mc1.play(axWindowsMediaPlayer1);
mc1.pause(axWindowsMediaPlayer1);
You can check the Media Players playStateproperty and then Play or Pause it.
To reuse the code you already have, you can add the following criteria:
myClass1 mc1 = new myClass1();
if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
{
mc1.pause(axWindowsMediaPlayer1);
}
else
{
mc1.pause(axWindowsMediaPlayer1);
}
You can read the documentation here
Windows media player has a useful property Ctlcontrols that gives you many options:
To pause:
axWindowsMediaPlayer1.Ctlcontrols.pause();
And another options:
axWindowsMediaPlayer1.Ctlcontrols.stop();
axWindowsMediaPlayer1.Ctlcontrols.currentPosition = // a double value...
axWindowsMediaPlayer1.Ctlcontrols.play();
.....
So I am doing a simple piano and trying to traverse the collection where I stored the notes, but the SoundPlayer doesn't want to play them properly in "without debugging mode", playing only the last one. However when I put a breakpoint there it plays all of them
public static List<MusicNote> music = new List<MusicNote>(15);
public static void PlayAll()
{
SoundPlayer sp = new SoundPlayer();
for (int i = 0; i <= music.Count - 1; i++)
{
string text = music[i].pitch.ToString();
sp.SoundLocation = (#"c:\my path here\" + text + ".wav");
sp.Play();
sp.Stop();
}
}
Pitch is simply ordinal number to link to file.
Thanks in advance
U better use PlaySyn in order to tell your program to wait until the music complete
// Create new SoundPlayer in the using statement.
using (SoundPlayer player = new SoundPlayer())
{
for (int i = 0; i <= music.Count - 1; i++)
{
string text = music[i].pitch.ToString();
sp.SoundLocation = (#"c:\my path here\" + text + ".wav");
// Use PlaySync to load and then play the sound.
// ... The program will pause until the sound is complete.
player.PlaySync();
}
}
I think its better when you use PlaySync(); instead of Play();
Because then you don't need the Stop() methode.
Here a link to the docu of SoundPlayer
Why use PlaySync? If you just call the Play method in this program, the program will terminate before the sound plays. The Sync indicates that the program should pause while the sound plays.
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();