![]() |
Tipp 0249
|
Midi-Dateien abspielen (einfach)
|
 |
|
Autor/Einsender: Datum: |
|
Alexander Csadek 20.06.2002 |
|
Entwicklungsumgebung:
DirectX-Version: |
|
VB 6
DirectX 7 |
|
|
MIDI-Dateien mit DirectX abzuspielen mag am Anfang auf Grund der vielen Objekte recht kompliziert aussehen, ist es aber nicht. Wenn man
erst einmal weis, wie die Objekte eingesetzt werden müssen, ist es genauso einfach, wie das Abspielen einer WAV-Datei mit
DirectSound.
|
Zusätzlich zu dem DirectX7-Objekt werden folgende 4 DirectMusic-Objecte benötigt:
|
DirectMusicLoader
DirectMusicSegment
DirectMusicPerformance
DirectMusicSegmentState
|
Mit dem DirectMusicLoader wird das MIDI geladen, im DirectMusicSegment stehen die Musik-Daten, über
DirectMusicPerformance
wird das MIDI abgespielt, und vom DirectMusicSegmentState
lassen sich noch nähere Angaben zum MIDI selbst entlocken.
|
|
|
Option Explicit
Dim DX7 As New DirectX7
Dim MIDLoader As DirectMusicLoader
Dim MIDSeg As DirectMusicSegment
Dim MIDPerf As DirectMusicPerformance
Dim MIDSegState As DirectMusicSegmentState
Dim mtTime As Long
Dim GetStartTime As Long
Dim Offset As Long
Dim MIDInPause As Boolean
Private Sub cmd_Open_Click()
Set MIDLoader = Nothing
Set MIDLoader = DX7.DirectMusicLoaderCreate
Set MIDPerf = Nothing
Set MIDPerf = DX7.DirectMusicPerformanceCreate
MIDPerf.Init Nothing, 0
MIDPerf.SetPort -1, 80
MIDPerf.SetMasterAutoDownload True
CDialog_Open.Filter = "MIDI Files (*.mid)|*.mid"
CDialog_Open.FileName = vbNullString
CDialog_Open.InitDir = App.Path
CDialog_Open.ShowOpen
If CDialog_Open.FileName <> vbNullString Then
Set MIDSeg = MIDLoader.LoadSegment(CDialog_Open.FileName)
MIDSeg.SetStartPoint 0
cmd_Play.Enabled = True
Else
Exit Sub
End If
End Sub
Private Sub cmd_Play_Click()
MIDSeg.SetStartPoint 0
Set MIDSegState = MIDPerf.PlaySegment(MIDSeg, 0, 0)
cmd_Play.Enabled = False
cmd_Pause.Enabled = True
cmd_Stop.Enabled = True
cmd_Open.Enabled = False
Timer1.Enabled = True
MIDInPause = False
End Sub
Private Sub cmd_Pause_Click()
If MIDPerf.IsPlaying(MIDSeg, MIDSegState) = True Then
MIDInPause = True
Timer1.Enabled = False
mtTime = MIDPerf.GetMusicTime()
GetStartTime = MIDSegState.GetStartTime()
MIDPerf.Stop MIDSeg, Nothing, 0, 0
Else
Offset = mtTime - GetStartTime + Offset + 1
MIDSeg.SetStartPoint Offset
Set MIDSegState = MIDPerf.PlaySegment(MIDSeg, 0, 0)
MIDInPause = False
Timer1.Enabled = True
End If
End Sub
Private Sub cmd_Stop_Click()
MIDPerf.Stop MIDSeg, MIDSegState, 0, 0
cmd_Play.Enabled = True
cmd_Pause.Enabled = False
cmd_Stop.Enabled = False
cmd_Open.Enabled = True
Timer1.Enabled = False
MIDInPause = False
End Sub
Private Sub cmd_Close_Click()
If MIDSeg Is Nothing Then
End
Else
If MIDPerf.IsPlaying(MIDSeg, MIDSegState) Then
MIDPerf.Stop MIDSeg, MIDSegState, 0, 0
End If
Set MIDSegState = Nothing
Set MIDPerf = Nothing
Set MIDSeg = Nothing
Set MIDLoader = Nothing
End
End If
End Sub
Private Sub Timer1_Timer()
If MIDInPause Then Exit Sub
If MIDPerf.IsPlaying(MIDSeg, MIDSegState) = False Then
Timer1.Enabled = False
cmd_Play.Enabled = True
cmd_Pause.Enabled = False
cmd_Stop.Enabled = False
cmd_Open.Enabled = True
End If
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, _
UnloadMode As Integer)
cmd_Close_Click
End Sub
|
|
|
|
|
|
Um dieses Beispiel ausführen zu können, wird die DirectX 7
for Visual Basic Type Library
benötigt (siehe dazu die Erläuterungen in der DirectX-Rubrik).
|
|
Windows-Version |
95 |
 |
|
98/SE |
 |
|
ME |
 |
|
NT |
 |
|
2000 |
 |
|
XP |
 |
|
Vista |
 |
|
Win
7 |
 |
|
|
VB-Version |
VBA 5 |
 |
|
VBA 6 |
 |
|
VB 4/16 |
 |
|
VB 4/32 |
 |
|
VB 5 |
 |
|
VB 6 |
 |
|
|
|
Download (19,1
kB)
|
Downloads bisher: [ 1068 ]
|
|
|