Results 1 to 8 of 8

Thread: Playing Mp3 using the API??

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2001
    Location
    Yate
    Posts
    5

    Playing Mp3 using the API??

    How can I play an MP3 file using the windows API. can anyone please provide the code.


    thanks in advance



    John.

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    I don't think that it can be done through the API. The mp3 file has to be decoded and i don't think there is such an api call. (i may be wrong). You can always use the windows media player ax control to play mp3 files.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    Wink You are wrong...

    The file is included... It should make some sense
    Attached Files Attached Files
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  4. #4
    Matthew Gates
    Guest
    Well, I didn't download the bas file, but here's the code I usually post when someone asks .
    And Vlatko, the mciSendString API function does the job.


    VB Code:
    1. Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long
    2. Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
    3. Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
    4. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    5. Private Declare Function ReleaseCapture Lib "user32" () As Long
    6. Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    7. Private Const SWP_NOMOVE = &H2
    8. Private Const SWP_NOSIZE = &H1
    9. Private Const HWND_TOPMOST = -1
    10. Private Const HWND_NOTOPMOST = -2
    11. Private TheFileName As String
    12.  
    13.  
    14.  
    15. Private Function IsItPlaying() As Boolean
    16. Static yn As String * 30
    17.     mciSendString "status MP3Play mode", yn, Len(yn), 0
    18.     IsItPlaying = (Mid$(yn, 1, 7) = "playing")
    19. End Function
    20.  
    21. Private Function mp3Play(FileName As String)
    22. Dim cmdToDo As String * 255
    23. Dim dwReturn As Long
    24. Dim ret As String * 128
    25.  
    26. Dim tmp As String * 255
    27. Dim lenShort As Long
    28. Dim ShortPathAndFie As String
    29.    
    30.     If Dir(FileName) = "" Then
    31.         mmOpen = "Error with input file"
    32.         Exit Function
    33.     End If
    34.     lenShort = GetShortPathName(FileName, tmp, 255)
    35.     ShortPathAndFie = Left$(tmp, lenShort)
    36.     glo_hWnd = hwnd
    37.     cmdToDo = "open " & ShortPathAndFie & " type MPEGVideo Alias MP3Play"
    38.     dwReturn = mciSendString(cmdToDo, 0&, 0&, 0&)
    39.  
    40.     If dwReturn <> 0 Then  'not success
    41.         mciGetErrorString dwReturn, ret, 128
    42.         mmOpen = ret
    43.         MsgBox ret, vbCritical
    44.         Exit Function
    45.     End If
    46.    
    47.     mmOpen = "Success"
    48.     mciSendString "play MP3Play", 0, 0, 0
    49. End Function
    50.  
    51. Private Function mp3Pause()
    52.     mciSendString "pause MP3Play", 0, 0, 0
    53. End Function
    54.  
    55. Private Function mp3Unpause()
    56.     mciSendString "resume MP3Play", 0, 0, 0
    57. End Function
    58.  
    59. Private Function mp3Stop() As String
    60.     mciSendString "stop MP3Play", 0, 0, 0
    61.     mciSendString "close MP3Play", 0, 0, 0
    62. End Function
    63.  
    64. Private Function PositionInSec()
    65. Static PIS As String * 30
    66.     mciSendString "set MP3Play time format milliseconds", 0, 0, 0
    67.     mciSendString "status MP3Play position", PIS, Len(PIS), 0
    68.     PositionInSec = Round(Mid$(PIS, 1, Len(PIS)) / 1000)
    69. End Function
    70.  
    71. Private Function Position()
    72. Static P As String * 30
    73.     mciSendString "set MP3Play time format milliseconds", 0, 0, 0
    74.     mciSendString "status MP3Play position", P, Len(P), 0
    75.     sec = Round(Mid$(P, 1, Len(P)) / 1000)
    76.     If sec < 60 Then Position = "0:" & Format(sec, "00")
    77.     If sec > 59 Then
    78.         mins = Int(sec / 60)
    79.         sec = sec - (mins * 60)
    80.         Position = Format(mins, "00") & ":" & Format(sec, "00")
    81.     End If
    82. End Function
    83.  
    84. Private Function LengthInSec()
    85. Static L As String * 30
    86.     mciSendString "set MP3Play time format milliseconds", 0, 0, 0
    87.     mciSendString "status MP3Play length", L, Len(s), 0
    88.     LengthInSec = Round(Val(Mid$(L, 1, Len(L))) / 1000) 'Round(CInt(Mid$(s, 1, Len(s))) / 1000)
    89. End Function
    90.  
    91. Private Function Length()
    92. Static L As String * 30
    93.     mciSendString "set MP3Play time format milliseconds", 0, 0, 0
    94.     mciSendString "status MP3Play length", L, Len(L), 0
    95.     sec = Round(Val(Mid$(L, 1, Len(L))) / 1000) 'Round(CInt(Mid$(l, 1, Len(l))) / 1000)
    96.     If sec < 60 Then Length = "0:" & Format(sec, "00")
    97.     If sec > 59 Then
    98.         mins = Int(sec / 60)
    99.         sec = sec - (mins * 60)
    100.         Length = Format(mins, "00") & ":" & Format(sec, "00")
    101.     End If
    102. End Function
    103.  
    104. Private Function TimeRemaining() As String
    105.     RSecs = (LengthInSeconds) - (PositionInSec)
    106.     RMins = Int(RSecs / 60)
    107.     RSecs = Format((((RSecs / 60) - RMins) / 1.67), "0.00") * 100
    108.     If RSecs > 59 Then
    109.         RMins = RMins + 1
    110.         RSecs = 0#
    111.     Else: RMins = RMins
    112.         RSecs = RSecs
    113.     End If
    114.     If RSecs < 10 Then
    115.         TimeRemaining = RMins & ":0" & RSecs
    116.     Else: TimeRemaining = RMins & ":" & RSecs
    117.     End If
    118. End Function
    119.  
    120.  
    121. Private Function SeekTo(Second)
    122.     mciSendString "set MP3Play time format milliseconds", 0, 0, 0
    123.     If IsItPlaying = True Then mciSendString "play MP3Play from " & Second, 0, 0, 0
    124.     If IsItPlaying = False Then mciSendString "seek MP3Play to " & Second, 0, 0, 0
    125. End Function
    126.  
    127. Private Function FastFoward(Second)
    128.     Second = (PositionInSec + Second) * 1000
    129.     mciSendString "set mpeg time format milliseconds", 0&, 0&, 0&
    130.     If IsItPlaying = True Then
    131.         mciSendString "play mpeg from " & Second, 0&, 0&, 0&
    132.     Else: mciSendString "seek mpeg from " & Second, 0&, 0&, 0&
    133.     End If
    134. End Function
    135.  
    136. Private Function Rewind(Second)
    137.     Second = (PositionInSec - Second) * 1000
    138.     mciSendString "set mpeg time format milliseconds", 0&, 0&, 0&
    139.     If IsItPlaying = True Then
    140.         mciSendString "play mpeg from " & Second, 0&, 0&, 0&
    141.     Else: mciSendString "seek mpeg from " & Second, 0&, 0&, 0&
    142.     End If
    143. End Function
    144.  
    145. Private Function SetVolume(Channel As String, VolumeValue As Long) As String
    146. Dim cmdToDo As String * 128
    147. Dim dwReturn As Long
    148. Dim ret As String * 128
    149. Dim VolumeV As Long
    150. VolumeV = VolumeValue
    151.  
    152. If VolumeV < 0 Or VolumeV > 100 Then
    153.     SetVolume = "out of volume"
    154.     Exit Function
    155. End If
    156.  
    157. VolumeV = VolumeV * 10
    158.  
    159. If LCase(Channel) = "left" Or LCase(Channel) = "right" Then
    160.     cmdToDo = "setaudio mpeg " & Channel & " Volume to " & VolumeV
    161. Else
    162.     cmdToDo = "setaudio mpeg Volume to " & VolumeV
    163. End If
    164.  
    165. dwReturn = mciSendString(cmdToDo, 0&, 0&, 0&)
    166.  
    167. If Not dwReturn = 0 Then  'not success
    168.     mciGetErrorString dwReturn, ret, 128 'Get the error
    169.     SetVolume = ret
    170.     Exit Function
    171. End If
    172.  
    173. 'Success
    174. SetVolume = "Success"
    175. End Function
    176.  
    177. Private Function Pitch(Value As Integer) As String
    178.     Dim dwReturn As Long
    179.     Dim ret As String * 128
    180.     Dim Pval As Long
    181.     Value = Value - 100
    182.     Pval = 1000 + Value
    183.     dwReturn = mciSendString("set mpeg speed " & Pval, 0&, 0&, 0&)
    184.     If Not dwReturn = 0 Then  'not success
    185.         mciGetErrorString dwReturn, ret, 128 'Get the error
    186.         Pitch = ret
    187.         Exit Function
    188.     End If
    189.    
    190. Pitch = "Success"
    191. End Function

  5. #5
    Addicted Member HMPA's Avatar
    Join Date
    Mar 2001
    Location
    Dominican Republic
    Posts
    172
    In this Api Program for Play MP3:
    Don't work correct the fastforward, rewind, picht... can some body help me with the problems?
    Last edited by HMPA; Jun 29th, 2001 at 09:48 AM.
    .Net my life; my passion, my poison, ...

  6. #6
    Addicted Member HMPA's Avatar
    Join Date
    Mar 2001
    Location
    Dominican Republic
    Posts
    172
    help
    .Net my life; my passion, my poison, ...

  7. #7
    appi101
    Guest
    Hi Matthew

    In the code which you posted

    If Dir(FileName) = "" Then
    mmOpen = "Error with input file"
    Exit Function
    End If
    lenShort = GetShortPathName(FileName, tmp, 255)
    ShortPathAndFie = Left$(tmp, lenShort)
    glo_hWnd = hwnd

    what is the hwnd variable. also what is mmopen.

    Appi

  8. #8
    Matthew Gates
    Guest
    mmOpen is just a variable. Should be Msgbox or somethin' like that.

    glo_hWnd = hwnd is not needed. Just delete it if you wish.

Posting Permissions

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



Click Here to Expand Forum to Full Width