I want to know how to close the media player when it finishes playing my wav file. Because if I run this multiple times, it consumes an dangerous amount of computer ram. If you can solve that without closing it, by all means tell me.
var player = new WMPLib.WindowsMediaPlayer();
player.URL = #"D:\notes\01.wav";
This is the code to play it.
There isn't a way to exit the player, the only(not so ideal approach) is to kill it.
var proc = Process.GetProcessesByName("wmplayer");
if (proc.Length > 0) {
proc[proc.Length - 1].Kill();
}
This might do the trick for you
var player = new WMPLib.WindowsMediaPlayer();
player.URL = #"D:\notes\01.wav";
Thread.Sleep(2000);
player.controls.stop();
player.close();
Edit
Than you can kill the process and stop the WMPlayer
var prc = Process.GetProcessesByName("wmplayer");
if (prc.Length > 0) prc[prc.Length - 1].Kill();
Related
I have made a card game that gets and shows player cards from resources in a UI using a foreach loop which works fine. I have recently added a card sound as well so that a sound would be played before each image is shown in the UI. However, the sounds are always played first and then the images are shown. A bit annoying...
I use sound player and playsync(). How do I force the GUI to update? (between the sounds)
Here is some of the code...
Starts with
//loop
foreach (var player in playerGUIs)....
//Call the sound Card_Sound(); //See below
//Update image player.img1.Source = new BitmapImage(new
Uri(("/Utilities;Component/Resources/" + card_file + ".png"),
UriKind.Relative));
private void Card_Sound()
{ //Play sound System.Threading.Thread.Sleep(800); // First sleep 800 milliseconds
using (FileStream stream = File.Open(#"Resources\dealingcard.wav",
FileMode.Open)) {
SoundPlayer myNewSound = new SoundPlayer(stream);
myNewSound.Load();
myNewSound.PlaySync(); }
}
PlaySync() will block the UI (if running on the UI thread) did you try Play()?
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++;
}
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.
I am trying to make my application play a quick .wav before closing the application, but every time it only plays the .wav and does not exit the application. Here is my code:
bool condition = false;
if (condition)
{
SoundPlayer sndPlayer = new SoundPlayer(Project_Rage_v3._1.Properties.Resources.syl);
sndPlayer.Play();
}
else
{
Application.Exit();
}
I know if the bool == true it will do the if function and if the bool == false it will do the else function. But if I don't split them up, the program just quits immediately without playing the sound.
How do I have it play the sound then quit, but only quit after the sound is finished playing?
Remove the else block. You're telling the program to play the sound if condition is true, and exit if condition is false. Put the commands in sequential order and call PlaySync instead of Play (MSDN). This will block the rest of the code from executing until the playing is finished.
bool condition = false;
if (condition)
{
SoundPlayer sndPlayer = new SoundPlayer(Project_Rage_v3._1.Properties.Resources.syl);
sndPlayer.PlaySync();
Application.Exit();
}
I have a windows form with a aXWindowsMediaPlayer which should play a random video/picture every 5 seconds using the method aXWindowsMediaPlayer.Url = <video/image file location>.
This works fine for pictures, but when a video is longer it'll only play the 5 seconds and not the full length, same for a shorter video, it'll play the full video and leaving the other seconds with a black screen.
Is there a way to configure the mediaPlayer to only go to the next picture/video when it has played the videos full length?
EDIT1:
Something like:
Timer_TickEvent()//Every 5 seconds it chooses a random given URL
{
axWindowsMediaPlayer1.URL = <Random URL>;
if(<Random URL> == 'A video?')
{
timer.Enabled = false;
PlayVideoLength();
}
}
Private void PlayVideoLength()
{
if('<Random URL.Length> ?' == 'the length of the played video in axWindowsMediaPlayer1 ?')
{
Timer.enabled = true;
}
}
This code obviously wont work. It's an idea of what I want it to do.
You can find if your URL is a video/audio/picture media with a IWMPMedia::getItemInfo, or getType.
instead of set Url in your axWindowsMediaPlayer component :
MyMediaComponent.Url = <MyMediaUrl>;
You can create a new with this URL:
WMPLib.IWMPMedia MyNewMedia = MyMediaComponent.newMedia(<MyMediaUrl>);
And I think you can get the type of your Media with :
MyNewMedia.getType();
You can Find the msdn documentation here.
When you find the type of your media, you can just test the type of your media before set your URL!
/* It is just a suggestion, this code don't work! */
if ( MyNewMedia.getType() == <A photo MEDIA code>)
{
// Set the URL for show the picture
MyMediaComponent.Url = <MyMediaUrl>;
return;
}
//else
// This media is not a picture!
Hope my idea help you.