Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MP3 Play 1

Status
Not open for further replies.

aweiss

Programmer
Dec 9, 1999
15
0
0
DE
Hello,<br><br>How can I play a MP3 File from VB? Can anyone help me?<br><br>Bye<br><br>
 
aweiss -<br><br>The answer to this is the same as opening a HTML document, or opening a .doc file, etc.&nbsp;&nbsp;You use the ShellExecute API, which uses the extension of the file you pass (.mp3, in your case) to open the default viewer for that file type.<br><br>So, for a MP3 file called GroovySong.mp3, if the user has Sonique as their default MP3 player, it'll start Sonique&nbsp;&nbsp;and pass your GroovySong.mp3 along for it to play.&nbsp;&nbsp;If the user had RealPlayer as their default MP3 player, it would use that program to play the song.<br><br><i>Here's the declare statement:</i><br><FONT FACE=monospace>&nbsp;&nbsp;&nbsp;&nbsp;Private Declare Function ShellExecute Lib &quot;shell32.dll&quot; Alias &quot;ShellExecuteA&quot; (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;Private Const SW_SHOWNORMAL = 1</font><br><br><i>And here's the call to it:</i><br><FONT FACE=monospace>&nbsp;&nbsp;&nbsp;&nbsp;Call ShellExecute(Me.hwnd, &quot;open&quot;, MP3FilePath, vbNull, vbNull, SW_SHOWNORMAL)</font><br><br>Chip H.<br><br><br>
 
So basicaly if Windows has an Mp3 Codec installed(like If you have the updated version of Media player) then media player control or API will play any codec supported. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
It's more than just the codec -- ShellExecute looks in the registry at the default value for the .mp3 extension (HKEY_CLASSES_ROOT\.mp3) for the program to launch.&nbsp;&nbsp;On my machine it's &quot;RealPlayer.MP3.6&quot;, which corresponds to the registry key:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;HKEY_CLASSES_ROOT\RealPlayer.MP3.6\shell\open\command<br><br>which the default value contains<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&quot;C:\Program Files\Real\RealPlayer\RealPlay.exe&quot; /m audio/mpeg %1<br><br>If the codec isn't found, the program itself will raise an error after it gets launched (or just die in silence, depending on how it's author wrote it)<br><br>Chip H.<br><br><br>
 
Of course I think he'd rather have his own player try to play it thru the codec, rather than openning the default player. which is why I recomended the Media player API.<br>(and if his own cant, have it prompt the user to get codec, or whichever helps) <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Ahhh, I didn't catch that.&nbsp;&nbsp;I naturally assumed he just wanted some sound to come out of the speakers, and didn't care how it got there, which is why I suggested that he just use whatever the default MP3 player is.<br><br>If he's trying to write a MP3 player in Visual Basic, wow!&nbsp;&nbsp;Media Player API would definately be the place to start.<br><br>I saw in the O'Reilly book catalog that they have a title out now that is all about MP3 (has some kind of crab or something on the cover), but I think it doesn't delve into the algorithms.<br><br>Chip H.<br>
 
Well if he wanted to write his own decoding MP3 player(like winamp or sonique, that decodes it themselves and not rely on Microsoft codecs) I definitly wouldnt think it would be very effective in Visual Basic, sounds more like something that should be done in C++, but oh well, hope either of our post were helpful for the language he is in. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
You can skip all this stuff and add the activeX component included with Pro and Enterprise Editions called active movie player. Set it to be visible=false in your form.load, and set its filename, Ex:

Activemovie1.filename = &quot;C:\mysong.mp3&quot;

Once the file is open and ready for play, the Activemovie player will trigger an Open_Complete event. You can use this to tell if the file is good to run. If you attempt to play a file, it will either not play or the previously loaded file will run instead. You play a file by executing:

Activemovie1.run

Stop it with: Activemovie.stop

You can tell where you're at using:
X = Activemovie1.currentposition
X is in tenths of seconds. You'll find you can do a lot with this cool control including playing more than one file at a time (mixing). You can set its playback volume and speed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top