ACC: Sample MCI Functions to control a CD Player (1.x/2.0)

Last reviewed: June 8, 1997
Article ID: Q109371
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article demonstrates how to create sample Access Basic functions that you can use to make calls to the media control interface (MCI) using the mciSendString() function to run a CD player application from Microsoft Access.

NOTE: These sample functions require that you use Windows version 3.1, or Multimedia Windows version 3.0, or Windows 95 and a CD-ROM drive that supports MCI.

MORE INFORMATION

The sample program below uses several functions to control basic CD-ROM drive features such as stop, play, and eject. The functions can be used in any CD-ROM application you create. The sample program below is not a complete application, but is rather a basis you can use to create a full- featured application.

The simplest way to use these functions is to create a form with five command buttons on it. Have each of the buttons call one of the five sample functions below with its OnPush property.

To create these functions, create a new Access Basic module and enter the following Declarations and functions.

NOTE: In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

NOTE: You may have these Microsoft Windows API functions defined in an existing Microsoft Access library or module; therefore, your declarations may be duplicates causing a duplicate procedure name error message. There are two resolutions to this error:

  • Remove or comment out the duplicated declarations statements.
  • Use function aliasing by replacing the phrase "alias_" throughout the code below with your own unique aliasing characters. This method allows you to remove the other module and not lose the declarations for the API functions in the new module.

For more information about aliasing see page 369 of the "Building Applications" manual.

   '******* Declarations Section *******

   Option Compare Database
   Option Explicit

   Declare Function alias_mciSendString& Lib "MMSystem" alias_
   "mciSendString" (ByVal Sound$, ByVal RtnString$,_
   ByVal RtnLength%, ByVal Hndl%)

   Dim RetInt As Integer
   Dim RetStr As String * 64
   Dim mciStatement As String

   Global gTrackCurrent As Integer ' stores current track on CD.
   Global gTrackCount As Integer   ' stores count of tracks on CD.

   '******* Play Function *********

   Function CDPlay () As Integer
      ' This function initializes the audio device
      ' with the first call to mciSendString(), and
      ' then starts the CD playing with the second call.

      RetInt = alias_mciSendString("Open CDAudio", "", 0, 0)
      CDPlay = alias_mciSendString("Play CDAudio", "", 0, 0)
   End Function

   '******* Stop Function **********

   Function CDStop () As Integer
      ' This function stops the CD with the first call
      ' to mciSendString(), and closes the audio device
      ' with the second call.
      RetInt = alias_mciSendString("Stop CDAudio", "", 0, 0)
      CDStop = alias_mciSendString("Close CDAudio", "", 0, 0)
   End Function

   '******** Previous Track Function **********

   Function CDTrackPrevious ()
      Dim TrackPrevious As Integer

      ' Set time format to Tracks, minutes, seconds, and frames.
      RetInt = alias_mciSendString("Set CDAudio Time Format TMSF",_
      "", 0, 0)

      ' Retrieve the track number that is currently playing, and
      ' the total number of tracks on the CD. Store these values
      ' in global variables so that they are available to the form.

      mciStatement = "Status CDAudio Current Track"
      RetInt = alias_mciSendString(mciStatement, RetStr, 63, 0)
      gTrackCurrent = Val(RetStr)
      TrackPrevious = gTrackCurrent - 1
      mciStatement = "Status CDAudio Number of Tracks"
      RetInt = alias_mciSendString(mciStatement, RetStr, 63, 0)
      gTrackCount = Val(RetStr)

      ' Check to see if the current track is the first track. If it
      ' is seek to the beginning of the current track and play it.
      ' Otherwise, move to the previous track and play from there to
      ' the end of the CD.

      If TrackPrevious > 0 Then
         mciStatement = "Play CDAudio from " & TrackPrevious
         mciStatement = mciStatement & " To " & gTrackCount
         CDTrackPrevious = alias_mciSendString(mciStatement, "", 0, 0)
      Else
         mciStatement = "Seek CDAudio to " & TrackPrevious
         RetInt = alias_mciSendString(mciStatement, "", 0, 0)
         CDTrackPrevious = alias_mciSendString("Play CDAudio", "", 0, 0)
      End If
   End Function

   '********** Next Track Function **********

   Function CDTrackNext () As Integer
      Dim TrackNext As Integer

      ' Set time format to Tracks, minutes, seconds, and frames.
      RetInt = alias_mciSendString("Set CDAudio Time Format TMSF",_
      "", 0, 0)

      ' Retrieve the track number that is currently playing, and
      ' the total number of tracks on the CD. Store these values
      ' in global variables so that they are available to the form.

      mciStatement = "Status CDAudio Current Track"
      RetInt = alias_mciSendString(mciStatement, RetStr, 63, 0)
      gTrackCurrent = Val(RetStr)
      TrackNext = gTrackCurrent + 1
      mciStatement = "Status CDAudio Number of Tracks"
      RetInt = alias_mciSendString(mciStatement, RetStr, 63, 0)
      gTrackCount = Val(RetStr)

      ' Check to see if the current track is the last track. If it
      ' is seek to the beginning of the last track and play it.
      ' Otherwise, move to the next track and play from there to the end
      ' of the CD.

      If (TrackNext) < gTrackCount Then
          mciStatement = "Play CDAudio from " & TrackNext
          mciStatement = mciStatement & " To " & gTrackCount
          CDTrackNext = alias_mciSendString(mciStatement, "", 0, 0)
       Else
          mciStatement = "Seek CDAudio to " & gTrackCurrent
          RetInt = alias_mciSendString(mciStatement, "", 0, 0)
          CDTrackNext = alias_mciSendString("Play CDAudio", "", 0, 0)
      End If
   End Function

   '********* Eject Function **********

   Function CDEject () As Integer
      ' This function is not supported by all CD devices.
      CDEject = alias_mciSendString("Set CDAudio Door Open", "", 0, 0)
   End Function

   '********* Pause Function **********

   Function CDPause () As Integer
      CDPause = alias_mciSendString("Stop CDAudio", "", 0, 0)
   End Function

Note that these functions are only a sample; there are many other functions that can be implemented with mciSendString(). For example, the Position command returns the current track number that is being played. By using the Position command with the timer function described in article Q95924, "How to Implement a Timer," you can create a dynamically updated readout of the current track number being played.

REFERENCES

Microsoft Windows Software Development Kit "Multimedia Programmer's Reference," Chapter 7, "MCI Command Strings"

Microsoft Windows Device Driver Kit "Multimedia Device Adaptation Guide", Chapter 3, "MCI Device Drivers"


Additional query words: cdrom
Keywords : kbprg PgmApi
Version : 1.0 1.1 2.0
Platform : WINDOWS
Hardware : X86
Issue type : kbinfo


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 8, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.