mciSendString and repeat/loop play

netsniper

Member
Joined
Aug 21, 2003
Messages
12
I am trying to make an alarm clock which plays a user selected song at the selected time and loops it at the end. I am having a little bit of trouble here with looping it. I just found this forum and browsed around a little bit and found a media player that someone made, copied the code from there and used it in my project. I think it is called Media Player 2.4 - dunno.

Heres my timer function which checks for the events. The initial play function works at the alarm time, but I cant seem to get it to loop play. The loop call just ends the song. Any ideas?


------------------------BEGIN CODE----------------------------
Code:
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        If strAlarmTime = System.DateTime.Now.ToString Then
            Play()
        End If
        If EndOfSong() Then
            Play()
        End If
    End Sub
------------------------END CODE----------------------------


And heres my module code:


------------------------BEGIN CODE----------------------------
Code:
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

    Public FileName As String
    Public strAlarmTime As String

    Play a music file
    Public Sub Play()
        Try
            mciSendString("close song", 0, 0, 0) Close the previous song
            FileName = Chr(34) + Trim(FileName) + Chr(34) put " and " at the beinning and end of the filename
            mciSendString("open " & FileName & " alias song", 0, 0, 0) open the filename with the alias named song
            mciSendString("play song", 0, 0, 0) play the song
        Catch
            MsgBox(Err.Description, , " Internal Error")
        End Try
    End Sub

    Stop playing the song
    Public Sub StopPlay()
        Try
            mciSendString("stop song", 0, 0, 0)
        Catch
            MsgBox(Err.Description, , " Internal Error")
        End Try
    End Sub

    Close the mci device
    Public Function CloseAudio()
        Try
            mciSendString("close all", 0, 0, 0)
        Catch
            MsgBox(Err.Description, , " Internal Error")
        End Try
    End Function

    Check if the song has ended. Good for repeat play, ect.
    Public Function EndOfSong() As Boolean
        Dim ThePos As Integer
        Dim TheDur As Integer
        Try
            ThePos = GetPositioninSec()
            TheDur = GetDurationInSec()
            If ThePos = 0 Or TheDur = 0 Then Exit Function
            If ThePos = TheDur Then
                EndOfSong = True
            Else
                EndOfSong = False
            End If
        Catch
            MsgBox(Err.Description, , " Internal Error")
        End Try
    End Function

    Get the Position in Seconds
    Public Function GetPositioninSec() As Integer
        Try
            GetPositioninSec = Val(GetPositioninMS() \ 1000)
        Catch
            MsgBox(Err.Description, , " Internal Error")
        End Try
    End Function

    Get the Duration in Seconds
    Public Function GetDurationInSec() As Integer
        Try
            GetDurationInSec = GetDurationInMS() \ 1000
        Catch
            MsgBox(Err.Description, , " Internal Error")
        End Try
    End Function

    Get duration in Milliseconds
    Public Function GetDurationInMS() As Integer
        Dim TotalTime As String
        Try
            TotalTime = Space$(128)
            mciSendString("status song length", TotalTime, 128, 0&)
            GetDurationInMS = Val(TotalTime)
        Catch
            MsgBox(Err.Description, , " Internal Error")
        End Try
    End Function

    Get the position in MilliSeconds
    Public Function GetPositioninMS() As Integer
        Static Stat As String
        Try
            Stat = Space$(128)
            mciSendString("set song time format milliseconds", 0, 0, 0)
            mciSendString("status song position", Stat, 128, 0)
            GetPositioninMS = Val(Stat)
        Catch
            MsgBox(Err.Description, , " Internal Error")
        End Try
    End Function
------------------------END CODE----------------------------


Thanks guys. This forum is awesome and I really love the FAQs. Sorry if this question has been answered before, but I could not find it anywhere while searching.

Netsniper
 
Nevermind. After racking my brains out for a while, I figured out that the problem was actually in the Play() function itself. The Play() function adds quotes to the beginning and end of the FileName variable so that it can be properly interpreted by mciSendString. When I watched the FileName variable, I noticed that quotes were being added to the beginning and end, which made me realize the problem. The Play() function shoud actually be passed FileName using ByVal referencing so that it doesnt actually change the FileName variable, which is unnecessary!

To temporarily fix this, I just reassigned the variable FileName before it was requested by Play().

Netsniper
 
Back
Top