How to Put Music on Console Aplication with C#

Asked

Viewed 1,521 times

5

I’m creating a game in C# (Aplication console) and I need to put a background music, as I do?

3 answers

5

As an example from the documentation of MSDN you can use something like:

Matter:

using System.Media;

And use it like this:

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

In case you only need to set the audio location on SoundLocation and then execute .Play (runs asynchronously, to charge synchronously, which can lock 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
}

2

  • since already I am grateful my friend, I am programming a very short time.

  • 1

    @Wilsondayvis perhaps could mark an answer as accepted.

0

First include the using System.Media; at the beginning of the code, after that, to differentiate from the other responses, you can include a Resource of a Sound (go to Project > Properties... > Resources > click to create a Resources file > look for the Add an Audio corner) and then play it to Soundplayer.

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.
}
  • 2

    This text is familiar, could you name the source from where you got it? Don’t copy information from where you don’t know if you can copy, and if you can copy, always cite the source. This is being discussed here: http://meta.pt.stackoverflow.com/q/5430/101

Browser other questions tagged

You are not signed in. Login or sign up in order to post.