How to put music in console application with C #

3

I'm creating a game in C # (console application) and need to put a background music, how do I?

    
asked by anonymous 01.11.2016 / 21:59

3 answers

4

As shown in the documentation for the MSDN you can use something like:

Matters:

using System.Media;

And use it like this:

SoundPlayer player = new SoundPlayer();
player.SoundLocation = Environment.CurrentLocation + "typewriter.wav";

In case you just need to set the audio location to SoundLocation and then run .Play (runs asynchronously, to load synchronously, which can crash the app depending on the case, use PlaySync ):

try {
    player.Play();
} catch (Exception ex) {
    Console.WriteLine(ex.Message); //Somente um exemplo exibir erros em caso de falha, pode modificar como desejar
}
    
01.11.2016 / 22:12
2

The question does not give details, but the basics would be something like this:

var player = new SoundPlayer("NomeDoArquivo.wav");
player.Play();

Documentation .

    
01.11.2016 / 22:12
0

First include the using System.Media; at the beginning of the code, after that, to differentiate from the other answers, you can include a Resource of a sound (go to Project > ProjectName> Resources > a Resources file - look for the corner to add an audio) and then put SoundPlayer to play it.

public static void Main(string[] args)
{
    SoundPlayer soundPlayer = new SoundPlayer(Properties.Resources.SimpleSound);
    soundPlayer.Play(); // Para começar o som normalmente.
    soundPlayer.PlayLooping(); // Para começar o som e ficar repetindo.
    soundPlayer.PlaySync(); // Para começar o som e só ir para a próxima linha quando parar de tocar.
}
    
02.11.2016 / 18:23