VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "AudioPlayer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Public Enum PlaybackModeConst
LOCAL_FILE
ONLINE_STREAM
End Enum

Public Enum SelectedChannelConst
CH_ALL
CH_LEFT
CH_RIGHT
End Enum

Dim m1 As New Bass
Dim ch As New Channel
Dim str As New Stream
Dim cLoop As BassBooleanConst
Dim fLen As Long
Dim fStart As Long
Dim fSize As Long
Dim mEOF As Boolean
Dim mFilename As String
Dim mLoaded As Boolean
Dim mPrg As Integer
Dim mMode As PlaybackModeConst
Dim mURL As String

Property Let VolumeLevel(val As Single)
m1.SetVolume Round(val, 0) / 100
End Property

Property Get VolumeLevel() As Single
VolumeLevel = m1.GetVolume * 100
End Property

Sub LoadFile(Filename As String, Optional PlayNow As Boolean = False)
Dim lp As Long

If str.Handle <> 0 Then
ch.Stop_ str.Handle
End If

mFilename = Filename

str.Free
DoEvents
str.CreateFile Filename

fStart = 0
fLen = ch.GetLength(str.Handle, BASS_POS_BYTE)

If m1.ErrorGetCode = BASS_OK Then
mLoaded = True
mMode = LOCAL_FILE
End If

If mLoaded = True And PlayNow = True Then
Play
End If

End Sub

Sub LoadStreaming(URL As String, Optional PlayNow As Boolean = False)
Dim lp As Long

If str.Handle <> 0 Then
ch.Stop_ str.Handle
End If

mURL = URL

str.Free
DoEvents
str.CreateURL URL

fStart = 0
fLen = 0

Debug.Print m1.ErrorGetCode

If m1.ErrorGetCode = BASS_OK Then
mLoaded = True
mMode = ONLINE_STREAM
End If

If mLoaded = True And PlayNow = True Then
Play
End If

End Sub

Function GetStatus() As BassActiveConst
GetStatus = ch.ActiveState(str.Handle)
End Function

Function IsLoaded()
IsLoaded = mLoaded
End Function

Sub Pause()
ch.Pause str.Handle
End Sub

Sub Stop_()
ch.Stop_ str.Handle
End Sub

Sub Play(Optional loop_ As BassBooleanConst = BASS_FALSE)

If ch.ActiveState(str.Handle) = BASS_ACTIVE_STOPPED Then
    If mMode = LOCAL_FILE Then
    LoadFile mFilename
    Else
    LoadStreaming mURL
    End If
ElseIf ch.ActiveState(str.Handle) = BASS_ACTIVE_PLAYING And mMode = LOCAL_FILE Then
SeekProgress 0
Exit Sub
End If

cLoop = loop_
ch.Play str.Handle, loop_

End Sub

Sub SeekProgress(Percent As Single)
Dim Pos As Long
Pos = Round(fLen * (Percent / 100), 0)
ch.SetPosition str.Handle, Pos, BASS_POS_BYTE
End Sub

Sub SeekPosition(Position As Long)

If Position < fStart Or Position > fLen Then
Exit Sub
End If

ch.SetPosition str.Handle, Position, BASS_POS_BYTE
End Sub

Sub Release()
m1.Stop_
str.Free
m1.Free
End Sub

Function CurrentPosition() As Long
CurrentPosition = ch.GetPosition(str.Handle, BASS_POS_BYTE)
End Function

Function CurrentProgress() As Integer
Dim Pos As Long

If fLen = 0 Then
CurrentProgress = 0
Exit Function
End If

Pos = ch.GetPosition(str.Handle, BASS_POS_BYTE)
mPrg = Round(Pos / fLen * 100, 0)
CurrentProgress = mPrg
End Function

Function CurrentLevel(Mode As BassLevelConst, Optional SelectedChannel As SelectedChannelConst = CH_ALL) As Single

If SelectedChannel = CH_ALL Then
CurrentLevel = Round(ch.GetLevel(str.Handle))
ElseIf SelectedChannel = CH_LEFT Then
CurrentLevel = LoWord(ch.GetLevel(str.Handle))
ElseIf SelectedChannel = CH_RIGHT Then
CurrentLevel = HiWord(ch.GetLevel(str.Handle))
End If

End Function

Property Get PlaybackMode() As PlaybackModeConst
PlaybackMode = mMode
End Property

Function CurrentTime() As Long
CurrentTime = Round(ch.GetBytes2Seconds(str.Handle, ch.GetPosition(str.Handle, BASS_POS_BYTE)), 0)
End Function

Property Get EOF() As Boolean
If ch.GetPosition(str.Handle, BASS_POS_BYTE) = fLen Then
    If mMode = LOCAL_FILE Then
    EOF = True
    Else
    EOF = False
    End If
Else
EOF = False
End If

End Property

Property Get Handle() As Long
Handle = str.Handle
End Property

Property Get Duration() As Long
Dim ps As Long
ps = fLen
Duration = ch.GetBytes2Seconds(str.Handle, ps)
End Property

Private Sub Class_Initialize()
m1.Initialize
End Sub

Private Sub Class_Terminate()
str.Free
m1.Stop_
m1.Free
mLoaded = False
End Sub

Function LastErrorNumber() As BassErrorConsts
LastErrorNumber = m1.ErrorGetCode
End Function

