Примеры работы функции mciSendString

Если нужно хорошее средство для воспроизведения видео и аудио, то лучше DirectShow под Windows я не нашел. Да придется где-то разобраться с COM интерфейсом, но оно того стоит. Если вам лень или просто некогда разбираться с COM , то тогда на помощь придет функция mciSendString. По крайней мере при работе с аудио под XP работает стабильно. В Windows 7 у меня не работает запись звука. Что касается видео, то это вообще гиблое дело (примеры с ним даже писать не буду). 
Эта статья не рассказ об этой функции, а только сборник примеров для работы с ней (по возможности примеры будут добавляться).

 

Открытие и закрытие лотка CD-ROM

Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _ 
     Byval lpstrCommand As String, _ 
     Byval lpstrReturnString As String, _ 
     Byval uReturnLength As Long, _ 
     Byval hwndCallback As Long) As Long 

mciSendString("set cdaudio door open", "", 0,0)
Sleep(2000)
mciSendString("set cdaudio door closed", "", 0,0)

 

Запись звука с микрофона

Можно так:

Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _ 
     Byval lpstrCommand As String, _ 
     Byval lpstrReturnString As String, _ 
     Byval uReturnLength As Long, _ 
     Byval hwndCallback As Long) As Long 
Print "If cancel record, press any key"  
mciSendString("open new type waveaudio alias mywav", "", 0, 0)
mciSendString("record mywav", "", 0, 0)
Sleep
mciSendString("stop mywav", "", 0, 0)
mciSendString("save mywav c:\cdtest.wav", "", 0, 0)
mciSendString("close mywav", "", 0, 0)

Или так:

Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _ 
     Byval lpstrCommand As String, _ 
     Byval lpstrReturnString As String, _ 
     Byval uReturnLength As Long, _ 
     Byval hwndCallback As Long) As Long 
Print "If cancel record, press any key"  
mciSendString("OPEN NEW TYPE WAVEAUDIO ALIAS rec", "", 0, 0)
mciSendString("set rec bitspersample 8 samplespersec 8000 channels 1", "", 0, 0)
mciSendString("record rec", "", 0, 0)
Sleep
mciSendString("STOP rec", "", 0, 0)
mciSendString("SAVE rec temp.wav", "", 0, 0)
mciSendString("CLOSE rec", "", 0, 0)

 

Проигрывание MIDI

Пример(проигрывание миди c автоожиданием):

Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _
     Byval lpstrCommand As String, _
     Byval lpstrReturnString As String, _
     Byval uReturnLength As Long, _
     Byval hwndCallback As Long) As Long

 mciSendString("OPEN Sample.mid TYPE SEQUENCER ALIAS myMIDI","",0,0)
 mciSendString("PLAY myMIDI wait","",0,0)
 mciSendString("CLOSE myMIDI","",0,0)

 

Проигрывание WAV, WMA и MP3

Пример1(проигрывание трека с автоожиданием):

Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _ 
     Byval lpstrCommand As String, _ 
     Byval lpstrReturnString As String, _ 
     Byval uReturnLength As Long, _ 
     Byval hwndCallback As Long) As Long 
Print "If cancel record, press any key"  
mciSendString("OPEN sample.mp3 TYPE MpegVideo ALIAS MP3","",0,0) 
mciSendString("PLAY MP3 wait","",0,0)
mciSendString("close MP3","",0,0) 

Пример2(реализованы методы: Play,Pause,Stop,Resume):

#INCLUDE "fbgfx.bi"
 Using FB
 Screen 1
 Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _ 
      Byval lpstrCommand As String, _ 
      Byval lpstrReturnString As String, _ 
      Byval uReturnLength As Long, _ 
      Byval hwndCallback As Long) As Long 
 Print "Press P to Play"
 Print "Press S to Stop"
 Print "Press Q to Pause"
 Print "Press W to Play after Pause"  
 Print "Press ESC to Quit"  
 mciSendString("OPEN C:\Sample.mp3 TYPE MpegVideo ALIAS MP3","",0,0) 

 Do
     If Multikey(SC_ESCAPE) Then 'ESC
        mciSendString("close MP3","",0,0)'Освобождаем хендл и выходим
         End
     Elseif Multikey(SC_P) Then 'Играть
         mciSendString("PLAY MP3 from 0","",0,0)
     Elseif Multikey(SC_S) Then 'Стоп
         mciSendString("STOP MP3","",0,0)
     Elseif Multikey(SC_Q) Then 'Пауза
         mciSendString("PAUSE MP3","",0,0)    
     Elseif Multikey(SC_W) Then 'Играть после паузы
         mciSendString("RESUME MP3","",0,0)                    
     Endif    
 Loop

Пример3(воспроизведение выбранного участка с получением диапазона. Выберите файл длительностью не менее 5 секунд):

#INCLUDE "fbgfx.bi"
Using FB
Screen 1
Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _
    Byval lpstrCommand As String, _
    Byval lpstrReturnString As String, _
    Byval uReturnLength As Long, _
    Byval hwndCallback As Long) As Long
Sub Draw_
    Dim As String*256 ST
    mciSendString("status MP3 position",ST,256,0)    
    Cls
    Print "Press P to Play select range"
    Print "Press ESC to Quit"
    Print ST
End Sub

mciSendString("OPEN C:\Sample.mp3 TYPE MpegVideo ALIAS MP3","",0,0)

Do
    If Multikey(SC_ESCAPE) Then 'ESC
        mciSendString("close MP3","",0,0)'Освобождаем хендл и выходим
        End
    Elseif Multikey(SC_P) Then 'Играть выбранный участок от 1 до 3 сек.
        mciSendString("PLAY MP3 from 1000 to 3000","",0,0)
    Endif
    Sleep(50)
    Draw_
Loop

Пример4 (перемещение указателя на выбранный участок и проигрывание с него до конца):

#INCLUDE "fbgfx.bi"
Using FB
Screen 1
Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _
     Byval lpstrCommand As String, _
     Byval lpstrReturnString As String, _
     Byval uReturnLength As Long, _
     Byval hwndCallback As Long) As Long
Print "Press P to Play"
Print "Press ESC to Quit" 
mciSendString("OPEN C:\Sample.mp3 TYPE MpegVideo ALIAS MP3","",0,0)

Do
    If Multikey(SC_ESCAPE) Then 'ESC
       mciSendString("close MP3","",0,0)'Освобождаем хендл и выходим
        End
    Elseif Multikey(SC_P) Then 'Играть
        mciSendString("Seek MP3 to 1500","",0,0)
        mciSendString("PLAY MP3","",0,0)               
    Endif   
Loop

 

Изменение громкости

Пример1(только изменение громкости):

#INCLUDE "fbgfx.bi"
Using FB
Screen 1
Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _
Byval lpstrCommand As String, _
Byval lpstrReturnString As String, _
Byval uReturnLength As Long, _
Byval hwndCallback As Long) As Long
Dim Shared n As Integer=1000
Sub volume()
    If n>1000 Then n=1000
    If n<0 Then n=0
    mciSendString("SetAudio MP3 volume to " & n,"",0,0)
End Sub
Print "Press + or = to big volume"
Print "Press - to small volume"
Print "Press ESC to Quit"
mciSendString("OPEN C:\Sample.mp3 TYPE MpegVideo ALIAS MP3","",0,0)
mciSendString("PLAY MP3 from 0","",0,0)
Do
    If Multikey(SC_ESCAPE) Then 'ESC
        mciSendString("close MP3","",0,0)'перед выходом освобождаем хендл
        End
    Elseif Multikey(SC_EQUALS) Then 'Увеличить громкость
        n+=10:volume()    
        Sleep(100)    
    Elseif Multikey(SC_MINUS) Then 'Уменьшить громкость
        n-=10:volume()
        Sleep(100)
    Endif
Loop

Пример2(изменение и получение громкости):

#INCLUDE "fbgfx.bi"
Using FB
Screen 1
Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _
Byval lpstrCommand As String, _
Byval lpstrReturnString As String, _
Byval uReturnLength As Long, _
Byval hwndCallback As Long) As Long
Dim Shared n As Integer=1000
Sub Print_
    Print "Press + or = to big volume"
    Print "Press - to small volume"
    Print "Press ESC to Quit"
    Print
End Sub
Sub volume()
    Cls
    Dim As String*16 ST
    If n>1000 Then n=1000
    If n<0 Then n=0
    mciSendString("SetAudio MP3 volume to " & n,"",0,0)
    mciSendString("status MP3 volume",ST,16,0)
    Print_
    Print "volume= " & ST
End Sub
mciSendString("OPEN C:\Sample.mp3 TYPE MpegVideo ALIAS MP3","",0,0)
mciSendString("PLAY MP3 from 0","",0,0)
Print_
volume()
Do
    If Multikey(SC_ESCAPE) Then 'ESC
        mciSendString("close MP3","",0,0)'перед выходом освобождаем хендл
        End
    Elseif Multikey(SC_EQUALS) Then 'Увеличить громкость
        n+=10:volume()
        Sleep(100)
    Elseif Multikey(SC_MINUS) Then 'Уменьшить громкость
        n-=10:volume()
        Sleep(100)
    Endif
Loop

 

Получение длины аудиофайла

#INCLUDE "fbgfx.bi"
 Using FB
 Screen 1
 Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _
     Byval lpstrCommand As String, _
     Byval lpstrReturnString As String, _
     Byval uReturnLength As Long, _
     Byval hwndCallback As Long) As Long

 mciSendString("OPEN C:\Sample.mp3 TYPE MpegVideo ALIAS MP3","",0,0)

 Do
     If Multikey(SC_ESCAPE) Then 'ESC
         mciSendString("close MP3","",0,0)'Освобождаем хендл и выходим
         End
     Elseif Multikey(SC_P) Then 'Играть       
         mciSendString("PLAY MP3","",0,0)
         Dim As String*260 ST
         mciSendString("status MP3 length",ST,260,0)
         Print "Press ESC to Quit"
         Print "Lenght audiofile=";ST
         Sleep(500)
     Endif
     Sleep(50)
 Loop

 

Скорость воспроизведения (только для Mp3 и WAV)

#INCLUDE "fbgfx.bi"
Using FB
Screen 1
Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _
    Byval lpstrCommand As String, _
    Byval lpstrReturnString As String, _
    Byval uReturnLength As Long, _
    Byval hwndCallback As Long) As Long
Sub Draw_
    Dim As String*256 ST
    mciSendString("status MP3 speed",ST,256,0)   
    Cls
    Print "Press P to Play select range"
    Print "Press ESC to Quit"
    Print "speed=";ST
End Sub

mciSendString("OPEN C:\Sample.mp3 TYPE MpegVideo ALIAS MP3","",0,0)

Do
    If Multikey(SC_ESCAPE) Then 'ESC
        mciSendString("close MP3","",0,0)'Освобождаем хендл и выходим
        End
    Elseif Multikey(SC_P) Then 'Играть 
        mciSendString("PLAY MP3 from 0","",0,0)
        mciSendString("set MP3 Speed 500","",0,0) 'скорость в 2 раза меньше обычной
    Endif
    Sleep(50)
    Draw_
Loop

 

Получение режимов воспроизведения

#INCLUDE "fbgfx.bi"
Using FB
Screen 1
Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _
    Byval lpstrCommand As String, _
    Byval lpstrReturnString As String, _
    Byval uReturnLength As Long, _
    Byval hwndCallback As Long) As Long
Sub Draw_
    Dim As String*256 ST
    mciSendString("status MP3 mode",ST,256,0)  
    Cls
    Print "Press P to Play select range"
    Print "Press ESC to Quit"
    Print "mode=";ST
End Sub
mciSendString("OPEN Sample.mp3 TYPE MpegVideo ALIAS MP3","",0,0)
mciSendString("PLAY MP3","",0,0)
Draw_
Sleep(2000)
mciSendString("Pause MP3","",0,0)
Draw_
Sleep(2000)
mciSendString("stop MP3","",0,0)
Draw_
Print "Press Any key to Quit"
Sleep()

Всего доброго!