home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / JDirect / cdplay2 / CdPlayer.java next >
Encoding:
Java Source  |  2000-05-04  |  8.3 KB  |  306 lines

  1. /* (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  2.  *
  3.  * This application calls the MCI interface to drive an Audio CD-ROM.
  4.  * Before executing it, make sure there is an Audio CD in the CD drive.
  5.  *
  6.  * This version uses the com.ms.win32 packages which must be installed
  7.  * separately from the main Active Setup menu.
  8.  *
  9.  */
  10.  
  11.  
  12. import java.applet.Applet;
  13. import java.awt.*;
  14. import com.ms.win32.*;
  15.  
  16. public class CdPlayer extends Applet implements Constants {
  17.     public static void main(String args[]) {
  18.  
  19.         try {
  20.             Class.forName("com/ms/win32/Winmm");
  21.         } catch (Exception e) {
  22.             System.err.println("***ERROR***: In order to run this sample, you must ");
  23.             System.err.println("install the com.ms.win32 package. This is installed");
  24.             System.err.println("from the main menu of the Active Setup program.");
  25.             System.err.println("An alternate version that does not depend on the");
  26.             System.err.println("com.ms.win32 package can be found under the CdPlayer peer directory.");
  27.             return;
  28.         }
  29.  
  30.  
  31.         try {
  32.             CdPlayerFrame uiframe = new CdPlayerFrame("CD Player");
  33.             uiframe.setBackground(Color.lightGray);
  34.  
  35.             // Must show Frame before we size it so insets() will return 
  36.             // valid values
  37.             uiframe.show();
  38.             uiframe.setVisible(false);
  39.             Insets insets = uiframe.insets();
  40.             uiframe.setSize(insets.left + insets.right  + 300,
  41.                             insets.top  + insets.bottom + 100);
  42.             uiframe.show();
  43.         } catch (Exception e) { 
  44.             e.printStackTrace(); 
  45.         }
  46.     }
  47.  
  48.     public void init() {
  49.         setLayout(new BorderLayout(0, 0));
  50.         setFont(new Font("Dialog", Font.BOLD, 12));
  51.         add("Center", new CdPlayerPanel());
  52.     }
  53. }
  54.  
  55.  
  56.  
  57. class CdPlayerFrame extends Frame implements Constants {
  58.     CdPlayerPanel cdPanel;
  59.  
  60.     public CdPlayerFrame(String str) {
  61.         super (str);
  62.         setLayout(new BorderLayout(0, 0));
  63.         setFont(new Font("Dialog", Font.BOLD, 12));
  64.  
  65.         cdPanel = new CdPlayerPanel();
  66.         add("Center", cdPanel);
  67.     }
  68.  
  69.     public boolean handleEvent(Event evt) {
  70.         switch (evt.id) {
  71.             case Event.WINDOW_DESTROY:
  72.                 cdPanel.media.ExitMedia();
  73.                 System.exit(0);
  74.                 return true;
  75.  
  76.             default:
  77.                 return super.handleEvent(evt);
  78.         }                        
  79.     }
  80. }
  81.  
  82.  
  83.  
  84. class CdPlayerPanel extends Panel implements Constants {
  85.     Button btnStop, btnPlay, btnSkip;
  86.     MediaAccess media; 
  87.  
  88.     CdPlayerPanel() {
  89.  
  90.         media = new MediaAccess();
  91.  
  92.         btnStop = new Button("Stop");
  93.         btnStop.setEnabled(false);
  94.  
  95.         btnPlay = new Button("Play");
  96.  
  97.         btnSkip = new Button("Skip");
  98.         btnSkip.setEnabled(false);
  99.  
  100.  
  101.  
  102.         add(btnStop);
  103.         add(btnPlay);
  104.         add(btnSkip);
  105.  
  106.     }
  107.  
  108.     public boolean action(Event e, Object arg) {
  109.         if (arg.equals("Stop")) {
  110.             btnStop.setEnabled(false);
  111.             btnPlay.setEnabled(true);
  112.             btnSkip.setEnabled(false);
  113.  
  114.             media.StopCD();
  115.         }
  116.  
  117.         if (arg.equals("Play")) {
  118.             btnStop.setEnabled(true);
  119.             btnPlay.setEnabled(false);
  120.             btnSkip.setEnabled(true);
  121.  
  122.             media.PlayCD(1);
  123.         }
  124.  
  125.         if (arg.equals("Skip")) {
  126.             int curTrack=media.GetCurrentTrack(), 
  127.                 totTracks=media.GetTotalTracks();
  128.         
  129.             if (curTrack == totTracks)
  130.                 media.PlayCD(1);
  131.             else
  132.                 media.PlayCD(curTrack+1);
  133.         }
  134.  
  135.         return(false);
  136.     }
  137. }
  138.  
  139.  
  140.  
  141. interface Constants {
  142.  
  143.     public static final int STOP   = 0;
  144.     public static final int SKIP   = 1;
  145.     public static final int PLAY   = 2;
  146.         
  147.  
  148. }
  149.  
  150.  
  151. class MediaAccess implements Constants {
  152.     static MCI_OPEN_PARMS open_parms = new MCI_OPEN_PARMS();
  153.     static int wDeviceID = 0;
  154.     static boolean MediaPlaying = false;
  155.  
  156.     public MediaAccess() {
  157.         if ((wDeviceID = FindDevice()) < 0) {
  158.             System.out.println("Error: CD-ROM device either not installed or is being used by another application.");
  159.             System.exit(0);
  160.         }
  161.         CloseDevice();
  162.     }
  163.  
  164.     public static int FindDevice() {
  165.         int iDrive, mciError;
  166.  
  167.         open_parms.lpstrDeviceType = "cdaudio";
  168.         open_parms.lpstrAlias = "myalias";
  169.         for (iDrive=0; iDrive<26; ++iDrive) {
  170.             Character tChar = new Character((char)('A' + iDrive));
  171.             open_parms.wDeviceID = 0;
  172.             open_parms.lpstrElementName = tChar.toString() + ":";
  173.  
  174.             mciError = Winmm.mciSendCommand(0, win.MCI_OPEN, win.MCI_OPEN_TYPE|win.MCI_OPEN_SHAREABLE|win.MCI_OPEN_ELEMENT|win.MCI_OPEN_ALIAS, open_parms);
  175.             if (mciError == 0) {
  176.                 return(open_parms.wDeviceID);
  177.             }
  178.         }
  179.         return (-1);
  180.     }
  181.  
  182.     public static void printMCIError(int mciError) {
  183.         StringBuffer sbuf = new StringBuffer(512);
  184.         boolean flag;
  185.  
  186.         flag = Winmm.mciGetErrorString(mciError, sbuf, sbuf.capacity()+1);
  187.         if (flag) System.out.println(sbuf);
  188.         else System.out.println("Call to mciGetErrorString() failed");
  189.     }
  190.  
  191.     public static boolean SetTimeFormat() {
  192.         int mciError;
  193.         MCI_SET_PARMS set_parms = new MCI_SET_PARMS();
  194.         
  195.         set_parms.dwTimeFormat = win.MCI_FORMAT_TMSF;
  196.         
  197.         mciError = Winmm.mciSendCommand(wDeviceID, win.MCI_SET, win.MCI_SET_TIME_FORMAT, set_parms);
  198.  
  199.         if (mciError != 0) {
  200.             printMCIError(mciError);
  201.             return(false);
  202.         }
  203.         return(true);
  204.     }
  205.  
  206.     public static void ExitMedia() {
  207.         if (MediaPlaying) {
  208.             StopCD();
  209.         }
  210.     }
  211.  
  212.     public static void CloseDevice() {
  213.         int mciError;
  214.  
  215.         mciError = Winmm.mciSendCommand(wDeviceID, win.MCI_CLOSE, 0, 0);
  216.  
  217.         if (mciError != 0) {
  218.             printMCIError(mciError);
  219.         }
  220.     }
  221.  
  222.     public static void OpenDevice() {
  223.         int mciError;
  224.  
  225.         mciError = Winmm.mciSendCommand(0, win.MCI_OPEN, win.MCI_OPEN_TYPE|win.MCI_OPEN_SHAREABLE|win.MCI_OPEN_ELEMENT|win.MCI_OPEN_ALIAS, open_parms);
  226.         SetTimeFormat();
  227.  
  228.         if (mciError != 0) {
  229.             printMCIError(mciError);
  230.         }
  231.     }
  232.  
  233.     public static int GetTotalTracks() {
  234.         int mciError, dwFlags;
  235.         MCI_STATUS_PARMS status_parms = new MCI_STATUS_PARMS();
  236.  
  237.         OpenDevice();
  238.         dwFlags = win.MCI_STATUS_ITEM;
  239.         status_parms.dwItem = win.MCI_STATUS_NUMBER_OF_TRACKS;
  240.  
  241.         mciError = Winmm.mciSendCommand(wDeviceID, win.MCI_STATUS, dwFlags, status_parms);
  242.  
  243.         if (mciError != 0) {
  244.             printMCIError(mciError);
  245.             CloseDevice();
  246.             return(0);
  247.         }
  248.  
  249.         CloseDevice();
  250.         return(status_parms.dwReturn);
  251.     }
  252.  
  253.     public static int GetCurrentTrack() {
  254.         int mciError, dwFlags;
  255.         MCI_STATUS_PARMS status_parms = new MCI_STATUS_PARMS();
  256.  
  257.         OpenDevice();
  258.         dwFlags = win.MCI_STATUS_ITEM;
  259.         status_parms.dwItem = win.MCI_STATUS_CURRENT_TRACK;
  260.  
  261.         mciError = Winmm.mciSendCommand(wDeviceID, win.MCI_STATUS, dwFlags, status_parms);
  262.  
  263.         if (mciError != 0) {
  264.             printMCIError(mciError);
  265.             CloseDevice();
  266.             return(0);
  267.         }
  268.  
  269.         CloseDevice();
  270.         return(status_parms.dwReturn);
  271.     }
  272.  
  273.     public static void PlayCD (int track) {
  274.         int mciError;
  275.         MCI_PLAY_PARMS play_parms = new MCI_PLAY_PARMS();
  276.  
  277.         OpenDevice();
  278.         play_parms.dwFrom = track;
  279.         mciError = Winmm.mciSendCommand(wDeviceID, win.MCI_PLAY, win.MCI_FROM, play_parms);
  280.  
  281.         if (mciError != 0) {
  282.             printMCIError(mciError);
  283.         }
  284.         MediaPlaying = true;
  285.  
  286.         CloseDevice();
  287.     }
  288.  
  289.     public static void StopCD () {
  290.         int mciError;
  291.         MCI_GENERIC_PARMS generic_parms = new MCI_GENERIC_PARMS();
  292.  
  293.         OpenDevice();
  294.         mciError = Winmm.mciSendCommand(wDeviceID, win.MCI_STOP, 0, generic_parms);
  295.  
  296.         if (mciError != 0) {
  297.             printMCIError(mciError);
  298.         }
  299.         MediaPlaying = false;
  300.  
  301.         CloseDevice();
  302.     }
  303.  
  304. }
  305.  
  306.