mercredi 31 décembre 2014

Program not working when playing song

I'm having trouble putting background music in my WinForm c# program. I'm kinda new to programming, so be gentle hahahaha >.<


Basically what I have tried are the following:


1) Using Sound Player



System.Media.SoundPlayer playsong = new System.Media.SoundPlayer();
playsong.Stream = Properties.Resources.song;
playsong.PlayLooping();


This kind of works, but only for a random few seconds. The program would, after a random amount of time, suddenly say "[name of program] has stopped working"


2) Using Sound Player, but with timer


I was guessing that, maybe there was some problem with "PlayLooping". And so I made 2 timers. One to play the song using .play(), and another timer to stop playing with .stop(). Every 30 seconds, timer 1 would play the song, while every 29 seconds timer 2 would stop playing.


This also works for a random few seconds and the program would also say "[name of program] has stopped working".


3) Used Windows Media Player


I did all the Com stuff and wrote the following code



WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();

wplayer.URL = Application.StartupPath + @"/song.mp3";


This works for a very short amount of time before the music stops playing. Program doesn't stop working, just that the music cuts and doesn't play again till I press the button to make the program run the code again.


I have no idea where to start, there's no errors no nothing. Sigh.


It should be noted that I have other lines of code other than the above lines though, including in my class files and stuff, although I really don't think it would affect playing the song.


Help would be much appreciated. If any more information would help, I would be glad to provide them.


Aucun commentaire:

Enregistrer un commentaire