Windows Form: Play sound, but not from beginning - c#

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.

Related

Stop sound playback in C# with SDL library

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.

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 Forms C# WMPLib Crashes while playing a mp3 file

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 :)

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/

Categories

Resources