Ejemplo n.º 1
0
    /// <summary>Opens a MIDI file.</summary>
    /// <remarks>Used interally only.</remarks>
    /// <exceptions cref="MediaException">Thrown when there was an error opening the file.</exceptions>
    /// <exceptions cref="FileNotFoundException">Thrown when the specified file could not be found.</exceptions>
    protected void OpenFile()
    {
        if (!File.Exists(Filename))
        {
            throw new FileNotFoundException();
        }
        MCI_OPEN_PARMS mciOpenParms = new MCI_OPEN_PARMS();

        mciOpenParms.lpstrDeviceType  = "sequencer";
        mciOpenParms.lpstrElementName = Filename;
        if (mciSendCommandOpen(0, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_ELEMENT, ref mciOpenParms) != 0)
        {
            throw new MediaException("Failed to open the MIDI device.");
        }
        // The device opened successfully; get the device ID.
        DeviceID = mciOpenParms.wDeviceID;
        // Get the length of the MIDI file
        MCI_STATUS_PARMS mciStatusParms = new MCI_STATUS_PARMS();

        mciStatusParms.dwItem = MCI_STATUS_LENGTH;
        if (mciSendCommandStatus(DeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM, ref mciStatusParms) != 0)
        {
            throw new MediaException("Couldn't get the length of the specified MIDI file.");
        }
        m_Length = mciStatusParms.dwReturn;
    }
Ejemplo n.º 2
0
 /// <summary>
 /// 获取音频文件的总时长
 /// </summary>
 /// <returns>返回音频文件的总时长</returns>
 public TimeSpan GetLength()
 {
     // Get audio length in millisecs
     if (m_DeviceID != 0)
     {
         MCI_STATUS_PARMS sp = new MCI_STATUS_PARMS();
         sp.dwCallback = IntPtr.Zero;
         sp.dwItem     = MCI_Status.MCI_STATUS_LENGTH;
         IntPtr spPointer = Marshal.AllocHGlobal(Marshal.SizeOf(sp));
         try
         {
             Marshal.StructureToPtr(sp, spPointer, false);
             UInt32 errorCode = mciSendCommand(m_DeviceID, (UInt32)MCI_Command.MCI_STATUS, (UInt32)(MCI_StatusFlags.MCI_WAIT | MCI_StatusFlags.MCI_STATUS_ITEM), spPointer);
             CheckError(errorCode);
             MCI_STATUS_PARMS sp2 = (MCI_STATUS_PARMS)Marshal.PtrToStructure(spPointer, typeof(MCI_STATUS_PARMS));
             return(new TimeSpan(0, 0, 0, 0, (Int32)sp2.dwReturn));
         }
         finally
         {
             Marshal.FreeHGlobal(spPointer);
         }
     }
     else
     {
         throw new InvalidOperationException("Sound device has already closed.");
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取播放状态
        /// </summary>
        /// <returns></returns>
        public SoundPlayerState GetState()
        {
            if (m_DeviceID != 0)
            {
                MCI_STATUS_PARMS sp = new MCI_STATUS_PARMS();
                sp.dwCallback = IntPtr.Zero;
                sp.dwItem     = MCI_Status.MCI_STATUS_MODE;
                IntPtr spPointer = Marshal.AllocHGlobal(Marshal.SizeOf(sp));
                try
                {
                    Marshal.StructureToPtr(sp, spPointer, false);
                    UInt32 errorCode = mciSendCommand(m_DeviceID, (UInt32)MCI_Command.MCI_STATUS, (UInt32)(MCI_StatusFlags.MCI_WAIT | MCI_StatusFlags.MCI_STATUS_ITEM), spPointer);
                    CheckError(errorCode);
                    MCI_STATUS_PARMS sp2 = (MCI_STATUS_PARMS)Marshal.PtrToStructure(spPointer, typeof(MCI_STATUS_PARMS));
                    switch (sp2.dwReturn)
                    {
                    case MCI_Mode.MCI_MODE_NOT_READY:
                        return(SoundPlayerState.NotReady);

                    case MCI_Mode.MCI_MODE_OPEN:
                        return(SoundPlayerState.Open);

                    case MCI_Mode.MCI_MODE_PAUSE:
                        return(SoundPlayerState.Pause);

                    case MCI_Mode.MCI_MODE_PLAY:
                        return(SoundPlayerState.Play);

                    case MCI_Mode.MCI_MODE_RECORD:
                        return(SoundPlayerState.Record);

                    case MCI_Mode.MCI_MODE_SEEK:
                        return(SoundPlayerState.Seek);

                    default:
                        return(SoundPlayerState.Stop);
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(spPointer);
                }
            }
            else
            {
                throw new InvalidOperationException("Sound device has already closed.");
            }
        }
        private int GetStatus(int item)
        {
            int err = 0;
            StringBuilder buf = new StringBuilder(1000);

            if (deviceID > 0)
            {
                MCI_STATUS_PARMS mcistatusparms = new MCI_STATUS_PARMS();
                mcistatusparms.dwItem = item;
                mcistatusparms.dwReturn = 0;
                err = mciSendCommandA(deviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM, ref mcistatusparms);

                if (err != 0)
                {
                    mciGetErrorStringA(err, buf, 1000);
                    //throw new ApplicationException("获取播放状态出错," + buf.ToString());
                }

                return mcistatusparms.dwReturn;
            }

            return -1;
        }
        /// <summary>
        /// 获取 trackNum 音轨的长度(驱动类型为 CDAudio 或 VideoDisc)。
        /// </summary>
        /// <param name="trackNum">音轨数。</param>
        /// <returns>trackNum 音轨的长度(MSF格式)。</returns>
        public int GetTrackLength(int trackNum)
        {
            int err = 0;
            StringBuilder buf = new StringBuilder(1000);

            if (deviceID > 0 && timeFormat == TimeFormat.TMSF)
            {
                MCI_STATUS_PARMS mcistatusparms = new MCI_STATUS_PARMS();
                mcistatusparms.dwItem = MCI_STATUS_LENGTH;
                mcistatusparms.dwTrack = trackNum;
                mcistatusparms.dwReturn = 0;
                err = mciSendCommandA(deviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM | MCI_TRACK, ref mcistatusparms);

                if (err != 0)
                {
                    mciGetErrorStringA(err, buf, 1000);
                    //throw new ApplicationException("获取播放状态出错," + buf.ToString());
                }

                return mcistatusparms.dwReturn;
            }
            return 0;
        }
Ejemplo n.º 6
0
 private static extern int mciSendCommandStatus(int wDeviceID, int uMessage, int dwParam1, ref MCI_STATUS_PARMS dwParam2);
 private static extern int mciSendCommandA(
     Int32 wDeviceID,
     UInt32 uMessage,
     Int32 dwParam,
     ref  MCI_STATUS_PARMS Any);