Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By jmcilhinney

Thread: Play music while console is running.

  1. #1
    Bryan_James is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2015
    Location
    Manila, Philippines, Asia, Earth
    Posts
    5
    Reputation
    0

    Play music while console is running.

    Hello!
    Is it possible to play a music while the console is running? if yes, then how?
    Thanks in advance!

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,778
    Reputation
    1703
    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.
    Bryan_James likes this.

  3. #3
    jolovic is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Jan 2016
    Posts
    1
    Reputation
    0
    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 by jolovic; 01-18-2016 at 4:39 PM.

  4. #4
    Herman is offline VB.NET Forum Idol
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2011
    Location
    Montreal, QC, CA
    Posts
    865
    Reputation
    1211
    "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.

  5. #5
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,778
    Reputation
    1703
    Quote Originally Posted by Herman View Post
    "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?

  6. #6
    Herman is offline VB.NET Forum Idol
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2011
    Location
    Montreal, QC, CA
    Posts
    865
    Reputation
    1211
    Yes, this particular component is not dependant on WPF, apart from needing a reference to WindowsBase.
    Last edited by Herman; 01-19-2016 at 10:10 AM.

  7. #7
    Herman is offline VB.NET Forum Idol
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2011
    Location
    Montreal, QC, CA
    Posts
    865
    Reputation
    1211
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •