|
V následujícím textu naleznete 3 postupy, jak využít k přehrání zvukových souborů API funkce Windows:
příklad č.1
'PREHRAVA SOUBORY Wav, AVI, MID
'POUZIJ SUB TESTIT()
'***************************************'
Public Const pcsSYNC = 0 ' CEKEJ NA UKONCENI PREHRAVANI
Public Const pcsASYNC = 1 ' NECEKEJ NA UKONCENI PREHRAVANI
Public Const pcsNODEFAULT = 2 ' POKUD NENELEZNES SOUBOR, PAK NEPREHRAVEJ PREDNASTAVENY ZVUK
Public Const pcsLOOP = 8 ' PREHRAVEJ V NEKONECNE SMYCCE (DOKUD NENI ZNOVU POUZIT apiPlaySound)
Public Const pcsNOSTOP = 16 ' NEPRERUSUJ PREHRAVANY SOUBOR
'Sound APIs
Private Declare Function apiPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
'AVI APIs
Private Declare Function apimciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Declare Function apimciGetErrorString Lib "winmm.dll" _
Alias "mciGetErrorStringA" (ByVal dwError As Long, _
ByVal lpstrBuffer As String, ByVal uLength As Long) As Long
Function fPlayStuff(ByVal strFilename As String, _
Optional intPlayMode As Integer) As Long
Dim lngRet As Long
Dim strTemp As String
Select Case LCase(fGetFileExt(strFilename))
Case "wav":
If Not IsMissing(intPlayMode) Then
lngRet = apiPlaySound(strFilename, intPlayMode)
Else
MsgBox "Must specify play mode."
Exit Function
End If
Case "avi", "mid":
strTemp = String$(256, 0)
lngRet = apimciSendString("play " & strFilename, strTemp, 255, 0)
End Select
fPlayStuff = lngRet
End Function
Function fStopStuff(ByVal strFilename As String)
'ZASTAV PREHRAVANI
Dim lngRet As Long
Dim strTemp As String
Select Case LCase(fGetFileExt(strFilename))
Case "Wav":
lngRet = apiPlaySound(0, pcsASYNC)
Case "avi", "mid":
strTemp = String$(256, 0)
lngRet = apimciSendString("stop " & strFilename, strTemp, 255, 0)
End Select
fStopStuff = lngRet
End Function
Private Function fGetFileExt(ByVal strFullPath As String) As String
Dim intPos As Integer, intLen As Integer
intLen = Len(strFullPath)
If intLen Then
For intPos = intLen To 1 Step -1
'NALEZNI POSLEDNI ZNAK \
If Mid$(strFullPath, intPos, 1) = "." Then
fGetFileExt = Mid$(strFullPath, intPos + 1)
Exit Function
End If
Next intPos
End If
End Function
Function fGetError(ByVal lngErrNum As Long) As String
' PREVED KOD CHYBY NA RETEZEC
Dim lngx As Long
Dim strErr As String
strErr = String$(256, 0)
lngx = apimciGetErrorString(lngErrNum, strErr, 255)
strErr = Left$(strErr, Len(strErr) - 1)
fGetError = strErr
End Function
Sub TestIt()
'ZADEJ AKTUALNI CESTU K PREHRAVANEMU SOUBORU
Dim a As Long
a = fPlayStuff("C:\zalohy\eee\01.avi")
' a = fStopStuff("C:\zalohy\eee\01.avi")
' a = fPlayStuff("C:\harp.wav")
' a = fStopStuff("C:\harp.wav")
End Sub
příklad č.2
Private Declare Function midiOutClose Lib "winmm.dll" _
(ByVal hMidiOut As Long) As Long
Private Declare Function midiOutOpen Lib "winmm.dll" _
(lphMidiOut As Long, _
ByVal uDeviceID As Long, _
ByVal dwCallback As Long, _
ByVal dwInstance As Long, _
ByVal dwFlags As Long) As Long
Private Declare Function midiOutShortMsg Lib "winmm.dll" _
(ByVal hMidiOut As Long, _
ByVal dwMsg As Long) As Long
Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Dim hMidiOut As Long
Public lanote As Integer
Public Const durée As Integer = 250 ' milisekundy'
Sub PlayMIDI()
Numéro = 0
notes_a_jouer = Array(50, 50, 52, 50, 55, 54, 50, _
50, 52, 50, 57, 55, 50, 50, _
62, 59, 55, 54, 52, 60, 60, 59, 55, 57, 55)
For Each noteG In notes_a_jouer
Numéro = Numéro + 1
dur_n = Array(400, 250, 700, 600, 650, 800, 400, _
250, 700, 600, 650, 800, 600, 400, 800, _
600, 600, 800, 600, 400, 300, 600, 600, 600, 2000)
Temps = dur_n(Numéro - 1)
On Error Resume Next
midiOutClose hMidiOut '------ZAVRI PORT MIDI PRO PREDCHOZI TON
midiOutOpen hMidiOut, 0, 0, 0, 0 ' ------OTEVRI PORT MIDI PRO NOVY TON
midiOutShortMsg hMidiOut, RGB(192, 84 - 1, 127)
lanote = 12 + CInt(noteG) ' -----SPOCITEJ OKTAVU
note = RGB(144, lanote, 127)
midiOutShortMsg hMidiOut, note ' ----PREHRAJ NOTU NA PORTU MIDI
Sleep (Temps)
midiOutClose hMidiOut
Next
End Sub
příklad č.3
'ZAZNAM A PREHRATI ZVUKU VE FORMATU WAV
'**********************************************************
' MCI funkce lze pouzit pro zaznam souboru ve formatu WAV
' zakladem funkce MCI je mciSendString, ktera predava
' prikazy ve forme retezce systemovemu zarizeni MCI a vykonava je
' Zarizeni, ktere ma bzt pouzito je specifikovano v retezci
' V tomto pripade se jedna o waveaudio
Private Declare Function mciSendString Lib "winmm" _
Alias "mciSendStringA" ( _
ByVal lpstrCommand As String, _
ByVal lpstrReturnString As String, _
ByVal uReturnLength As Long, _
ByVal hwndCallback As Long) As Long
Private Declare Function mciExecute Lib "winmm" ( _
ByVal lpstrCommand As String) As Long
Private Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" ( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Private Const SND_SYNC = &H0
Private Const SND_ASYNC = &H1
Private Const SND_NODEFAULT = &H2
Private Const SND_LOOP = &H8
Private Const SND_NOSTOP = &H10
Private Const strAlias As String = "RcrdWavFile "
Private Const strOpenCmd As String = "Open new type waveaudio alias " & strAlias
Private Const strRecordCmd As String = "Record " & strAlias
Private Const strTimeCmd As String = "Record " & strAlias & " to "
Private Const strWaitCmd As String = " WAIT"
Private Const strSaveCmd As String = "Save " & strAlias
Private Const strCloseCmd As String = "Close " & strAlias
'soubor, do ktereho ma byt zvuk zaznamenan
Private Const strSaveAs As String = "C:\harp.wav"
Sub TestRecording()
Dim strCommand As String
Dim ExecCmd As Long
Dim tWait
tWait = 5
'otevri zaznamove zarizeni
ExecCmd = mciSendString(strOpenCmd, vbNullString, 0, 0&)
'zacni zaznamenavat
ExecCmd = mciSendString(strRecordCmd, vbNullString, 0, 0&)
'nastav dobu trvani zaznamu
ExecCmd = mciSendString(strTimeCmd & (tWait * 1000) & strWaitCmd, _
vbNullString, 0, 0&)
'uloz zaznamenany zvuk
ExecCmd = mciSendString(strSaveCmd & strSaveAs, vbNullString, 0, 0&)
'zavri zarizeni
ExecCmd = mciSendString(strCloseCmd, vbNullString, 0, 0&)
'info pro uzivatele o skonceni zaznamu
MsgBox "Záznam ukonèen", vbInformation
End Sub
Sub PlayBack()
WAVPlay strSaveAs
End Sub
Sub PlayBackLoop()
WAVLoop strSaveAs
End Sub
Sub PlayBackStop()
'pokud je prehravano v nekonecne smycce, pak zastav
Call WAVPlay(vbNullString)
End Sub
Sub WAVLoop(File As String)
Dim SoundName As String
Dim wFlags As Long
Dim x As Long
SoundName = File
wFlags = SND_ASYNC Or SND_LOOP
x = sndPlaySound(SoundName, wFlags)
If x = 0 Then MsgBox "Nemohu otevrit soubor. " & File
End Sub
Sub WAVPlay(File As String)
Dim SoundName As String
Dim wFlags As Long
Dim x As Long
SoundName = File
wFlags = SND_ASYNC Or SND_NODEFAULT
x = sndPlaySound(SoundName, wFlags)
If x = 0 Then MsgBox "Nemohu otevrit soubor " & File
End Sub
|