PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 [RESOLVED] Play MP3 File in Console Application-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] Play MP3 File in Console Application

  1. #1

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Resolved [RESOLVED] Play MP3 File in Console Application

    Hello, I was wondering if it is possible to play an MP3 file in a console application without using the Windows Media Player control (which is a bit annoying to use and would be more annoying on a console application) or DirectSound (which is not available in VS 2010)?

    Thanks in advance!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,243

    Re: Play MP3 File in Console Application

    try this:

    vb Code:
    1. Module Module1
    2.  
    3.     Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
    4.     (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As _
    5.      Integer, ByVal hwndCallback As Integer) As Integer
    6.  
    7.     Sub Main()
    8.         Dim _fileToPlay As String = Chr(34) + ("filename.mp3") + Chr(34)
    9.         Console.WriteLine("play mp3? [y/n]")
    10.         If Console.ReadLine.ToUpper = "Y" Then
    11.             mciSendString("open " & _fileToPlay & " alias myDevice", Nothing, 0, 0)
    12.             mciSendString("play myDevice", Nothing, 0, 0)
    13.             Console.ReadLine()
    14.         End If
    15.     End Sub
    16.  
    17. End Module

  3. #3
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,518

    Re: Play MP3 File in Console Application

    I think .paul. code will cover you fine but if you want something more complicated ( i had a crazy moment and i made this )you can see my application here:
    http://www.vbforums.com/showthread.php?p=3948319
    Slow as hell.

  4. #4

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Play MP3 File in Console Application

    Thank you .paul., it works perfectly! If it's at all possible, how do you make it synchronous? (If it isn't, never mind!)

  5. #5

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: [RESOLVED] Play MP3 File in Console Application

    Okay, I just found out a way to do it, but it involves using window handles which are not that great in a console application. Instead, I run mciSendString("close mySound", Nothing, 0, 0) when the user presses Enter. Resolved x 2

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,518

    Re: [RESOLVED] Play MP3 File in Console Application

    No offense minitech but the code i was implying to have a look was doing da job,if by synchronous you meant to be playing with the console and the mp3,wav whatever to get the keys and act accordingly.
    If that was not what you wanted my mistake.
    Slow as hell.

  7. #7

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: [RESOLVED] Play MP3 File in Console Application

    No, from what I understood of the sentence you are thinking of "asynchronous". I didn't want to close the sound before it had even started playing!

  8. #8
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,518

    Re: [RESOLVED] Play MP3 File in Console Application

    Nobody wanted to close the sound before playing(except if it was some spice girls tune ) .Well with the code i provide you can start,stop,mute,pause the file and the console closes only with escape or with the mouse on the X button.You can modify it also to your needs (p.e. load another file).
    Anyway you probably got what you needed so no point on continuing.
    Cheers.
    Slow as hell.

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width