Stop sound playback in C# with SDL library - c#

I have this problem where I can't stop the audio file from playing.
This is a snippet of my code:
private Sound bgMusic;
bgMusic = new Sound("bgMusic.wav");
if(gameHasStarted)
bgMusic.Play();
else
bgMusic.Stop();
I've searched Google mulptiple times, but without any success. The music just doesn't stop.
I've tried threading this audio file, but without luck.
I've also tried using a MediaPlayer - the sound always plays, whichever option I choose, but it just doesn't stop.
So my question:
How do I stop audio file from playing? Do I need to reset it? Close it? Destroy it? How?!

It's actually not that difficult:
private System.Media.SoundPlayer playerMainMenu; - initialize player
playerMainMenu = new System.Media.SoundPlayer("getlucky.wav"); - load file
playerMainMenu.PlayLooping(); - to play looping
playerMainMenu.Play(); - to play once
playerMainMenu.Stop(); - to stop this player
Works like a charm. Found by trial-and-error.

Related

How to generate constant beep in windows forms

I am creating a morse code interpreter on C# because I am bored.
I have the whole thing working except one thing, the beep.
I am currently using Console.Beep(1000, timer1.Interval); in the timer1_tick event to beep for the same amount of time as the timer ticks, attempting to emulate a constant beep until the key is released. However this comes out very choppy with a few ms gap of silence between beeps.
Is there any way to create a constant buzz/beep sound until my isPressed bool is found false in the tick event?
Why you don't use playing the wav file?
You can have a special file and play it, then stop playing when the key is released...
//start
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = #"file.wav";
player.Play(); //or player.PlaySync();
//stop
player.Stop();
Hope this helps!

Windows Form: Play sound, but not from beginning

I want to play a track (.wav file) in my Windows Forms Application. But I do not want it to play from the beginning, but from a certain point somewhere in the track (let's say 10 seconds).
To play the track from beginning is no problem:
private void playSimpleSound()
{
SoundPlayer simpleSound = new SoundPlayer(#"c:\Windows\Media\sound.wav");
simpleSound.Play();
}
But how can I skip the first 10 secons, and play only the rest of it?
Thanks in advance!
SoundPlayer class not to support to do this. It gives a very simple control on WAV files.
Maybe this question can help you.

How can I check if MediaPlayer has finished loading a sound?

I am trying to play multiple sounds at the same time for a simple game using the MediaPlayer class (System.Windows.Media.Mediaplayer).
To play the same sound multiple times at the same time, I created my own class, which loads the same sound many times.
Here is a short example:
sound = new MediaPlayer[count]; //array of MediaPlayers
//...
for (int a = 0; a < count; a++)
{
sound[a] = new MediaPlayer();
sound[a].Open(new System.Uri(sp + pfad)); //load sound
}
Here is my problem:
If I try to play a sound shortly after starting the game either I can't hear the sound or the beginning of the first sound I loaded will start playing (even if I tried to play a completely different sound).
It seems like all these MediaPlayers (I'm loading ~100) need some time to load the sounds.
Therefore I'm looking for a way to check if the loading is done.
Loading or playback? When the media has finished playback it rises the MediaEnded event, when buffering has finished it rises BufferingEnded event. MediaPlayer events.

Song doesn't play all the time in Monogame

Heey,
We created a game with Monogame but we got the following problem.
We got a themesong that plays when you have loaded the game now is the problem that the themesong sometimes plays but sometimes just doesn't. We convert it by the XNA pipeline to a wma and load it into our game with the .xnb together but just sometimes the music doesn't wanna start.
We just use the standard code for starting a song and all of this code does fire.
internal void PlayBackgroundMusic()
{
if (MediaPlayer.GameHasControl)
{
MediaPlayer.Volume = mainVolume;
MediaPlayer.Play(backgroundMusic);
MediaPlayer.IsRepeating = true;
}
}
We also use SoundEffects but these work 100% of the time it's only the song that won't play everytime you start. Windows RT runs it fine by the way.
Make sure that the debugger gets into the if statement through debugging (or remove the statement temporarily). Another possibility might be that the function is running before the game is fully initialized. You could try delaying the function until the game has been fully loaded.
PS: I can't comment on questions yet so here's an answer.
Edit:
Alright, after some messing around with the Song class and looking in the implementation in MonoGame I came to the conclusion that the SoundEffect class is easier to use and better implemented.
backgroundSound = Content.Load<SoundEffect>("alarm");
protected override void BeginRun()
{
// I created an instance here but you should keep track of this variable
// in order to stop it when you want.
var backSong = backgroundSound.CreateInstance();
backSong.IsLooped = true;
backSong.Play();
base.BeginRun();
}
I used this post: using BeginRun override to play the SoundEffect on startup.
If you want to play a song, use the Song class. But make sure you are using ".mp3" instead of ".wma" before converting them into ".xnb".
backgroundMusic = Content.Load<Song>("MainTheme");
MediaPlayer.Play(backgroundMusic);
MediaPlayer.IsRepeating = true;
See MSDN.

How can I update the frame of a Windows Media Player COM?

I have a windows media player COM in my Windows Form project that plays and opens videos admirably. However, I would like to be able to grab the first frame of the loaded video so my program users can preview the video (and, ideally, recognize one video from another).
How can I update the frame displayed by the windows media player object?
I have tried using the following code at the end of my openFileDialog event response:
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
Text = openFileDialog1.SafeFileName + " - MPlayer 2.0";
//mediaPlayer1.openPlayer(openFileDialog1.FileName);
mediaPlayer1.URL = openFileDialog1.FileName;
//hopefully, this will load the first frame.
mediaPlayer1.Ctlcontrols.play();
mediaPlayer1.Ctlcontrols.pause();
}
However, when I run this, the pause command gets ignored (Auto-play for video loading is turned off, so the video won't start playing without calling .play(), above). If I had to guess, I'd say that this is because of some threading operation that calls play, moves on to call pause, calls pause, and then, finally, the play resolves, and the video starts - but because the .pause resolved before the .play, the net effect is the .pause is ultimately unheeded.
Firstly, is there a way other than .play(); .pause(); to snag a preview image of the video for the AxWindowsMediaPlayer object? If not, how can I make sure that my .pause() doesn't get ignored?
(I know that .play(); .pause(); works in the general case, because I tested with a separate button that invoked those two methods after the video finished loading, and it worked as expected)
You can't do a lot of things with this COM. However Follow this link and you will find a Class that will help you extract an image from a Video file. You could simply get extract the image and just put it in top of the video, or next to it. This is a simple workaround for your requirement. If not happy with it, I would strongly recommend not using this COM at all and use some other open source video player/plugins. There a lot of real good ones, but I could recommend the VLC Plugin, or try finding another.
Good luck in your quest.
Hanlet
While the Windows Media Player Com might not officially support a feature like this, its quite easy to 'fake' this. If you use a CtlControls2, you have access to the built-in "step(1)" function, which proceeds exactly one frame.
I've discovered that if you call step(1) after calling pause(), searching on the trackbar will also update the video.
It's not pretty, but it works!
This is a little trick to solve the common step(-1) not working issue.
IWMPControls2 Ctlcontrols2 = (IWMPControls2)WindowsMediaPlayer.Ctlcontrols;
double frameRate = WindowsMediaPlayer.network.encodedFrameRate;
Console.WriteLine("FRAMERATE: " + frameRate); //Debug
double step = 1.0 / frameRate;
Console.WriteLine("STEP: " + step); //Debug
WindowsMediaPlayer.Ctlcontrols.currentPosition -= step; //Go backwards
WindowsMediaPlayer.Ctlcontrols.pause();
Ctlcontrols2.step(1);

Categories

Resources