Re: VB6 and Windows Media Player

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Ron" <Ron@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:FA5C7BFB-8102-45E9-9BE2-F86CACC0AFE0@xxxxxxxxxxxxxxxx
Hi Mike
Using the 3 list boxes, Drive,Dir,File to display in the FLB all the *.wma
files from the my music folder, I want to click on a single track to fire
the
Media Player and listen to that track. The same way that it can be done
within the My Music Folder.

Well, while you *could* use Media Player to play the .wma file, it wouldn't
be my choice. I mean, if your program is supposed to be a "player" then
shouldn't IT play them rather than open the file in some other player? But
it really all depends on what you want to do.

This will do what you're asking:

-----BEGIN CODE
Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias
"ShellExecuteA" (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

Private Const SW_SHOW = 5


Dim sFileName As String
sFileName = "K:\My Music\Artists\KISS\Lemonheads - Plaster Caster.wma"
Call ShellExecute(Me.hwnd, "play", sFileName, vbNullString,
vbNullString, SW_SHOW)
-----END CODE

What this code will actually do is open and play the file in whatever
program is associated with .wma files (assuming that program supports the
"play" verb). The program may be Media Player or it may be some other
program. But if you're wanting to play the file externally from your app,
this is how you should do it.

If you'd prefer not to play the file in an external program (and this would
be my preference per above), you can use the mciSendString Win32API
function. Here's an example of that using the auto-open and auto-close
feature of MCI (this simply means you don't need to explicitly open and
close the file, although there are definate advantages to doing so such as
being able to get the total time of the song, seek forwards or backwards,
pause playback, etc.):

-----BEGIN CODE
Private Declare Function mciSendString Lib "winmm.dll" Alias
"mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As
String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

Private Declare Function GetShortPathNameAPI Lib "kernel32" Alias
"GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As
String, ByVal cchBuffer As Long) As Long

Private Const MAX_PATH As Long = 260

Private Function GetShortPathName(ByVal PathName As String) As String

Dim lRet As Long
Dim sBuffer As String

sBuffer = String$(MAX_PATH, vbNullChar)
lRet = GetShortPathNameAPI(PathName, sBuffer, MAX_PATH)
GetShortPathName = StripNulls(sBuffer)

End Function

Private Sub Form_Click()

Dim sFileName As String

sFileName = "K:\My Music\Artists\KISS\Lemonheads - Plaster Caster.wma"

'With MCI, it's important to always get the short path and file names
'because space characters are used to separate arguments in an MCI
'command string and MCI is also limited to how long a command string can
be.
'Long file names if enclosed in quotation marks can overcome the first
issue,
'but it's still quite easy to exceed the max length of a command string
'if long names are used.
sFileName = GetShortPathName(sFileName)

Call mciSendString("play " & sFileName, vbNullString, 0&, 0&)

End Sub
-----END CODE


A further question is that when I run the program the display in the DLB
is
always the same C:\Program Files\Visual Basic can I get it to always start
in
the folder Documents and Settings eg:- C:\Documents and Settings?

That's because when you start VB, that's what your "start in" folder is, so
that's what the DirListBox (and Drive and File listboxes) default to. In
your compiled app, they'd default to the folder where the EXE file is
located.

You will need to assign the folder to the DirListBox's Path property when
your program launches. You should also change the DriveListBox as
appropriate because you don't normally write code to change the DriveListBox
when the DirListBox changes (you write code to change the DirListBox when
the DriveListBox changes).

You should NOT hard-code C:\Documents and Settings because it won't be
C:\Documents and Settings on all computers. For example, I have a multiboot
system (Win98, Win2000, WinXP, and Vista). For WinXP, it's H:\Documents and
Settings and for Win2000 it's F:\Documents and Settings. For Win98, this
isn't even applicable. I won't go into Vista because so far I completely
dislike Vista (but I need to test my programs under it since people are
going to have it).

Also, wouldn't it make more sense for it to start in the My Music folder, or
perhaps the My Documents folder? Why are you wanting it to start in
Documents and Settings?

You can get the actual path for My Documents and My Music and many other
special folders by calling the SHGetSpecialFolderPath Win32 API function.
Here's example code, including most, if not all, the constants for the
special folders you can get with this function:

-----BEGIN CODE
Public Enum SHSpecialFolderIDs
CSIDL_DESKTOP = &H0& ' <desktop>
CSIDL_INTERNET = &H1& ' Internet Explorer
(icon on desktop)
CSIDL_PROGRAMS = &H2& ' Start Menu\Programs
CSIDL_CONTROLS = &H3& ' My Computer\Control
Panel
CSIDL_PRINTERS = &H4& ' My Computer\Printers
CSIDL_PERSONAL = &H5& ' My Documents
CSIDL_FAVORITES = &H6& ' <user name>\Favorites
CSIDL_STARTUP = &H7& ' Start
Menu\Programs\Startup
CSIDL_RECENT = &H8& ' <user name>\Recent
CSIDL_SENDTO = &H9& ' <user name>\SendTo
CSIDL_BITBUCKET = &HA& ' <desktop>\Recycle Bin
CSIDL_STARTMENU = &HB& ' <user name>\Start
Menu
CSIDL_MYDOCUMENTS = &HC& ' logical "My
Documents" desktop icon
CSIDL_MYMUSIC = &HD& ' "My Music" folder
CSIDL_MYVIDEO = &HE& ' "My Videos" folder
CSIDL_DESKTOPDIRECTORY = &H10& ' <user name>\Desktop
CSIDL_DRIVES = &H11& ' My Computer
CSIDL_NETWORK = &H12& ' Network Neighborhood
(My Network Places)
CSIDL_NETHOOD = &H13& ' <user name>\nethood
CSIDL_FONTS = &H14& ' windows\fonts
CSIDL_TEMPLATES = &H15&
CSIDL_COMMON_STARTMENU = &H16& ' All Users\Start Menu
CSIDL_COMMON_PROGRAMS = &H17& ' All Users\Start
Menu\Programs
CSIDL_COMMON_STARTUP = &H18& ' All Users\Startup
CSIDL_COMMON_DESKTOPDIRECTORY = &H19& ' All Users\Desktop
CSIDL_APPDATA = &H1A& ' <user
name>\Application Data
CSIDL_PRINTHOOD = &H1B& ' <user name>\PrintHood
CSIDL_LOCAL_APPDATA = &H1C& ' <user name>\Local
Settings\Applicaiton Data (non roaming)
CSIDL_ALTSTARTUP = &H1D& ' non localized startup
CSIDL_COMMON_ALTSTARTUP = &H1E& ' non localized common
startup
CSIDL_COMMON_FAVORITES = &H1F&
CSIDL_INTERNET_CACHE = &H20&
CSIDL_COOKIES = &H21&
CSIDL_HISTORY = &H22&
CSIDL_COMMON_APPDATA = &H23& ' All Users\Application
Data
CSIDL_WINDOWS = &H24& ' GetWindowsDirectory()
CSIDL_SYSTEM = &H25& ' GetSystemDirectory()
CSIDL_PROGRAM_FILES = &H26& ' C:\Program Files
CSIDL_MYPICTURES = &H27& ' C:\Program Files\My
Pictures
CSIDL_PROFILE = &H28& ' USERPROFILE
CSIDL_SYSTEMX86 = &H29& ' x86 system directory
on RISC
CSIDL_PROGRAM_FILESX86 = &H2A& ' x86 C:\Program Files
on RISC
CSIDL_PROGRAM_FILES_COMMON = &H2B& ' C:\Program
Files\Common
CSIDL_PROGRAM_FILES_COMMONX86 = &H2C& ' x86 Program
Files\Common on RISC
CSIDL_COMMON_TEMPLATES = &H2D& ' All Users\Templates
CSIDL_COMMON_DOCUMENTS = &H2E& ' All Users\Documents
CSIDL_COMMON_ADMINTOOLS = &H2F& ' All Users\Start
Menu\Programs\Administrative Tools
CSIDL_ADMINTOOLS = &H30& ' <user name>\Start
Menu\Programs\Administrative Tools
CSIDL_CONNECTIONS = &H31& ' Network and Dial-up
Connections
CSIDL_COMMON_MUSIC = &H35& ' All Users\My Music
CSIDL_COMMON_PICTURES = &H36& ' All Users\My Pictures
CSIDL_COMMON_VIDEO = &H37& ' All Users\My Video
CSIDL_RESOURCES = &H38& ' Resource Direcotry
CSIDL_RESOURCES_LOCALIZED = &H39& ' Localized Resource
Direcotry
CSIDL_COMMON_OEM_LINKS = &H3A& ' Links to All Users
OEM specific apps
CSIDL_CDBURN_AREA = &H3B& ' USERPROFILE\Local
Settings\Application Data\Microsoft\CD Burning
' unused = &H003c&
CSIDL_COMPUTERSNEARME = &H3D& ' Computers Near Me
(computered from Workgroup membership)
End Enum

Private Declare Function SHGetSpecialFolderPath Lib "Shell32" Alias
"SHGetSpecialFolderPathA" (ByVal hwndOwner As Long, ByVal lpszPath As
String, ByVal nFolder As Long, ByVal fCreate As Long) As Long

Private Const MAX_PATH As Long = 260

Public Function GetSpecialFolder(ByVal FolderID As SHSpecialFolderIDs) As
String

Dim sBuffer As String

'SHGetSpecialFolderPath is not necessarily supported under Win95. It can
be called
'under Win95 if IE4 AND the Desktop Update are installed.

'Cheap-shot error handling
'If running under Win95 or NT4, merely ignore the error
'This will result in a 0-length string being returned.
On Error Resume Next

sBuffer = String$(MAX_PATH, vbNullChar)

'For Win98 and higher
Call SHGetSpecialFolderPath(0&, sBuffer, FolderID, 0&)

GetSpecialFolder = StripNulls(sBuffer)

End Function

Public Function StripNulls(ByVal sText As String) As String

'Returns all characters up to a null character.
'If the string does not contain a null character,
'the string is returned unmodified.

Dim lNullPos As Long

lNullPos = InStr(sText, vbNullChar)

If lNullPos Then
StripNulls = Left$(sText, lNullPos - 1)
Else
StripNulls = sText
End If

End Function
-----END CODE

Now, browsing through those constants, I don't see one for specifically for
Documents and Settings, but there are constants for My Documents and My
Music (both personal and common). However, I believe Documents and Settings
is *always* in the root of the drive to which Windows is installed (and
can't be changed). So, you can get the Windows folder (using the
CSIDL_WINDOWS constant or by calling the GetWindowsDirectory API function),
extract the drive letter from that, and then append "Documents and
Settings".

I would not recommend relying on getting any of the other special folders
and trying to parse "Documents and Settings" from that even though on most
systems it will be part of the path. On most sytems, My Music (for example)
will be a subdirectory of My Documents which will be a subdirectory of <user
name> (or All Users, depending on which constant you use) which will be a
subdirectory of Documents and Settings. But this is not a SURE thing because
those particular special directories can be changed to something else. For
example, my My Documents directory is in the root of my K: drive and if
that's not bad enough, my My Music, My Pictures, and My Videos directories
are also in the root of my K: drive rather than subdirectories of My
Documents.

I do this because I hate having to navigate deep into directory structures
and my K: drive is used for nothing but "documents". Basically, I detest the
whole "Documents and Settings" structure because it makes for extremely long
and deep paths, and so I change it as much as Windows will allow. It's kind
of a pain to configure initially, but IMO makes things much easier and
better. Plus, it weeds out the poorly written programs (i.e. those that
hard-code these paths or assume, for example, that My Music will be a
subdirectory of My Documents).

I hope all of this is not "information overload" for you. <g>

--
Mike
Microsoft MVP Visual Basic


.



Relevant Pages

  • Re: InStrRev
    ... Ran the program on a single folder with 268 *.jpg pictures in that folder. ... Private Declare Function SHGetPathFromIDList _ ... ByVal sDir As String) As Long ... Dim bInf As BrowseInfo ...
    (microsoft.public.vb.general.discussion)
  • Re: FileListBox and directories with more than 32,768 files!
    ... Here is an example that allows you to select a folder and it then simply counts the number of files and folders within that folder ... fileName As String * MAX_PATH ... Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _ ... Dim browse_info As BrowseInfo ...
    (comp.lang.basic.visual.misc)
  • Re: Can this be faster?
    ... I know you're only looking for folders, but the code needs to check all files in order to determine whether it is a folder or not. ... fileName As String * MAX_PATH ... Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _ ... Dim browse_info As BrowseInfo ...
    (microsoft.public.vb.general.discussion)
  • Re: File Input Question
    ... To have a user select a folder, not a file, I use the below. ... pszDisplayName As String 'Address of a buffer to receive the display name of the folder ... Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long ... Private Declare Function GetWindowRect Lib "user32" ...
    (microsoft.public.excel.programming)
  • Re: Execute macro for all documents in the folder
    ... It is simple enough to batch process a single folder. ... Dim strFileName As String ... It seems as though the below macro from an earlier posting was put ...
    (microsoft.public.word.docmanagement)