home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / JDirect / cdplayer / CdPlayer.java next >
Encoding:
Java Source  |  2000-05-04  |  9.8 KB  |  364 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.  */
  7.  
  8.  
  9. import java.applet.Applet;
  10. import java.awt.*;
  11.  
  12. public class CdPlayer extends Applet implements Constants {
  13.     public static void main(String args[]) {
  14.         try {
  15.             CdPlayerFrame uiframe = new CdPlayerFrame("CD Player");
  16.             uiframe.setBackground(Color.lightGray);
  17.  
  18.             // Must show Frame before we size it so insets() will return 
  19.             // valid values
  20.             uiframe.show();
  21.             uiframe.setVisible(false);
  22.             Insets insets = uiframe.insets();
  23.             uiframe.setSize(insets.left + insets.right  + 300,
  24.                             insets.top  + insets.bottom + 100);
  25.             uiframe.show();
  26.         } catch (Exception e) { 
  27.             e.printStackTrace(); 
  28.         }
  29.     }
  30.  
  31.     public void init() {
  32.         setLayout(new BorderLayout(0, 0));
  33.         setFont(new Font("Dialog", Font.BOLD, 12));
  34.         add("Center", new CdPlayerPanel());
  35.     }
  36. }
  37.  
  38.  
  39.  
  40. class CdPlayerFrame extends Frame implements Constants {
  41.     CdPlayerPanel cdPanel;
  42.  
  43.     public CdPlayerFrame(String str) {
  44.         super (str);
  45.         setLayout(new BorderLayout(0, 0));
  46.         setFont(new Font("Dialog", Font.BOLD, 12));
  47.  
  48.         cdPanel = new CdPlayerPanel();
  49.         add("Center", cdPanel);
  50.     }
  51.  
  52.     public boolean handleEvent(Event evt) {
  53.         switch (evt.id) {
  54.             case Event.WINDOW_DESTROY:
  55.                 cdPanel.media.ExitMedia();
  56.                 System.exit(0);
  57.                 return true;
  58.  
  59.             default:
  60.                 return super.handleEvent(evt);
  61.         }                        
  62.     }
  63. }
  64.  
  65.  
  66.  
  67. class CdPlayerPanel extends Panel implements Constants {
  68.     Button btnStop, btnPlay, btnSkip;
  69.     MediaAccess media; 
  70.  
  71.     CdPlayerPanel() {
  72.  
  73.         media = new MediaAccess();
  74.  
  75.         btnStop = new Button("Stop");
  76.         btnStop.setEnabled(false);
  77.  
  78.         btnPlay = new Button("Play");
  79.  
  80.         btnSkip = new Button("Skip");
  81.         btnSkip.setEnabled(false);
  82.  
  83.  
  84.  
  85.         add(btnStop);
  86.         add(btnPlay);
  87.         add(btnSkip);
  88.  
  89.     }
  90.  
  91.     public boolean action(Event e, Object arg) {
  92.         if (arg.equals("Stop")) {
  93.             btnStop.setEnabled(false);
  94.             btnPlay.setEnabled(true);
  95.             btnSkip.setEnabled(false);
  96.  
  97.             media.StopCD();
  98.         }
  99.  
  100.         if (arg.equals("Play")) {
  101.             btnStop.setEnabled(true);
  102.             btnPlay.setEnabled(false);
  103.             btnSkip.setEnabled(true);
  104.  
  105.             media.PlayCD(1);
  106.         }
  107.  
  108.         if (arg.equals("Skip")) {
  109.             int curTrack=media.GetCurrentTrack(), 
  110.                 totTracks=media.GetTotalTracks();
  111.         
  112.             if (curTrack == totTracks)
  113.                 media.PlayCD(1);
  114.             else
  115.                 media.PlayCD(curTrack+1);
  116.         }
  117.  
  118.         return(false);
  119.     }
  120. }
  121.  
  122.  
  123.  
  124. interface Constants {
  125.  
  126.     public static final int STOP   = 0;
  127.     public static final int SKIP   = 1;
  128.     public static final int PLAY   = 2;
  129.         
  130.  
  131.     static final int MCI_OPEN = 0x0803, 
  132.             MCI_SET_TIME_FORMAT = 0x00000400,
  133.             MCI_CLOSE = 0x0804,
  134.             MCI_STOP = 0x0808,
  135.             MCI_PLAY = 0x0806,
  136.             MCI_SET = 0x080D,
  137.             MCI_STATUS = 0x0814,
  138.             MCI_FROM = 0x00000004,
  139.             MCI_OPEN_TYPE = 0x00002000, 
  140.             MCI_OPEN_ELEMENT = 0x00000200,
  141.             MCI_OPEN_ALIAS = 0x00000400,
  142.             MCI_STATUS_ITEM = 0x00000100,
  143.             MCI_TRACK = 0x00000010,
  144.             MCI_CDA_STATUS_TYPE_TRACK = 0x00004001,
  145.             MCI_STATUS_MEDIA_PRESENT = 0x00000005,
  146.             MCI_STATUS_NUMBER_OF_TRACKS = 0x00000003,
  147.             MCI_STATUS_CURRENT_TRACK = 0x00000008,
  148.             MCI_FORMAT_TMSF = 10,
  149.             MCI_OPEN_SHAREABLE = 0x00000100;
  150. }
  151.  
  152. /** @dll.struct() */
  153. class MCI_SET_PARMS {
  154.     int dwCallback;
  155.     int dwTimeFormat;
  156.     int dwAudio;
  157. }
  158.  
  159. /** @dll.struct() */
  160. class MCI_STATUS_PARMS {
  161.     int dwCallback;
  162.     int dwReturn;
  163.     int dwItem;
  164.     int dwTrack;
  165. }
  166.  
  167. /** @dll.struct() */
  168. class MCI_GENERIC_PARMS {
  169.     int dwCallback;
  170. }
  171.  
  172. /** @dll.struct() */
  173. class MCI_PLAY_PARMS {
  174.     int dwCallback;
  175.     int dwFrom;
  176.     int dwTo;
  177. }
  178.  
  179. /** @dll.struct() */
  180. class MCI_OPEN_PARMS {
  181.     int dwCallback;
  182.     int wDeviceID;
  183.   
  184.     String lpstrDeviceType;
  185.     String lpstrElementName;
  186.     String lpstrAlias;
  187. }
  188.  
  189. class MediaAccess implements Constants {
  190.     static MCI_OPEN_PARMS open_parms = new MCI_OPEN_PARMS();
  191.     static int wDeviceID = 0;
  192.     static boolean MediaPlaying = false;
  193.  
  194.     public MediaAccess() {
  195.         if ((wDeviceID = FindDevice()) < 0) {
  196.             System.out.println("Error: CD-ROM device either not installed or is being used by another application.");
  197.             System.exit(0);
  198.         }
  199.         CloseDevice();
  200.     }
  201.  
  202.     public static int FindDevice() {
  203.         int iDrive, mciError;
  204.  
  205.         open_parms.lpstrDeviceType = "cdaudio";
  206.         open_parms.lpstrAlias = "myalias";
  207.         for (iDrive=0; iDrive<26; ++iDrive) {
  208.             Character tChar = new Character((char)('A' + iDrive));
  209.             open_parms.wDeviceID = 0;
  210.             open_parms.lpstrElementName = tChar.toString() + ":";
  211.  
  212.             mciError = mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT|MCI_OPEN_ALIAS, open_parms);
  213.             if (mciError == 0) {
  214.                 return(open_parms.wDeviceID);
  215.             }
  216.         }
  217.         return (-1);
  218.     }
  219.  
  220.     public static void printMCIError(int mciError) {
  221.         StringBuffer sbuf = new StringBuffer(512);
  222.         boolean flag;
  223.  
  224.         flag = mciGetErrorString(mciError, sbuf, sbuf.capacity()+1);
  225.         if (flag) System.out.println(sbuf);
  226.         else System.out.println("Call to mciGetErrorString() failed");
  227.     }
  228.  
  229.     public static boolean SetTimeFormat() {
  230.         int mciError;
  231.         MCI_SET_PARMS set_parms = new MCI_SET_PARMS();
  232.         
  233.         set_parms.dwTimeFormat = MCI_FORMAT_TMSF;
  234.         
  235.         mciError = mciSendCommand(wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, set_parms);
  236.  
  237.         if (mciError != 0) {
  238.             printMCIError(mciError);
  239.             return(false);
  240.         }
  241.         return(true);
  242.     }
  243.  
  244.     public static void ExitMedia() {
  245.         if (MediaPlaying) {
  246.             StopCD();
  247.         }
  248.     }
  249.  
  250.     public static void CloseDevice() {
  251.         int mciError;
  252.  
  253.         mciError = mciSendCommand(wDeviceID, MCI_CLOSE, 0, 0);
  254.  
  255.         if (mciError != 0) {
  256.             printMCIError(mciError);
  257.         }
  258.     }
  259.  
  260.     public static void OpenDevice() {
  261.         int mciError;
  262.  
  263.         mciError = mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT|MCI_OPEN_ALIAS, open_parms);
  264.         SetTimeFormat();
  265.  
  266.         if (mciError != 0) {
  267.             printMCIError(mciError);
  268.         }
  269.     }
  270.  
  271.     public static int GetTotalTracks() {
  272.         int mciError, dwFlags;
  273.         MCI_STATUS_PARMS status_parms = new MCI_STATUS_PARMS();
  274.  
  275.         OpenDevice();
  276.         dwFlags = MCI_STATUS_ITEM;
  277.         status_parms.dwItem = MCI_STATUS_NUMBER_OF_TRACKS;
  278.  
  279.         mciError = mciSendCommand(wDeviceID, MCI_STATUS, dwFlags, status_parms);
  280.  
  281.         if (mciError != 0) {
  282.             printMCIError(mciError);
  283.             CloseDevice();
  284.             return(0);
  285.         }
  286.  
  287.         CloseDevice();
  288.         return(status_parms.dwReturn);
  289.     }
  290.  
  291.     public static int GetCurrentTrack() {
  292.         int mciError, dwFlags;
  293.         MCI_STATUS_PARMS status_parms = new MCI_STATUS_PARMS();
  294.  
  295.         OpenDevice();
  296.         dwFlags = MCI_STATUS_ITEM;
  297.         status_parms.dwItem = MCI_STATUS_CURRENT_TRACK;
  298.  
  299.         mciError = mciSendCommand(wDeviceID, MCI_STATUS, dwFlags, status_parms);
  300.  
  301.         if (mciError != 0) {
  302.             printMCIError(mciError);
  303.             CloseDevice();
  304.             return(0);
  305.         }
  306.  
  307.         CloseDevice();
  308.         return(status_parms.dwReturn);
  309.     }
  310.  
  311.     public static void PlayCD (int track) {
  312.         int mciError;
  313.         MCI_PLAY_PARMS play_parms = new MCI_PLAY_PARMS();
  314.  
  315.         OpenDevice();
  316.         play_parms.dwFrom = track;
  317.         mciError = mciSendCommand(wDeviceID, MCI_PLAY, MCI_FROM, play_parms);
  318.  
  319.         if (mciError != 0) {
  320.             printMCIError(mciError);
  321.         }
  322.         MediaPlaying = true;
  323.  
  324.         CloseDevice();
  325.     }
  326.  
  327.     public static void StopCD () {
  328.         int mciError;
  329.         MCI_GENERIC_PARMS generic_parms = new MCI_GENERIC_PARMS();
  330.  
  331.         OpenDevice();
  332.         mciError = mciSendCommand(wDeviceID, MCI_STOP, 0, generic_parms);
  333.  
  334.         if (mciError != 0) {
  335.             printMCIError(mciError);
  336.         }
  337.         MediaPlaying = false;
  338.  
  339.         CloseDevice();
  340.     }
  341.  
  342.     /** @dll.import("WINMM") */
  343.     static native int mciSendCommand(int mciId, int uMsg, int dwParam1, MCI_SET_PARMS set_parms);
  344.  
  345.     /** @dll.import("WINMM") */
  346.     static native int mciSendCommand(int mciId, int uMsg, int dwParam1, MCI_STATUS_PARMS status_parms);
  347.  
  348.     /** @dll.import("WINMM") */
  349.     static native int mciSendCommand(int mciId, int uMsg, int dwParam1, MCI_PLAY_PARMS play_parms);
  350.  
  351.     /** @dll.import("WINMM") */
  352.     static native int mciSendCommand(int mciId, int uMsg, int dwParam1, MCI_OPEN_PARMS open_parms);
  353.  
  354.     /** @dll.import("WINMM") */
  355.     static native int mciSendCommand(int mciId, int uMsg, int dwParam1, MCI_GENERIC_PARMS generic_parms);
  356.  
  357.     /** @dll.import("WINMM") */
  358.     static native int mciSendCommand(int mciId, int uMsg, int dwParam1, int dwParam2);
  359.  
  360.     /** @dll.import("WINMM") */
  361.     static native boolean mciGetErrorString(int fdwError, StringBuffer lpstrErrorText, int cchErrorText);
  362. }
  363.  
  364.