Public Class MciCommand #Region "フィールド" ' 再生や停止といった一連の動作を行うmciコマンド Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer ' エラー内容を返すmciコマンド Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal fdwError As Integer, ByVal lpszErrorText As String, ByVal cchErrorText As Integer) As Integer ' 再生時間 Private iPlayTime As Integer = 0 #End Region #Region "プロパティ" ''' <summary> ''' 再生時間を取得する。 ''' </summary> Public Property GetPlayTime() As Integer Get Return Me.iPlayTime End Get Private Set(ByVal value As Integer) Me.iPlayTime = value End Set End Property #End Region #Region "メソッド(各種MCIコマンド)" ''' <summary> ''' 対応したエラーメッセージを返す ''' </summary> ''' <param name="iAPIResult">対応したエラー</param> ''' <returns>エラー内容</returns> Private Function ErrorStringResult(ByVal iApiResult As Integer) As String Dim strBuf As String = New String(" "c, 255) Dim strErrMsg As String Call mciGetErrorString(iApiResult, strBuf, strBuf.Length) strErrMsg = strBuf.Replace(Chr(0), "") Return strErrMsg.Replace(" ", "") End Function ''' <summary> ''' mciSendStringのコマンドを送る ''' </summary> ''' <param name="strCmd">送るコマンド</param> ''' <returns>コマンド</returns> Private Function SendString(ByVal strCmd As String) As Long If strCmd.EndsWith("length ") Then '' バッファー 'Dim strBuf As String = New String(" "c, 255) 'mciSendString(strCmd, strBuf, strBuf.Length, 0) Me.iPlayTime = Me.MciSendLengthResult(strCmd) Else SendString = mciSendString(strCmd, "", 0, 0) End If ' エラーの戻り値 Dim lResult As Long = SendString If lResult <> 0 Then ' エラーメッセージを受け取る。 Dim strErrMsg As String = ErrorStringResult(Integer.Parse(lResult.ToString())) Console.WriteLine("frmMain.cs SendString ErrorMessage:\n") Console.WriteLine("\t" & strErrMsg & " Err:" & lResult & " Cmd:" & strCmd) End If End Function ''' <summary> ''' 再生時間や動画サイズの計算時に使用する。 ''' </summary> ''' <param name="strCmd">コマンド名</param> ''' <returns>再生時間か動画サイズ</returns> Private Function MciSendLengthResult(ByVal strCmd As String) As Integer ' バッファー Dim strBuf As String = New String(" "c, 255) mciSendString(strCmd, strBuf, strBuf.Length, 0) ' 再生時間か動画サイズを取得 Return Integer.Parse(Val(strBuf).ToString()) End Function ''' <summary> ''' プレイヤーをOPENする。 ''' </summary> ''' <param name="strFullPath">ファイルを再生するフルパス</param> Public Sub Open(ByVal strFullPath As String) Me.SendString(String.Format("open ""{0}"" alias MySound", strFullPath)) End Sub ''' <summary> ''' playコマンドを行う ''' </summary> ''' <param name="strAlias">エイリアス名</param> Public Sub Play(ByVal strAlias As String) Me.SendString(String.Concat("play ", strAlias)) Me.SendString(String.Concat("set ", strAlias, " time format milliseconds")) Me.SendString(String.Concat("status ", strAlias, " length ")) End Sub ''' <summary> ''' プレイヤーを終了する ''' </summary> ''' <param name="strAlias">エイリアス名</param> Public Sub PlayerStop(ByVal strAlias As String) Me.SendString(String.Concat("stop ", strAlias)) Me.SendString(String.Concat("close ", strAlias)) End Sub #End Region End Class