Link to home
Start Free TrialLog in
Avatar of yoi55
yoi55

asked on

MP3 Player help...

I am making an MP3 player and am stuck on how to get the length of an MP3 file WITHOUT using the MultiMedia Control!!!  Any help would be greatly appreciated!
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Calculates the duration in seconds of a .mp3 file. Also displays the associated layer, bitrate, sample rate of the file
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=4185 

This code shows you how to read the ID3-Tag from MP3-Files
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=6326 

ead the Mp3 file as a binary, and check the frameheader for the available data. Then take this data and calculate whatever you want. The following link will explain the positions of the different data in a mp3 file. It also explains the TAG format and how to access it. It also has the formula for calculating the framesize...
http://www.dv.co.yu/mp3list/mpeghdr.htm
http://home12.inet.tele.dk/mkaratha/
http://www.dialog-medien.de/html/mp3info.ocx.html


Or use this code :
VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   5235
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6795
   LinkTopic       =   "Form1"
   ScaleHeight     =   5235
   ScaleWidth      =   6795
   StartUpPosition =   3  'Windows Default
   Begin VB.TextBox Text7
      Height          =   285
      Left            =   2400
      TabIndex        =   15
      Top             =   4680
      Width           =   4215
   End
   Begin VB.DirListBox Dir1
      Height          =   2115
      Left            =   240
      TabIndex        =   14
      Top             =   2400
      Width           =   1935
   End
   Begin VB.FileListBox File1
      Height          =   2040
      Left            =   2280
      TabIndex        =   13
      Top             =   2400
      Width           =   2175
   End
   Begin VB.DriveListBox Drive1
      Height          =   315
      Left            =   240
      TabIndex        =   12
      Top             =   4680
      Width           =   1935
   End
   Begin VB.TextBox Text6
      Height          =   285
      Left            =   1200
      TabIndex        =   5
      Top             =   1920
      Width           =   2295
   End
   Begin VB.TextBox Text5
      Height          =   285
      Left            =   1200
      TabIndex        =   4
      Top             =   1560
      Width           =   2295
   End
   Begin VB.TextBox Text4
      Height          =   285
      Left            =   1200
      TabIndex        =   3
      Top             =   1200
      Width           =   2295
   End
   Begin VB.TextBox Text3
      Height          =   285
      Left            =   1200
      TabIndex        =   2
      Top             =   840
      Width           =   2295
   End
   Begin VB.TextBox Text2
      Height          =   285
      Left            =   1200
      TabIndex        =   1
      Top             =   480
      Width           =   2295
   End
   Begin VB.TextBox Text1
      Height          =   285
      Left            =   1200
      TabIndex        =   0
      Top             =   120
      Width           =   2295
   End
   Begin VB.Label Label8
      Caption         =   "32 bits header in binary format"
      Height          =   255
      Left            =   2880
      TabIndex        =   17
      Top             =   5040
      Width           =   2535
   End
   Begin VB.Label Label7
      Caption         =   "Click on file to display header"
      Height          =   615
      Left            =   4680
      TabIndex        =   16
      Top             =   2880
      Width           =   1695
   End
   Begin VB.Label Label6
      Caption         =   "MPEG Version"
      Height          =   255
      Left            =   0
      TabIndex        =   11
      Top             =   1920
      Width           =   1095
   End
   Begin VB.Label Label5
      Caption         =   "Mode"
      Height          =   255
      Left            =   0
      TabIndex        =   10
      Top             =   1560
      Width           =   975
   End
   Begin VB.Label Label4
      Caption         =   "Layer"
      Height          =   255
      Left            =   0
      TabIndex        =   9
      Top             =   1200
      Width           =   975
   End
   Begin VB.Label Label3
      Caption         =   "Frequency"
      Height          =   255
      Left            =   0
      TabIndex        =   8
      Top             =   840
      Width           =   1095
   End
   Begin VB.Label Label2
      Caption         =   "Emphasis"
      Height          =   255
      Left            =   0
      TabIndex        =   7
      Top             =   480
      Width           =   855
   End
   Begin VB.Label Label1
      Caption         =   "Bitrate"
      Height          =   255
      Left            =   0
      TabIndex        =   6
      Top             =   120
      Width           =   855
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
Call clearText 'clear all text boxes
End Sub
Private Sub clearText()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
End Sub

Private Sub File1_Click()
Dim Layer As String
Dim Frequency As String
Dim Bitrate As String
Dim Mode As String
Dim MpegVersion As String
Dim BinaryString As String
Dim Emphasis As String
Dim byteArray(4) As Byte    'array that store first four bytes
Dim bin As String           'string that store binary number converted from readed bytes
Dim BinString As String     'containing binary string
Dim DecString As Integer  'containing decimal extracted from BinString
'''''''''''''''end of declarations'''''''

If Right(Dir1.Path, 1) = "\" Then        'if path is in root
filename = Dir1.Path & File1.filename      'then "/" is not needed for path
Else
filename = Dir1.Path & "\" & File1.filename 'otherwise add "/" for correct filname
End If

''''''''''''her goes read file and binary conversion'''''

Open filename For Binary Access Read As #1  'open file #1 for read
   For z = 1 To 4                           'step through four bytes
   Get #1, z, byteArray(z)                  'store every(z)byte  in array position z
   Next z                                   'back for next byte
 Close #1                                   'close file
 bin = ""                                   'reset and build the desired binary number in this string
   For z = 1 To 4                           'convert all bytes to binary
     For i = 0 To 7 Step 1                  'Here comes the decimal=>binary conversion
         If byteArray(z) And (2 ^ i) Then   'Use the logical "AND" operator.
            bin = bin + "1"
            Else
            bin = bin + "0"
         End If
         Next i                             'End of binary conversion
Next z
BinaryString = bin
Text7.Text = BinaryString
'''''''''check frequency''''
DecString = 0
BinString = Mid(bin, 19, 2)         'take 19 to 21
For i = 1 To Len(BinString)         'convert to decimal
If Mid(BinString, i, 1) = 1 Then
DecString = DecString + 2 ^ (Len(BinString) - i)
End If
Next i
Select Case DecString
Case 0
Frequency = 44100
Case 1
Frequency = 32000
Case 2
Frequency = 48000
Case 3
End Select
'''''check layer''''
DecString = 0
BinString = Mid(bin, 10, 2)
For i = 1 To Len(BinString)
If Mid(BinString, i, 1) = 1 Then
DecString = DecString + 2 ^ (Len(BinString) - i)
End If
Next i
Select Case DecString
Case 0
Layer = ""
Case 1
Layer = 2
Case 2
Layer = 3
Case 3
Layer = 1
End Select
''''check mode''''
DecString = 0
BinString = Mid(bin, 31, 2)
For i = 1 To Len(BinString)
If Mid(BinString, i, 1) = 1 Then
DecString = DecString + 2 ^ (Len(BinString) - i)
End If
Next i
Select Case DecString
Case 0
Mode = "Stereo"
Case 1
Mode = "Dual Channel"
Case 2
Mode = "Joint stereo"
Case 3
Mode = "Mono"
End Select
''''check MpegVersion
If Mid(bin, 12, 1) = 0 Then
MpegVersion = 2
Else
MpegVersion = 1
End If
'''''check bitrate''''
DecString = 0
BinString = Mid(bin, 21, 4)
For i = 1 To Len(BinString)
If Mid(BinString, i, 1) = 1 Then
DecString = DecString + 2 ^ (Len(BinString) - i)
End If
Next i
Select Case DecString
Case 0
Bitrate = 0
Case 1
Bitrate = 112
Case 2
Bitrate = 56
Case 3
Bitrate = 224
Case 4
Bitrate = 40
Case 5
Bitrate = 160
Case 6
Bitrate = 80
Case 7
Bitrate = 320
Case 8
Bitrate = 32
Case 9
Bitrate = 128
Case 10
Bitrate = 64
Case 11
Bitrate = 256
Case 12
Bitrate = 48
Case 13
Bitrate = 192
Case 14
Bitrate = 96
Case 15
Bitrate = 0
If Layer = 1 Then
    Select Case DecString
    Case 0
Bitrate = 0
    Case 1
  Bitrate = 128
    Case 2
   Bitrate = 64
    Case 3
Bitrate = 256
    Case 4
Bitrate = 48
    Case 5
Bitrate = 192
    Case 6
Bitrate = 96
    Case 7
    Bitrate = 384
    Case 8
Bitrate = 32
    Case 9
Bitrate = 160
    Case 10
    Bitrate = 80
    Case 11
Bitrate = 320
    Case 12
Bitrate = 56
    Case 13
Bitrate = 224
    Case 14
  Bitrate = 112
    Case 15
Bitrate = 0
End Select
End If
End Select
'''''emphasis''''
DecString = 0
BinString = Mid(bin, 25, 2)
For i = 1 To Len(BinString)        'go from first
If Mid(BinString, i, 1) = 1 Then
DecString = DecString + 2 ^ (Len(BinString) - i)
End If
Next i
Select Case DecString
Case 0
Emphasis = "No"
Case 1
Emphasis = "-?-"
Case 2
Emphasis = "50/15"
Case 3
Emphasis = "CITT j. 17"

End Select


Text1.Text = Bitrate
Text2.Text = Emphasis
Text3.Text = Frequency
Text4.Text = Layer
Text5.Text = Mode
Text6.Text = MpegVersion


End Sub

Private Sub Form_Load()
File1.Pattern = "*.mp3"
End Sub

Avatar of [ fanpages ]
[ fanpages ]

Hi yoi55,

If you wish to use a third party ActiveX:
http://ewal.net/_shazam.aspx

Private Function ReadInformation(Filename As String) As Boolean
  Dim oMP3File As ShazamMP3.cMP3File
  On Error Resume Next
  Set oMP3File = New ShazamMP3.cMP3File
  If oMP3File.ReadFile(Filename) Then
    If oMP3File.HasMPEG Then
      Call MsgBox("Duration (in seconds): " & oMP3File.MPEGHeader.Duration)
    End If
    If oMP3File.HasID3Tag Then
      Call MsgBox("Title: " & oMP3File.ID3Tags.Title)
    End If
    ReadInformation = True
  Else
    ReadInformation = False
  End If
End Function  

BFN,

fp.
ASKER CERTIFIED SOLUTION
Avatar of [ fanpages ]
[ fanpages ]

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial