Windows Forms C# WMPLib Crashes while playing a mp3 file - c#

I'm working on a simple mp3 player. I'm using WMPLib for playing mp3 files.
I'm displaying all tracks in Data Grid View, where double click plays selected track. The program plays the right song, however once I start scrolling or pressing buttons fairly quickly the song stops playing.
This is the method which I'm using for playing individual tracks:
public static void playTrack(int t)
{
Library l = new Library();
WMPLib.WindowsMediaPlayer song = new WMPLib.WindowsMediaPlayer();
song.URL = l.myLibrary[t].Patch;
song.controls.play();
}
I call the above method when dataGridView1_CellContentDoubleClick is raised in a separate class.
Do you guys know why this is happening?
Do I need to use multi threading in order to fix it?
I also thought that my program's UI is simply too "heavy" as I'm using multiple nested panels in custom controls. I'm including a print screen of my running application.

Ok, i found the answer to my problem.
Just needed to declare Library and WMPLib as a field outside of my method as shown below:
static Library l;
static WMPLib.WindowsMediaPlayer song;
public static void playTrack(int t)
{
l = new Library();
song = new WMPLib.WindowsMediaPlayer();
song.URL = l.myLibrary[t].Patch;
song.controls.play();
}
I hope this helps someone :)

Related

How to play background music in C# Form FROM properties.resources simultaneously with other sounds

I'm new to C# and I'm making a mini-game. Playing sound effects is no problem, I can just use the System.Media.SoundPlayer class object to play a wav file streaming from, for example Properties.Resources.attack.wav, this is good but the background music will stop after I play the sound effect. Yes, I know there's Windows Media Player, but that uses URI, which I can't seem to add the resources directory there. I don't want to use local directory like #"D:\MyGame\backgroundmusic.wav" because I want to send it to a friend and can still hear the music. Is there any class or external sdk's that allow sound to be played simultaneously with other sounds FROM Properties.Resources? If none, what is the URI of the Resources folder inside a solution? Any help or advice would be awesome! Thank you.
edit: I already opened those links and tried them all several times. My main question is "How to play sound FROM the Resources Folder of the solution without other sounds interrupting it"
This is the concept:
private void Form_Load(object sender, EventArgs e)
{
var player = new System.Windows.Media.MediaPlayer();
player.Open(Properties.Resources.sfx_background);
player.Play();
}
private void attack()
{
SoundPlayer p1 = new SoundPlayer();
p1.Stream = Properties.Resources.sfx_sword;
p1.Play();
}
Where p1 plays synchronous with player, in which player gets the sound file from the Resources of the solution, not from a local storage of a computer.

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.

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.

Playing MIDI-files and synchronizing time with visuals with WPF

I'm writing an application using C#/Windows Presentation Foundation.
It is visualizing the steps of a dance with foot shapes.
Currently I'm playing the music as WAV-file and timing the steps with a Timer.
Because of the irregularities of a Timer the music is not in sync with the steps.
I need some kind of synchronization, this is why I wanted to use MIDI-files.
To sync the steps I need an event for each time in the music and would then show the next step. In this case I wouldn't use the Timer anymore.
I already looked at NAudio. I found tutorials for playing MP3-files which don't help me. I created a MidiFile-object but I don't know how to play it. I know that a MIDI-file contains information on how to play the music (for synthesizers) but I don't want to implement my own player.
What is a simple way to play a MIDI-file with NAudio?
How can I receive Events in each time of the music?
Is there an alternative to NAudio that can probably help me better?
Is there an alternative to MIDI that can sync to my visualization?
I am thankful for every kind of help. I've been searching for a while and think that I am maybe looking in the wrong direction.
With DryWetMIDI (I'm the author) playing MIDI files along with firing played events is pretty simple:
namespace SimplePlaybackApp
{
class Program
{
private static Playback _playback;
static void Main(string[] args)
{
var midiFile = MidiFile.Read("The Greatest Song Ever.mid");
var outputDevice = OutputDevice.GetByName("Microsoft GS Wavetable Synth");
_playback = midiFile.GetPlayback(outputDevice);
_playback.EventPlayed += OnEventPlayed;
_playback.Start();
SpinWait.SpinUntil(() => !_playback.IsRunning);
Console.WriteLine("Playback stopped or finished.");
outputDevice.Dispose();
_playback.Dispose();
}
private static void OnEventPlayed(object sender, MidiEventPlayedEventArgs e)
{
// ... do something
}
}
}
More info in Playback article and Playback API reference.
If you want to get deeper into the midi internals this looked like a pretty cool library and source code to explore.
http://code.google.com/p/midi-dot-net/

What to use for playing sound effects in silverlight for wp7

I know I can reference XNA for the SoundEffect class and that's what I've been doing so far but I was wondering if there was a better way than what I've been doing.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using (var stream = TitleContainer.OpenStream("test.mp3"))
{
var effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
}
For my test app I have 20 sounds each 1 second long that I want to play once button are pressed. I'm playing around with different techniques but if possible I'd like to know how professionals go about doing this before I commit in making a sound effect based app. Little things such as loading the sound effect first or loading it the instance the button is pressed would be helpful.
Thanks.
If I were you I would use PhoneyTools SoundEffectPlayer
This class is used to play SoundEffect
objects using the XNA integration. The
player must live long enough for the
sound effect to play so it is common
to have it scoped outside a method.
For example:
public partial class MediaPage : PhoneApplicationPage
{
// ...
SoundEffectPlayer _player = null;
private void playButton_Click(object sender, RoutedEventArgs e)
{
var resource = Application.GetResourceStream(new Uri("alert.wav", UriKind.Relative));
var effect = SoundEffect.FromStream(resource.Stream);
_player = new SoundEffectPlayer(effect);
_player.Play();
}
}
I think a good example would be the official sample on AppHub. It demonstrates how to play multiple sounds. You can directly download the sample from here.
This sample demonstrates how to use
the XNA Framework's SoundEffect and
SoundEffectInstance classes to play
multiple sounds simultaneously in a
Silverlight application for Windows
Phone. It also shows a simple way to
set up a DispatchTimer to call
FrameworkDispatcher.Update in order to
simulate the Game loop for the XNA
Framework's internals. Finally, it
shows how to load a wave audio file
into a Stream that can be played by
the SoundEffect classes.

Categories

Resources