home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / cdinfo / ccdinfo.cpp next >
C/C++ Source or Header  |  1998-03-26  |  3KB  |  96 lines

  1. // ccdinfo.cpp : device manager class implementation
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #include "stdafx.h"
  14. #include "ccdinfo.h"
  15.  
  16. CDInfo::CDInfo()
  17. {
  18.     // We don't have an open device yet
  19.     m_MCIOpen.wDeviceID = 0;
  20.     m_nNumberOfTracks = 0;
  21. }
  22.  
  23. CDInfo::~CDInfo()
  24. {
  25.     // If we have an open device then we'll be nice and close it.
  26. //  if (m_MCIOpen.wDeviceID != -1)
  27. //  {
  28. //      mciSendCommand(m_MCIOpen.wDeviceID, MCI_CLOSE, NULL, NULL);
  29. //  }
  30. }
  31.  
  32. short CDInfo::Read()
  33. {
  34.     int   i;
  35.     short nTrackLength;
  36.  
  37.     m_nNumberOfTracks = 0;
  38.     m_MCIOpen.lpstrDeviceType = (LPCTSTR)MCI_DEVTYPE_CD_AUDIO;
  39.     if (mciSendCommand(NULL, MCI_OPEN, MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID, (DWORD)(LPVOID)&m_MCIOpen))
  40.     {
  41.         ATLTRACE(_T("Couldn't open CD player"));
  42.     }
  43.  
  44.     m_MCIStatus.dwItem = MCI_STATUS_NUMBER_OF_TRACKS;
  45.     if (mciSendCommand(m_MCIOpen.wDeviceID, MCI_STATUS, MCI_STATUS_ITEM|MCI_WAIT, (DWORD)(LPVOID)&m_MCIStatus))
  46.     {
  47.         ATLTRACE(_T("Error getting number of tracks"));
  48.         mciSendCommand(m_MCIOpen.wDeviceID, MCI_CLOSE, NULL, NULL);
  49.         return 0;
  50.     }
  51.     m_nNumberOfTracks = (short)m_MCIStatus.dwReturn;
  52.     if (m_nNumberOfTracks > MAX_TRACKS)
  53.         m_nNumberOfTracks = MAX_TRACKS;
  54.  
  55.     m_MCIStatus.dwItem = MCI_STATUS_LENGTH;
  56.     for (i=0; i<m_nNumberOfTracks; i++)
  57.     {
  58.         m_MCIStatus.dwTrack = i+1;
  59.         mciSendCommand(m_MCIOpen.wDeviceID, MCI_STATUS, MCI_TRACK|MCI_STATUS_ITEM|MCI_WAIT, (DWORD)(LPVOID)&m_MCIStatus);
  60.         nTrackLength = (short)(MCI_MSF_MINUTE(m_MCIStatus.dwReturn)*60 + MCI_MSF_SECOND(m_MCIStatus.dwReturn));
  61.         m_nTrackLength[i] = nTrackLength;
  62.     }
  63.     mciSendCommand(m_MCIOpen.wDeviceID, MCI_CLOSE, NULL, NULL);
  64.  
  65.     return m_nNumberOfTracks;
  66. }
  67.  
  68. void CDInfo::Play(short nTrack)
  69. {
  70.     MCI_SET_PARMS   mciSet;
  71.     MCI_PLAY_PARMS  mciPlay;
  72.  
  73.     m_MCIOpen.lpstrDeviceType = (LPCTSTR)MCI_DEVTYPE_CD_AUDIO;
  74.     if (mciSendCommand(NULL, MCI_OPEN, MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID, (DWORD)&m_MCIOpen))
  75.     {
  76.         ATLTRACE(_T("Couldn't open CD player"));
  77.     }
  78.  
  79.     // Set the time format to track/minute/second/frame (TMSF).
  80.     mciSet.dwTimeFormat = MCI_FORMAT_TMSF;
  81.     if (mciSendCommand(m_MCIOpen.wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, (DWORD)&mciSet))
  82.     {
  83.         mciSendCommand(m_MCIOpen.wDeviceID, MCI_CLOSE, 0, NULL);
  84.         return;
  85.     }
  86.  
  87.     mciPlay.dwCallback = 0;
  88.     mciPlay.dwFrom = MCI_MAKE_TMSF(nTrack, 0, 0, 0);
  89.     if (mciSendCommand(m_MCIOpen.wDeviceID, MCI_PLAY, MCI_FROM, (DWORD)&mciPlay))
  90.     {
  91.         ATLTRACE(_T("Error playing track"));
  92.     }
  93.  
  94.     mciSendCommand(m_MCIOpen.wDeviceID, MCI_CLOSE, 0, NULL);
  95. }
  96.