PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
mp3 Play Length-VBForums
Results 1 to 6 of 6

Thread: mp3 Play Length

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,341
    I was able to get the length simply by creating a function that plays the mp3, gets the length and stops playing it. That way, the info is extracted without the sound actually coming through the speakers. Thanks guys.

  2. #2
    Lively Member formulav8's Avatar
    Join Date
    Mar 2002
    Location
    Orlando
    Posts
    116
    I thought you said you didn't want to have to get it from playing it.

  3. #3
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    Can you post that function please? :-D
    Luke

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,341
    Public Function Getmp3Length(sFilePath As String) As String
    mp3Play (sFilePath)
    Getmp3Length = Length
    mp3Stop
    End Function

    'Pass the full mp3 file path.

  5. #5
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    It would be much better to get the length from the header. I think you can get the file format specification of mp3 at wotsit.org
    Oetje
    oetje@home.nl
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  6. #6
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    Re: mp3 Play Length

    Originally posted by robertx
    Is there any way of determing the play length time of an mp3 file?
    If you strickly want to pass the path of the mp3 file and return the length, then try this...
    (modified from one of my apps to only return the duration from the mp3 file header)
    VB Code:
    1. 'IN A MODULE
    2. Option Explicit
    3. Type MP3Info
    4.     Bitrate As Integer
    5.     Frequency As Long
    6.     MpegLayer As Integer
    7.     Duration As String
    8.     VBR As Boolean
    9.     Frames As Integer
    10. End Type
    11. Public GetMP3Info As MP3Info
    12. ''this function converts Binary string to decimal integer
    13. Public Function BinToDec(BinValue As String) As Long
    14. BinToDec = 0
    15. For i = 1 To Len(BinValue)
    16. If Mid(BinValue, i, 1) = 1 Then
    17. BinToDec = BinToDec + 2 ^ (Len(BinValue) - i)
    18. End If
    19. Next i
    20. End Function
    21. Public Function ByteToBit(ByteArray) As String
    22. 'convert 4*1 byte array to 4*8 bits'''''
    23. ByteToBit = ""
    24.    For Z = 1 To 4
    25.     For i = 7 To 0 Step -1
    26.       If Int(ByteArray(Z) / (2 ^ i)) = 1 Then
    27.         ByteToBit = ByteToBit & "1"
    28.         ByteArray(Z) = ByteArray(Z) - (2 ^ i)
    29.       Else
    30.             If ByteToBit <> "" Then
    31.                 ByteToBit = ByteToBit & "0"
    32.             End If
    33.       End If
    34.   Next
    35. Next Z
    36. End Function
    37. Private Function BinaryHeader(filename As String, ReadHeader As Boolean) As String
    38. Dim ByteArray(4) As Byte
    39. Dim XingH As String * 4
    40. FIO% = FreeFile
    41. Open filename For Binary Access Read As FIO%
    42. n& = LOF(FIO%): If n& < 256 Then Close FIO%: Return 'ny
    43. If ReadHeader = False Then GoTo 5:   'if we only want to read the IDtag goto 5
    44. Dim x As Byte
    45. '''''start check startposition for header''''''''''''
    46. '''''if start position <>1 then id3v2 tag exists'''''
    47.    For i = 1 To 5000            'check up to 5000 bytes for the header
    48.     Get #FIO%, i, x
    49.     If x = 255 Then             'header always start with 255 followed by 250 or 251
    50.         Get #FIO%, i + 1, x
    51.         If x > 249 And x < 252 Then
    52.             Headstart = i       'set header start position
    53.             Exit For
    54.         End If
    55.     End If
    56. Next i
    57. '''end check start position for header'''''''''''''
    58.  
    59. ''start check for XingHeader'''
    60.     Get #1, Headstart + 36, XingH
    61.     If XingH = "Xing" Then
    62.         GetMP3Info.VBR = True
    63.                     For Z = 1 To 4 '
    64.                     Get #1, Headstart + 43 + Z, ByteArray(Z)  'get framelength to array
    65.                     Next Z
    66.                     Frames = BinToDec(ByteToBit(ByteArray))   'calculate # of frames
    67.                     GetMP3Info.Frames = Frames                'set frames
    68.                     Else: GetMP3Info.VBR = False
    69.                     End If
    70. '''end check for XingHeader
    71.  
    72. '''start extract the first 4 bytes (32 bits) to an array
    73.    On Error GoTo msg 'ADDED AA june 4 2002
    74.    For Z = 1 To 4 '
    75.       Get #1, Headstart + Z - 1, ByteArray(Z)
    76.    Next Z
    77.   '''stop extract the first 4 bytes (32 bits) to an array
    78. 5:
    79.  
    80. Close FIO%
    81. BinaryHeader = ByteToBit(ByteArray)
    82. Exit Function
    83. msg:
    84. MsgBox "Could not retreive header information from: " & filename, vbCritical
    85. GoTo 5
    86. End Function
    87. Public Function ReadMP3(filename As String, ReadHeader As Boolean) As MP3Info
    88.  
    89.  Dim lngSeconds As Long, lngHours As Long, lngMins As Long, lngSecs2 As Long, lngSecs As Long
    90.  
    91.  bin = BinaryHeader(filename, ReadHeader)                      'extract all 32 bits
    92.  
    93. If ReadHeader = False Then Exit Function
    94. Version = Array(25, 0, 2, 1)                             'Mpegversion table
    95. MpegVersion = Version(BinToDec(Mid(bin, 12, 2)))    'get mpegversion from table
    96. layer = Array(0, 3, 2, 1)                           'layer table
    97. MpegLayer = layer(BinToDec(Mid(bin, 14, 2)))        'get layer from table
    98.  
    99. Select Case MpegVersion                                 'look for version to create right table
    100. Case 1                                                  'for version 1
    101. Freq = Array(44100, 48000, 32000)
    102. Case 2 Or 25                                            'for version 2 or 2.5
    103. Freq = Array(22050, 24000, 16000)
    104. Case Else
    105. Frequency = 0
    106. Exit Function
    107. End Select
    108. Frequency = Freq(BinToDec(Mid(bin, 21, 2)))             'look for frequency in table
    109. If GetMP3Info.VBR = True Then                           'check if variable bitrate
    110.     temp = Array(, 12, 144, 144)                        'define to calculate correct bitrate
    111.     Bitrate = (FileLen(filename) * Frequency) / (Int(GetMP3Info.Frames)) / 1000 / temp(MpegLayer)
    112.     Else                                                 'if not variable bitrate
    113.  
    114.     Dim LayerVersion As String
    115.     LayerVersion = MpegVersion & MpegLayer          'combine version and layer to string
    116.     Select Case Val(LayerVersion)                        'look for the right bitrate table
    117.     Case 11                                              'Version 1, Layer 1
    118.     Brate = Array(0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448)
    119.     Case 12                                              'V1 L1
    120.     Brate = Array(0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384)
    121.     Case 13                                               'V1 L3
    122.     Brate = Array(0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320)
    123.     Case 21 Or 251                                         'V2 L1 and 'V2.5 L1
    124.     Brate = Array(0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256)
    125.     Case 22 Or 252 Or 23 Or 253                            ''V2 L2 and 'V2.5 L2 etc...
    126.     Brate = Array(0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160)
    127.     Case Else                                               'if variable bitrate
    128.     Bitrate = 1                                             'e.g. for Variable bitrate
    129.     Exit Function
    130.     End Select
    131.     Bitrate = Brate(BinToDec(Mid(bin, 17, 4)))
    132.     End If
    133.  
    134. ms = (FileLen(filename) * 8) / Bitrate                  'calculate duration
    135. lngSeconds = Int(ms / 1000)
    136. lngHours = Fix(lngSeconds / 3600)
    137.     lngSecs = lngSeconds - (lngHours * 3600)
    138.     lngMins = Fix(lngSecs / 60)
    139.     lngSecs2 = lngSecs - (lngMins * 60)
    140.      Duration = Format(lngMins, "0:") & Format(lngSecs2, "00")
    141.  
    142.  GetMP3Info.Duration = Duration                       'set values
    143.      
    144. End Function
    145.  
    146. 'USAGE
    147. Private Sub Command1_Click()
    148. Dim tmpString As String
    149. tmpString = "D:\Full Albums\America - Greatest Hits\05 - Don't Cross The River.mp3"
    150. Call ReadMP3(tmpString, True)
    151. MsgBox tmpString & " <" & GetMP3Info.Duration & ">"
    152. End Sub
    Cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width