Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Windows.Media
- Module Misc_Audio
- 'Stores all Audio Resources
- Private GameAudio As New Dictionary(Of String, AudioBit)
- 'Load an audio stream into the list for later playback
- Public Sub LoadAudio(ByVal Name As String, ByVal Path As String, Optional ByVal Hash As Byte() = Nothing)
- If Not GameAudio.ContainsKey(Name) Then GameAudio.Add(Name, New AudioBit(Path))
- End Sub
- 'Removes the specified audio from the list
- Public Sub UnloadAudio(ByVal Name As String)
- If GameAudio.ContainsKey(Name) Then
- GameAudio(Name).StopAll()
- GameAudio(Name).Close()
- GameAudio.Remove(Name)
- End If
- End Sub
- 'Removes all audio from the list
- Public Sub ClearAudio()
- For Each k As KeyValuePair(Of String, AudioBit) In GameAudio
- k.Value.StopAll()
- k.Value.Close()
- Next
- GameAudio.Clear()
- End Sub
- 'Plays the main audio stream
- Public Sub PlayAudio(ByVal Name As String)
- If GameAudio.ContainsKey(Name) Then GameAudio(Name).Play()
- End Sub
- 'Stops all instances for specified audio
- Public Sub StopAllAudio(ByVal Name As String)
- If GameAudio.ContainsKey(Name) Then GameAudio(Name).StopAll()
- End Sub
- 'Pause all instances for specified audio
- Public Sub PauseAllAudio(ByVal Name As String)
- If GameAudio.ContainsKey(Name) Then GameAudio(Name).PauseAll()
- End Sub
- 'Plays all instances for specified audio
- Public Sub PlayAllAudio(ByVal Name As String)
- If GameAudio.ContainsKey(Name) Then GameAudio(Name).PlayAll()
- End Sub
- 'Pauses all sub instances for specified audio
- Public Sub PauseAllSubAudio(ByVal Name As String)
- If GameAudio.ContainsKey(Name) Then GameAudio(Name).PauseAllSubInstances()
- End Sub
- 'Plays all sub instances for specified audio
- Public Sub PlayAllSubAudio(ByVal Name As String)
- If GameAudio.ContainsKey(Name) Then GameAudio(Name).PlayAllSubInstances()
- End Sub
- 'Stops a sub instance of specified audio
- Public Sub StopSubAudio(ByVal Name As String, ByVal Instance As UInteger)
- If GameAudio.ContainsKey(Name) Then GameAudio(Name).StopSubInstance(Instance)
- End Sub
- 'Stops all sub instances for specified audio
- Public Sub StopAllSubAudio(ByVal Name As String)
- If GameAudio.ContainsKey(Name) Then GameAudio(Name).StopAllSubInstances()
- End Sub
- 'Replays the main audio stream
- Public Sub ReplayAudio(ByVal Name As String)
- If GameAudio.ContainsKey(Name) Then GameAudio(Name).Replay()
- End Sub
- 'Stops the main audio stream
- Public Sub StopAudio(ByVal Name As String)
- If GameAudio.ContainsKey(Name) Then GameAudio(Name).Stop()
- End Sub
- 'Pauses the main audio stream
- Public Sub PauseAudio(ByVal Name As String)
- If GameAudio.ContainsKey(Name) Then GameAudio(Name).Pause()
- End Sub
- 'Plays audio as a separate instance
- Public Function PlayAudioInstance(ByVal Name As String) As UInteger
- If GameAudio.ContainsKey(Name) Then Return GameAudio(Name).PlayNewInstance() Else Return 0
- End Function
- 'Main audio object for main audio
- Private Class AudioBit
- Inherits MediaPlayer
- Private Shared Beginning As New TimeSpan(0, 0, 0, 0, 1)
- Private Instances As UInteger = 1
- Private TempStore As New Dictionary(Of Integer, TempAudioBit)
- Public Sub New(ByVal Path As String)
- Open(New Uri(Path))
- Position = Beginning
- End Sub
- 'Replays the main audio instance at position 1 millisecond
- Public Sub Replay()
- If Position <> Beginning Then Position = Beginning
- Play()
- End Sub
- 'Stops all audio instances in this object
- Public Sub StopAll()
- If TempStore.Count > 0 Then
- For Each k As KeyValuePair(Of Integer, TempAudioBit) In TempStore
- k.Value.Stop()
- k.Value.Close()
- Next
- End If
- TempStore.Clear()
- [Stop]()
- End Sub
- 'Pause all audio instances in this object
- Public Sub PauseAll()
- If TempStore.Count > 0 Then
- For Each k As KeyValuePair(Of Integer, TempAudioBit) In TempStore
- k.Value.Pause()
- Next
- End If
- Pause()
- End Sub
- 'Plays all audio instance in this object
- Public Sub PlayAll()
- If TempStore.Count > 0 Then
- For Each k As KeyValuePair(Of Integer, TempAudioBit) In TempStore
- k.Value.Play()
- Next
- End If
- Play()
- End Sub
- 'Pause all sub instances of audio in this object
- Public Sub PauseAllSubInstances()
- If TempStore.Count > 0 Then
- For Each k As KeyValuePair(Of Integer, TempAudioBit) In TempStore
- k.Value.Pause()
- Next
- End If
- End Sub
- 'Plays all sub instances of audio in this object
- Public Sub PlayAllSubInstances()
- If TempStore.Count > 0 Then
- For Each k As KeyValuePair(Of Integer, TempAudioBit) In TempStore
- k.Value.Play()
- Next
- End If
- End Sub
- 'Stops a specified Instance of audio
- Public Sub StopSubInstance(ByVal Instance As UInteger)
- If TempStore.ContainsKey(Instance) Then
- Dim AudioInstance As TempAudioBit = TempStore(Instance)
- AudioInstance.Stop()
- AudioInstance.Close()
- End If
- End Sub
- 'Stops all sub instances of audio to this object
- Public Sub StopAllSubInstances()
- If TempStore.Count > 0 Then
- For Each k As KeyValuePair(Of Integer, TempAudioBit) In TempStore
- k.Value.Stop()
- k.Value.Close()
- Next
- End If
- TempStore.Clear()
- End Sub
- 'Creates/Adds/Plays a new instance of the audio enclosed in this object
- Public Function PlayNewInstance() As UInteger
- If Instances = UInteger.MaxValue Then Instances = 1
- Dim CrntInstance As UInteger = Instances
- Instances += 1
- Dim tempAudio As New TempAudioBit(CrntInstance, Me)
- AddHandler tempAudio.Finished, AddressOf SubInstanceFinished
- TempStore.Add(CrntInstance, tempAudio)
- Return CrntInstance
- End Function
- 'When main audio is finished playback set to stop state and set position to 1 millisecond
- Private Sub AudioBit_MediaEnded(sender As Object, e As EventArgs) Handles Me.MediaEnded
- [Stop]()
- Position = Beginning
- End Sub
- 'If sub instance of audio is completed prepare it for closing and removal
- Private Sub SubInstanceFinished(ByVal Instance As Integer)
- If TempStore.ContainsKey(Instance) Then TempStore.Remove(Instance)
- End Sub
- 'Sub instance of audio for multiple playbacks simultaneously
- Private Class TempAudioBit
- Inherits MediaPlayer
- Public Event Finished(ByVal Instance As UInteger)
- Private Instance As UInteger
- Public Sub New(ByVal Instance As UInteger, ByVal Clone As AudioBit)
- Me.Instance = Instance
- Open(Clone.Source)
- Position = Beginning
- Play()
- End Sub
- 'When sub instance finishes playback, prepares itself for removal and closes itself
- Private Sub TempAudioBit_MediaEnded(sender As Object, e As EventArgs) Handles Me.MediaEnded
- RaiseEvent Finished(Instance)
- Close()
- End Sub
- End Class
- End Class
- End Module
Advertisement
Add Comment
Please, Sign In to add comment