Play music while console is running.

What exactly do you mean by "while the console is running"? Are you saying that you want a console application to play a sound? I've never tried so I'm not sure. Console applications aren't really made to have interactions beyond the simple console interface and possibly not even that. If you want more than that then a console application probably isn't really the right option.

That said, you might first try the My.Computer.Audio.Play method. If that works then you're obviously good to go. Mind you, that only supports WAV sounds. If you're talking about WMA or MP3 or the like then that's not something that any .NET application can do directly.
 
Mate , try this.
My.Computer.Audio.Play("C:\song.wav",AudioPlayMode.Background)
Edit :
Or, you can add song to My Resources , so it will be My.Computer.Audio.Play(My.Resources.song,AudioPlayMode.Background)
 
Last edited:
"If you're talking about WMA or MP3 or the like then that's not something that any .NET application can do directly."

Sure you can! Check out System.Windows.Media.Mediaplayer class. It uses WMP as a back end and plays any format with the appropriate codecs installed.
 
"If you're talking about WMA or MP3 or the like then that's not something that any .NET application can do directly."

Sure you can! Check out System.Windows.Media.Mediaplayer class. It uses WMP as a back end and plays any format with the appropriate codecs installed.

I wasn't aware of that, not having used WPF all that much. Is it possible/practical to use outside of a WPF application though?
 
I dug up an example I posted a while ago on here for the MediaPlayer class. You need to add references to WindowsBase and PresentationCore for this to work:

Public Sub Test()
    ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf PlaySound), "D:\Music\Black Sabbath\Best of Black Sabbath\02. The Wizard.mp3")
    Thread.Sleep(5000)
    ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf PlaySound), "D:\Music\Black Sabbath\Best of Black Sabbath\14. Children of the Grave.mp3")
End Sub
 
Public Sub PlaySound(ByVal filename As String)
    Dim mplayer As New MediaPlayer
    AddHandler mplayer.MediaEnded, AddressOf MediaEndedHandler
    mplayer.Open(New Uri(filename))
    mplayer.Play()
End Sub
 
Private Sub MediaEndedHandler(sender As Object, e As EventArgs)
    DirectCast(sender, MediaPlayer).Close()
End Sub
 
Back
Top