Results 1 to 6 of 6

Thread: how can i change this code so it can play mp3

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    3

    how can i change this code so it can play mp3

    i got this code from directx 8 sample, i have added filter to *.mp3 but i dont know what else should be added to make it function can anyone help me plz..

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''
    '
    ' Copyright (C) 2000 Microsoft Corporation. All Rights Reserved.
    '
    ' File: frmAudio.frm
    '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''

    'Global Variables
    Option Explicit
    Option Compare Text

    Implements DirectXEvent8

    Private dx As New DirectX8
    Private dmp As DirectMusicPerformance8
    Private dml As DirectMusicLoader8
    Private dmSeg As DirectMusicSegment8
    Private dmEvent As Long

    'API declare for windows folder
    Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

    Private Sub cmdExit_Click()
    Unload Me 'Cleanup happens in form unload
    End Sub

    Private Sub cmdOpen_Click()
    Static sCurDir As String
    Static lFilter As Long

    'We want to open a file now
    cdlOpen.flags = cdlOFNHideReadOnly Or cdlOFNFileMustExist
    cdlOpen.FilterIndex = lFilter
    'i added *.mp3 below
    cdlOpen.Filter = "MP3 Files (*.mp3)|*.mp3|Wave Files (*.wav)|*.wav|Music Files (*.mid;*.rmi)|*.mid;*.rmi|Segment Files (*.sgt)|*.sgt|All Audio Files|*.wav;*.mid;*.rmi;*.sgt;*.mp3|All Files (*.*)|*.*"
    cdlOpen.FileName = vbNullString
    If sCurDir = vbNullString Then
    'Set the init folder to \windows\media if it exists. If not, set it to the \windows folder
    Dim sWindir As String
    sWindir = Space$(255)
    If GetWindowsDirectory(sWindir, 255) = 0 Then
    'We couldn't get the windows folder for some reason, use the c:\
    cdlOpen.InitDir = "C:\"
    Else
    Dim sMedia As String
    sWindir = Left$(sWindir, InStr(sWindir, Chr$(0)) - 1)
    If Right$(sWindir, 1) = "\" Then
    sMedia = sWindir & "Media"
    Else
    sMedia = sWindir & "\Media"
    End If
    If Dir$(sMedia, vbDirectory) <> vbNullString Then
    cdlOpen.InitDir = sMedia
    Else
    cdlOpen.InitDir = sWindir
    End If
    End If
    Else
    cdlOpen.InitDir = sCurDir
    End If
    On Local Error GoTo ClickedCancel
    cdlOpen.CancelError = True
    cdlOpen.ShowOpen ' Display the Open dialog box

    'Save the current information
    sCurDir = GetFolder(cdlOpen.FileName)
    'Set the search folder to this one so we can auto download anything we need
    dml.SetSearchDirectory sCurDir
    lFilter = cdlOpen.FilterIndex

    On Local Error GoTo NoLoadSegment
    'Before we load the segment stop one if it's playing
    cmdStop_Click
    'Now let's load the segment
    If FileLen(cdlOpen.FileName) = 0 Then Err.Raise 5
    EnableTempoControl (Right$(cdlOpen.FileName, 4) <> ".wav")
    Set dmSeg = dml.LoadSegment(cdlOpen.FileName)
    If (Right$(cdlOpen.FileName, 4) = ".mid") Or (Right$(cdlOpen.FileName, 4) = ".rmi") Or (Right$(cdlOpen.FileName, 5) = ".midi") Then
    dmSeg.SetStandardMidiFile
    End If
    txtFile.Text = cdlOpen.FileName
    EnablePlayUI True
    sldTempo.Value = 10
    sldTempo_Click
    Exit Sub
    NoLoadSegment:
    MsgBox "Couldn't load this segment", vbOKOnly Or vbCritical, "Couldn't load"
    ClickedCancel:
    End Sub

    Private Sub cmdPlay_Click()
    If Not (dmSeg Is Nothing) Then
    If chkLoop.Value = vbChecked Then
    dmSeg.SetRepeats -1 'Loop infinitely
    Else
    dmSeg.SetRepeats 0 'Don't loop
    End If
    dmp.PlaySegmentEx dmSeg, DMUS_SEGF_DEFAULT, 0
    EnablePlayUI False
    End If
    End Sub

    Private Sub cmdStop_Click()
    If Not (dmSeg Is Nothing) Then dmp.StopEx dmSeg, 0, 0
    EnablePlayUI True
    End Sub

    Private Sub DirectXEvent8_DXCallback(ByVal eventid As Long)
    Dim dmNotification As DMUS_NOTIFICATION_PMSG

    'We only have one event
    If Not dmp.GetNotificationPMSG(dmNotification) Then
    MsgBox "Error processing this Notification", vbOKOnly Or vbInformation, "Cannot Process."
    Exit Sub
    Else
    If dmNotification.lNotificationOption = DMUS_NOTIFICATION_SEGEND Then 'The segment has ended
    EnablePlayUI True
    End If
    End If
    End Sub

    Private Sub Form_Load()
    InitAudio
    EnableTempoControl False
    End Sub

    Private Sub InitAudio()
    On Error GoTo FailedInit
    'We need to create our objects now
    Set dmp = dx.DirectMusicPerformanceCreate
    Set dml = dx.DirectMusicLoaderCreate
    Dim dmusAudio As DMUS_AUDIOPARAMS

    'Now call init audio
    dmp.InitAudio Me.hWnd, DMUS_AUDIOF_ALL, dmusAudio, Nothing, DMUS_APATH_SHARED_STEREOPLUSREVERB, 128
    dmp.SetMasterAutoDownload True

    'Now add a notification for the segment
    dmp.AddNotificationType DMUS_NOTIFY_ON_SEGMENT
    'Create an event so we can receive these
    dmEvent = dx.CreateEvent(Me)
    dmp.SetNotificationHandle dmEvent

    Exit Sub

    FailedInit:
    MsgBox "Could not initialize DirectMusic." & vbCrLf & "This sample will exit.", vbOKOnly Or vbInformation, "Exiting..."
    CleanupAudio
    Unload Me
    End
    End Sub

    Private Sub CleanupAudio()
    'Cleanup everything
    On Error Resume Next
    dmp.RemoveNotificationType DMUS_NOTIFY_ON_SEGMENT
    dx.DestroyEvent dmEvent
    If Not (dmSeg Is Nothing) Then dmp.StopEx dmSeg, 0, 0
    Set dmSeg = Nothing
    Set dml = Nothing
    If Not (dmp Is Nothing) Then dmp.CloseDown
    Set dmp = Nothing
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    CleanupAudio
    End Sub

    Private Function GetFolder(ByVal sFile As String) As String
    Dim lCount As Long

    For lCount = Len(sFile) To 1 Step -1
    If Mid$(sFile, lCount, 1) = "\" Then
    GetFolder = Left$(sFile, lCount)
    Exit Function
    End If
    Next
    GetFolder = vbNullString
    End Function

    Public Sub EnablePlayUI(fEnable As Boolean)
    'Enable/Disable the buttons
    If fEnable Then
    chkLoop.Enabled = True
    cmdStop.Enabled = False
    cmdPlay.Enabled = True
    cmdOpen.Enabled = True
    cmdPlay.SetFocus
    Else
    chkLoop.Enabled = False
    cmdStop.Enabled = True
    cmdPlay.Enabled = False
    cmdOpen.Enabled = False
    cmdStop.SetFocus
    End If
    End Sub

    Private Sub sldTempo_Click()
    'Update the tempo now
    dmp.SetMasterTempo (sldTempo.Value / 10)
    End Sub

    Private Sub sldTempo_Scroll()
    sldTempo_Click
    End Sub

    Private Sub sldVolume_Click()
    sldVolume_Scroll
    End Sub

    Private Sub sldVolume_Scroll()
    'Update the volume
    dmp.SetMasterVolume sldVolume.Value
    End Sub

    Private Sub EnableTempoControl(ByVal fEnable As Boolean)
    'If this is a wave file, turn off tempo control
    fraTempo.Enabled = fEnable
    sldTempo.Enabled = fEnable
    lbl(4).Enabled = fEnable
    lbl(5).Enabled = fEnable
    lbl(6).Enabled = fEnable
    If Not fEnable Then
    sldTempo.TickStyle = sldNoTicks
    Else
    sldTempo.TickStyle = sldBottomRight
    End If
    End Sub

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    well , I prefer mediaplayer ocx better than such long code , unless you want other implementations

  3. #3
    Lively Member
    Join Date
    Jul 2002
    Posts
    118
    The zip file attached uses VB and DirectShow to play MP3s. You will need DirectX8 installed on your system for it to work.

    When it is running click browse to find an MP3 on your comp. The initial file displayed wont exist on your comp since I only made it for me to practice coding with.

    Hope it helps

    Dan
    Attached Files Attached Files

  4. #4
    Lively Member
    Join Date
    Jul 2002
    Posts
    118
    PS U cant play MP3s with DirectMusic or DirectSound by the way. DirectShow is the only way to do that wiv DirectX.

    DM supports 4 file types I believe and MP3 is not one of them.

    DirectShow on the other hand rocks at it!

    PPS Check out the project references if u decide to download the code. Make sure ur project references the same things. I didnt like not referencing DX at first until I found that DirectShow is a part of DX. I think it is referenced as MediaSomething or AudioSomething or other. That is DirectShows reference name (why they didnt just reference it as DirectShow I dont know).

  5. #5
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I've found DirectShow way too restrictive. That is why I switched to FMod: http://www.fmod.org/.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    3

    Thumbs up thanks everyone!

    thanks everyone, i didn't know that DirectMusic cannot play mp3, thanks for the taught and thanks for the links and sample codes..

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